Java使用AXIS调用webService接口
通用方法-直接上代码
/**
* @param methodname 调用的方法名
* @param o 参数
* @return String
*/
private String getCallResult(String methodname, Object[] o) {
String serverpath ="http://localhost:8080/platform-service/settlementServiceImpl?wsdl"
Service service = new Service();
try {
// 通过Service实例创建Call的实例
Call call = (Call) service.createCall();
// 将WebService的服务路径加入到call实例之中
call.setTargetEndpointAddress(serverpath);
Object a = call.invoke(methodname, o);
if (a != null) {
return a.toString();
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("调用接口失败", e);
}
return null;
}
如果返回值是自定义的bean,在使用通用方法会报错:org.xml.sax.SAXException: Deserializing parameter ‘getAccountInfomantionReturn’: could not find deserializer for type {urn:BeanService}CardUserDTO
无法找到类型的反序列化器,这时需要我们设置返回的参数类型
自定义返回实体类
public CardUserDTO getBalance(String[] paramBalance) {
try {
String serverpath = "http://localhost:8080/platform-service/settlementServiceImpl?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(serverpath);
//设置出参,参数1:参数的命名空间,在http://localhost:8080/platform-service/settlementServiceImpl?wsdl中找到你锁调用的方法的xxxxxRespon,参数2:返回的实体类
QName outType = new QName("http://webService.wst.synjones.com", "CardUserDTO");
call.registerTypeMapping(CardUserDTO.class, outType, BeanSerializerFactory.class,
BeanDeserializerFactory.class);
// 调用的服务接口方法名
call.setOperationName("getAccountInfomantion");
// 传递的参数名,参数类型,入参
call.addParameter("account", XMLType.XSD_STRING, ParameterMode.IN);
// 设置服务方法返回类型实体类
call.setReturnClass(CardUserDTO.class);
//call.setReturnType(XMLType.XSD_STRING);
// Long[] paramBalance = new Long[] { Long.valueOf("1759") };
String[] paramBalance = new String[] { "1759" };
CardUserDTO aa = (CardUserDTO) call.invoke("getAccountInfomantion", paramBalance);
System.out.println(aa.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
当然不能少了包
maven引入:
<!-- WSDL -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-wsdl4j</artifactId>
<version>1.5.1</version>
</dependency>
遇到的问题,记录下来,防止再次遇到后的茫然…ლ(´ڡ`ლ)