1.使用rpc方式解析远程wsdl:
优点在于:代码简单、不用繁琐的生成服务端的一大堆代码
缺点在于:需要知道要解析的wsdl的服务端部分的实体类的定义
2.使用的包的maven依赖:
<!--==============axis2 client===============-->
<!--http://blog.csdn.net/liwf_/article/details/9788789-->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>${axis2.version}</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>${axis2.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>${axis2.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>${axis2.version}</version>
<exclusions>
<!-- We want to choose the JavaMail implementation ourselves -->
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--http://dominikdorn.com/2010/05/maven-junit-classformaterror-absent-code-attribute/-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
<scope>provided</scope>
</dependency>
3.工具类代码:
public class CallWSDLByAxis2 {
/**
* RPC调用AXIS2 webservice
*
* @param endpoint 服务地址
* 如:http://192.168.0.1:2597/aixs2/services/jqservice?wsdl
* @param localPart 方法名 如<xs:element name="Receive">
* @param opArgs 方法参数 如Object[] opArgs = new Object[] { param };
* @param namespaceURI 命名空间 如 :targetNamespace="http://server.test.com.cn">
* @param opReturnType 返回类型 如字符串:Class[] opReturnType = new Class[] {
* String[].class };
* @param timeoutSeconds
* @param username
* @param password
* @return
*/
public static Object axis2RPCInvoke(String endpoint, String localPart, Object[] opArgs, String namespaceURI, Class[] opReturnType, int timeoutSeconds, String username, String password) {
Object[] ret = null;
try {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
Authenticator authenticator = new Authenticator();
List<String> auth = new ArrayList<String>();
auth.add(Authenticator.BASIC);
authenticator.setAuthSchemes(auth);
authenticator.setUsername(username);
authenticator.setPassword(password);
authenticator.setPreemptiveAuthentication(true);
options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);
options.setTimeOutInMilliSeconds(timeoutSeconds * 1000);
EndpointReference targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
QName opQName = new QName(namespaceURI, localPart);
ret = serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
serviceClient.cleanupTransport();
} catch (Exception e) {
e.printStackTrace();
}
return ret[0];
}
/**
* 没有返回值的请求
* @param endpoint
* @param localPart
* @param opArgs
* @param namespaceURI
* @param timeoutSeconds
* @param username
* @param password
*/
public static void axis2RPCInvoke(String endpoint, String localPart, Object[] opArgs, String namespaceURI, int timeoutSeconds, String username, String password) {
try {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
Authenticator authenticator = new Authenticator();
List<String> auth = new ArrayList<String>();
auth.add(Authenticator.BASIC);
authenticator.setAuthSchemes(auth);
authenticator.setUsername(username);
authenticator.setPassword(password);
authenticator.setPreemptiveAuthentication(true);
options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);
options.setProperty(HTTPConstants.CHUNKED, false);
options.setProperty(Constants.Configuration.MESSAGE_TYPE, HTTPConstants.MEDIA_TYPE_APPLICATION_ECHO_XML);
options.setProperty(Constants.Configuration.DISABLE_SOAP_ACTION, Boolean.TRUE);
options.setTimeOutInMilliSeconds(timeoutSeconds * 1000);
EndpointReference targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
QName opQName = new QName(namespaceURI, localPart);
serviceClient.invokeRobust(opQName, opArgs);
serviceClient.cleanupTransport();
} catch (Exception e) {
e.printStackTrace();
}
}
}