最近工作中需要用到webservice调用其他服务,没接触过这个,研究了几天,做个记录
1.eclipse生成webservice客户端:打开eclipse, File >> New >> Other
![](https://img-blog.csdnimg.cn/20200718160722439.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pob3V5aW5nZ2UxMTA0,size_16,color_FFFFFF,t_70)
2. Web Services >> Web Service Client
![](https://img-blog.csdnimg.cn/20200718160759714.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pob3V5aW5nZ2UxMTA0,size_16,color_FFFFFF,t_70)
3. 输入webservice地址即可。
![](https://img-blog.csdnimg.cn/20200718160934636.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pob3V5aW5nZ2UxMTA0,size_16,color_FFFFFF,t_70)
4. 自动生成的代码如下:
![](https://img-blog.csdnimg.cn/20200718161013503.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pob3V5aW5nZ2UxMTA0,size_16,color_FFFFFF,t_70)
5.由于使用idea开发,把eclipse生成的代码考到idea自己创建的包下面,把文件中写死的包路径改成自己的包路径
![](https://img-blog.csdnimg.cn/f52166dce54241eab7904ee216e36660.png)
6,组织数据,编写代码
6.1通过eclipse生成的文件调用
AIPWebServiceImplServiceLocator serviceLocator = new AIPWebServiceImplServiceLocator();
serviceLocator.setAIPWebServiceImplPortEndpointAddress(endpoint1);
AIPWebServiceImplServiceSoapBindingStub aip = (AIPWebServiceImplServiceSoapBindingStub) serviceLocator.getAIPWebServiceImplPort();
//这里是调用方法的地方,里面是请求服务端的入参
claimPublicityResponseDto = aip.uploadClaimPublicity(clientInfo, claimPublicityDtoArray);
MessageContext messageContext = aip._getCall().getMessageContext();
//请求报文
String requestStr = messageContext.getRequestMessage().getSOAPPartAsString();
//响应报文
String responseStr = messageContext.getResponseMessage().getSOAPPartAsString();
System.out.println("请求报文:");
System.out.println(requestStr);
System.out.println("++++++++++++++++++++++");
System.out.println("响应报文:");
System.out.println(responseStr);
System.out.println(claimPublicityResponseDto.getErrorCode() + claimPublicityResponseDto.getErrorDesc());
6.2 通过axis调用,要求生成的实体必须有序列化的方法不然会报没有序列化的错误
![](https://img-blog.csdnimg.cn/98841d097d8a46c59031b26570ac00c6.png)
String encodingStyle = "UTF-8";
//WSDL的地址
String endpoint = "";
String endpoint1 = "";
// 命名空间,在WSDL中对应的标签是:
// <wsdl:definitions xmlns:soap="" ...
// targetNamespace="****">
String targetNamespace = "****";
// 具体方法的调用URI,在WSDL中对应的标签是:
// <soap:operation soapAction="" style="document"/>
String soapActionURI = "";
// 具体调用的方法名,在WSDL中对应的标签是:
// <wsdl:operation name="getCharFonts">
String method = "";
//调用接口的参数的名字
//String[] paramNames = {"byFontsLength"};
//调用接口的参数的值
//Integer[] paramValues = {1};
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
Call call = (Call) service.createCall();
// call.setTimeout(new Integer(20000)); //设置超时时间
call.setSOAPActionURI(soapActionURI);
call.setTargetEndpointAddress(new java.net.URL(endpoint)); //设置目标接口的地址
call.setEncodingStyle(encodingStyle);//设置传入服务端的字符集格式如utf-8等
call.setOperationName(new QName(targetNamespace,method));// 具体调用的方法名,可以由接口提供方告诉你,也可以自己从WSDL中找
call.setUseSOAPAction(true);
//入参
call.addParameter(new QName("","clientInfo"),
XMLType.XSD_ANYTYPE,//任意类型
javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.addParameter(new QName("","claimPublicityDtoList"),
XMLType.XSD_ANYTYPE,
javax.xml.rpc.ParameterMode.IN);// 接口的参数
// call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型 ,如String
call.setReturnClass(ClaimPublicityResponseDto.class); //返回字符串数组类型
// 给方法传递参数,并且调用方法 ,如果无参,则new Obe
claimPublicityResponseDto = (ClaimPublicityResponseDto) call.invoke(new Object[]{clientInfo, claimPublicityDtoList});
MessageContext messageContext = call.getMessageContext();
Message requestMessage = messageContext.getRequestMessage();
LOGGER.info(requestMessage.getSOAPPartAsString());
我这还有两个问题不清楚,一是请求报文中时间的时区问题,一直少8个小时,还有一个中文字符是乱码的问题,但是在反参的dto里面是正常的,只有在报文中是错误的,个人猜测可能是soap协议的问题,有大佬看到可以帮忙解答一下。