webservice有提供wsdl跟asmx两种方式,两种方式的调用方法不同
1、wsdl的方式
添加依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.9</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.9</version>
</dependency>
请求代码:
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
Client client = clientFactory.createClient(webserviceAddress);
Object[] result = null;
try {
//如果有命名空间的话
QName operationName = new QName(namespaceURI,localPort); //如果有命名空间需要加上这个,第一个参数为命名空间名称,第二个参数为WebService方法名称
result = client.invoke(operationName,param1, param2);//后面为WebService请求参数数组
//如果没有命名空间的话
result = client.invoke(operationName, param1); //注意第一个参数是字符串类型,表示WebService方法名称,第二个参数为请求参数
} catch (Exception e) {
String errMsg = "WebService发生异常!";
result = new Object[] { errMsg };
logger.error(errMsg, e);
}
参考地址:https://blog.csdn.net/lichuangcsdn/article/details/83053327
2、asmx的方式
maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.rpc/javax.xml.rpc-api -->
<dependency>
<groupId>javax.xml.rpc</groupId>
<artifactId>javax.xml.rpc-api</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
Java代码:
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**调用webservice.asmx接口
*
* @param method 需要调用的方法
* @param json 传递的参数
*/
public static void callWebserviceASMX(String method,String json){
//获取webservice接口地址
String url = "http://xxxx/xx/Webservice.asmx";
//获取域名地址,server定义的
String soapaction = "http://tempuri.org/";
Service service = new Service();
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
//设置要调用的方法
call.setOperationName(new QName(soapaction,method));
//设置要返回的数据类型
call.setReturnType(new QName(soapaction,method), String.class);
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction+method);
//调用方法并传递参数
String result = (String) call.invoke(new Object[]{json});
System.out.println("result is:::"+result);
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
You must call addParameter() for all parameters if you have called setReturnType():
----
// 设置参数名称,具体参照从浏览器中看到的
call.addParameter(new QName(soapaction, method),
XMLType.XSD_STRING, ParameterMode.IN); //设置请求参数及类型
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
----设置parameter处的参数名不对
// 设置参数名称,具体参照从浏览器中看到的
call.addParameter(new QName(soapaction, "model"),
XMLType.XSD_STRING, ParameterMode.IN); //设置请求参数及类型
自己调试后的代码:
//获取webservice接口地址
String url = "http://101.1.1.1:8999/DataPlatform/66KC/Transfer.asmx";
//获取域名地址,server定义的
String soapaction = "http://tempuri.org/";
String method = "ReceiveDelivey" ;
Service service = new Service();
ShengaoPushTransportInfo info = new ShengaoPushTransportInfo();
info.setEntrustId(6572678081465549845L);
String request = gson.toJson(info);
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction+method);
//设置要调用的方法
call.setOperationName(new QName(soapaction,method));
// 设置参数名称,具体参照从浏览器中看到的
call.addParameter(new QName(soapaction, "request"),
XMLType.XSD_STRING, ParameterMode.IN); //设置请求参数及类型
//设置要返回的数据类型
call.setReturnType(new QName(soapaction,method), String.class);
//调用方法并传递参数
String result = (String) call.invoke(new QName(soapaction,method),new Object[]{request});
System.out.println("result is:::"+result);
}catch (ServiceException se){
se.printStackTrace();
}catch (RemoteException re) {
re.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
WebService几个常见的异常 :https://blog.csdn.net/chenjunan888/article/details/8753184
参考地址:https://www.cnblogs.com/zhoulian/p/9323908.html
3、httpOK,http模式调用
public static void main(String args[]){
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("text/xml;charset=utf-8");
RequestBody body = RequestBody.create(mediaType,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<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/\">\n <soap:Body>\n <ReceiveDelivey xmlns=\"http://tempuri.org/\">\n <model>\n <entrustId>6572678081465549845</entrustId>\n <entrustNo>WTD20190829000012</entrustNo>\n <consigNo>EMXJD2019082900001</consigNo>\n <transportId>6572678592684098581</transportId>\n <transportNo>YSD20190829000009</transportNo>\n <batchNo>1112233</batchNo>\n <weightGet> 15.000</weightGet>\n <timeGet>2019-08-29 11:19:40</timeGet>\n <status>Y003</status>\n </model>\n </ReceiveDelivey>\n </soap:Body>\n</soap:Envelope>");
Request request = new Request.Builder()
.url("http://101.1.1.1:8999/DataPlatform/66KC/Transfer.asmx")
.method("POST", body)
.addHeader("Content-Type", "text/xml;charset=utf-8")
.build();
try {
Response response = client.newCall(request).execute();
System.out.println(response);
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
麻烦点是拼接参数麻烦