今天需要用到与其他系统对接接口,提供的是webservice的接口,写了一个客户端,特记录一下。
1.引入需要的jar包
<!-- 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/org.apache.axis/axis-jaxrpc -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/wsdl4j.wso2/wsdl4j -->
<dependency>
<groupId>wsdl4j.wso2</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2.wso2v4</version>
</dependency>
2.java客户端实现
final String Url = "http://ip:port/WebService.asmx";
final String namespace = "http://xxx.org/";
final String method = "xxx";
final String paraName = "xxx";
final String paraValue = "xxx";
public void doService() {
try {
Service service = new Service();
Call call = (Call) service.createCall();
//访问路径
call.setTargetEndpointAddress(Url);
//访问的方法名称
call.setOperationName(new QName(namespace,method));
//访问参数
call.addParameter(new QName(namespace,paraName), XMLType.XSD_STRING, ParameterMode.IN);
//使用SOAP方式请求
call.setUseSOAPAction(true);
//设置请求的路径
call.setSOAPActionURI(namespace + method);
//设置返回结果类型
call.setReturnType(XMLType.XSD_STRING);
//开始执行,并获取结果
Object obj = call.invoke(new Object[] {paraValue});
} catch (Exception e) {
e.printStackTrace();
}
}