0
点赞
收藏
分享

微信扫一扫

webService 实战篇--客户端调用

<wsdlsoap:address location=“http://10.213.54.118:7000/BundleSmart/webservice/GridWebService”/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

[](()IDEA 解析WSDL

==========================================================================

如果右键没有发现webservice ,说明你的IDEA 没有webservice 插件,不用慌,装一个就可以了,也很简单。

ctrl+alt+s,勾选如下图两个就可以了。

在这里插入图片描述

右键项目webservice,如下图,会弹出框。

在这里插入图片描述

在这里插入图片描述

file:/D:/guanxian.wsdl 是我本地的wsdl 文件路径,如果网络互通,可以直接填写url.

com.guanxian.webserver 是我们自定义存放的包路径。

点击OK,就可以下载成功,如下图。

在这里插入图片描述

[](()请求参数

===================================================================

在这之前,先说一下需求以及接口的格式吧。

在这里插入图片描述

输出格式:

<?xml version="1.0" encoding="UTF-8"?>

网格、经营部ID

1-网格、2-经营部

0-失败、1-成功

失败时反馈失败信息(例:该网格数据不存在、该经营部数据不存在。。。)

<gcll_line>

<gcll_name>专线名称</gcll_name>

<gcll_type>专线类型</gcll_type>

</gcll_line>

<gcll_line>

<gcll_name>专线名称</gcll_name>

<gcll_type>专线类型</gcll_type>

</gcll_line>

<gcll_site>

<gcll_name>站点名称</gcll_name>

</gcll_site>

<gcll_site>

<gcll_name>站点名称</gcll_name>

</gcll_site>

<gcll_customer>

<gcll_name>站点名称</gcll_name>

</gcll_customer>

<gcll_customer>

<gcll_name>站点名称</gcll_name>

</gcll_customer>

可以看到都是xml 格式的。我们这里请求的参数比较少,所以就直接拼接了。

private String queryFromEoms() throws Exception {

StringBuilder sb = new StringBuilder();

sb.append(“<?xml version=\"1.0\" encoding=\"UTF-8\"?>”)

.append(“”)

.append(“”)

.append(“”).append(grid).append(“”)

.append(“”).append(type).append(“”)

.append(“”)

.append(“”);

log.info(“------requestxml-------”+sb.toString());

GridWebServiceSoapBindingStub stub =new GridWebServiceSoapBindingStub();

return stub.getGridResource(stub.toString());

}

[](()XML 转json

========================================================================

但是返回给我们也是xml ,这里我们返回给前端xml的话就不好了,所以我们将xml 转成json 来返回。

[](()添加maven 依赖


com.alibaba

fastjson

1.2.67

dom4j

dom4j

1.6.1

[](()xml 转json 工具类


public class XmlUtils {

public static JSONObject xml2Json(String xmlStr) throws Exception{

Document doc= DocumentHelper.parseText(xmlStr);

JSONObject json=new JSONObject();

dom4j2Json(doc.getRootElement(), json);

return json;

}

/**

  • xml转json

  • @param element

  • @param json

*/

public static void dom4j2Json(Element element, JSONObject json){

//如果是属性

for(Object o:element.attributes()){

Attribute attr=(Attribute)o;

if(!isEmpty(attr.getValue())){

json.put(“@”+attr.getName(), attr.getValue());

}

}

List chdEl=element.elements();

if(chdEl.isEmpty()&&!isEmpty(element.getText())){//如果没有子元素,只有一个值

json.put(element.getName(), element.getText());

}

for(Element e:chdEl){//有子元素

if(!e.elements().isEmpty()){//子元素也有子元素

JSONObject chdjson=new JSONObject();

dom4j2Json(e,chdjson);

Object o=json.get(e.getName());

if(o!=null){

JSONArray jsona=null;

if(o instanceof JSONObject){//如果此元素已存在,则转为jsonArray

JSONObject jsono=(JSONObject)o;

json.remove(e.getName());

jsona=new JSONArray();

jsona.add(jsono);

jsona.add(chdjson);

}

if(o instanceof JSONArray){

jsona=(JSONArray)o;

jsona.add(chdjson);

}

json.put(e.getName(), jsona);

}else{

if(!chdjson.isEmpty()){

json.put(e.getName(), chdjson);

}

}

}else{//子元素没有子元素

for(Object o:element.attributes()){

Attribute attr=(Attribute)o;

if(!isEmpty(attr.getValue())){

jso 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 n.put(“@”+attr.getName(), attr.getValue());

}

}

if(!e.getText().isEmpty()){

json.put(e.getName(), e.getText());

}

}

}

}

public static boolean isEmpty(String str) {

if (str == null || str.trim().isEmpty() || “null”.equals(str)) {

return true;

}

return false;

}

}

[](()测试方法


举报

相关推荐

0 条评论