JAVA的WebService创建和使用
搭建环境
开发工具:IntelliJ IDEA 2019.2.4 x64
JDK:1.8.0_202
Tomcat:apache-tomcat-8.5.69
webservice服务端
一、文件-NEW-新建项目

二、

要生成wsdl文件(有的版本可以直接java文件右键),我的在tool里


webservice客户端
使用http方式进行调用
使用Maven的pom.xml增加以下依赖
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency>
public void GetHttpWebService(String url, String strParams) { String InterfaceName="UserTransmit"; String userName = null; String userPass=null; try { userName = AESHelper.encryptAES("admin"); userPass = AESHelper.encryptAES("pass"); } catch (Exception e) { e.printStackTrace(); } String name="aaa"; //soap 参数 String soapXml =""; soapXml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:otms=\"http://otms.efh.com.cn\">"; soapXml += " <soap:Header>"; soapXml += " <SoapHeader xmlns=\"http://otms.efh.com.cn/\">"; soapXml+= " <name>" + userName + "</name>"; soapXml+= " <password>" + userPass + "</password>"; soapXml+= " </SoapHeader>"; soapXml+= " </soap:Header>"; soapXml+= " <soap:Body>"; soapXml+= " <otms:" + InterfaceName + ">"; // 接口名称; if (strParams != null && strParams.length() > 0) { soapXml += " <wno>" + strParams + "</wno>"; // 参数加数据 } soapXml += " </otms:" + InterfaceName + ">"; // 接口名称; soapXml += "</soap:Body>"; soapXml += "</soap:Envelope>"; //获取http构建器对象 HttpClientBuilder builder = HttpClientBuilder.create(); CloseableHttpClient httpClient = builder.build(); //封装httppost请求对象 HttpPost httpPost = new HttpPost(url); httpPost.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build()); try { httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", ""); StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8")); httpPost.setEntity(data); //设置post请求参数实体 //执行http请求 CloseableHttpResponse response = httpClient .execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { // 打印响应内容 String retStr = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println(retStr); } // 释放资源 httpClient.close(); } catch (Exception e) { // logger.error("exception in doPostSoap1_1", e); } }
调用:
String url="http://localhost:8889/PubWebService?wsdl";
GetHttpWebService(url,"helloChenze");
其他
public static void GetHttpWebService(String url, String InterfaceName,String strParams) { String userName = null; String userPass=null; try { userName = AESHelper.encryptAES("admin"); userPass = AESHelper.encryptAES("pass"); } catch (Exception e) { e.printStackTrace(); } String name="aaa"; //soap 参数 String soapXml =""; soapXml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:otms=\"http://otms.efh.com.cn\">"; soapXml += " <soap:Header>"; soapXml += " <SoapHeader xmlns=\"http://otms.efh.com.cn/\">"; soapXml+= " <name>" + userName + "</name>"; soapXml+= " <password>" + userPass + "</password>"; soapXml+= " </SoapHeader>"; soapXml+= " </soap:Header>"; soapXml+= " <soap:Body>"; soapXml+= " <otms:" + InterfaceName + ">"; // 接口名称; if (strParams != null && strParams.length() > 0) { soapXml += " <wno>" + strParams + "</wno>"; // 参数加数据 } soapXml += " </otms:" + InterfaceName + ">"; // 接口名称; soapXml += "</soap:Body>"; soapXml += "</soap:Envelope>"; //获取http构建器对象 HttpClientBuilder builder = HttpClientBuilder.create(); CloseableHttpClient httpClient = builder.build(); //封装httppost请求对象 HttpPost httpPost = new HttpPost(url); httpPost.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build()); try { httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", ""); StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8")); httpPost.setEntity(data); //设置post请求参数实体 //执行http请求 CloseableHttpResponse response = httpClient .execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { // 打印响应内容 String retStr = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println(retStr); } // 释放资源 httpClient.close(); } catch (Exception e) { // logger.error("exception in doPostSoap1_1", e); } } GetHttpWebService("http://localhost:8888/PubWebService?wsdl","GetFactoryInfo","{\"componentType\":\"ws\"}");
其他借鉴 :https://www.cnblogs.com/furenjian/p/16145823.html
public String getOnline(String url){ int errCode=0; JSONObject resultJson=new JSONObject(); String result=""; Service service = new Service(); Call call; try { call=(Call) service.createCall(); QName opAddEntry = new QName("urn:demo", "GetOnlineInfo"); //设置命名空间和需要调用的方法名 call.setTargetEndpointAddress(url); //设置请求路径 call.setOperationName("GetNcgOnlineInfo"); //调用的方法名 call.setTimeout(Integer.valueOf(2000)); //设置请求超时 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 result= (String) call.invoke(opAddEntry,new Object[]{}); } catch (ServiceException e) { // TODO Auto-generated catch block System.out.println("查询在线状态1:"+e.getMessage()); errCode=1; } catch (RemoteException e) { // TODO Auto-generated catch block System.out.println("查询在线状态2:"+e.getMessage()); errCode=2; } resultJson.put("errCode", errCode); resultJson.put("data", result); return resultJson.toString(); }
里面注释比较全。还有些别的设置也比较简单,自己琢磨就知道了。例如编码方式、解析时间等。
说说这种方式的问题吧。我在使用的时候遇到的是:和我对接的人编写了两个WebService。但是由于这两个中有许多部分是相同的,他就把这两个合并了,同时提供了两个命名空间(具体怎么操作的我也不清楚),那么问题了,这其中有一个命名空间的所有方法我都能成功调用,但是都无法收到返回值。当时我就方了,开始还是好好的,怎么就突然不行了,于是我继续执行,查看报错消息,同时抓包查看报文内容。终于给我发现了问题。
下图是返回结果报的错,大体意识就是说我设置的命名空间和对方的命名空间不匹配。然后RPC解析就失败了。

然后我利用Wireshark抓包,得到一下结果。可以看看出,我请求的是命名空间是 ns1="urn:ncg"(其余的都是wsdl默认自带的)。可是我收到的返回报文就变了。变成了这样的 xmlns:dag="http://tempuri.org/dag.xsd" xmlns:dag="urn:dag" xmlns:ncg="urn:ncg" 足足有三个啊。RPC按照默认设置的 ns1="urn:ncg" 去解析,那肯定什么都解析不了的。所以只有自己去解析了。这种情况可以利用第三种或者第四种方式进行调用。

|
作者:chenze 出处:https://www.cnblogs.com/chenze-Index/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |

浙公网安备 33010602011771号