使用 Axis2 调用 Java WebService 接口

应用场景:
SpringBoot 项目(IntelliJ IDEA)

第1步:在pom文件中引入jar包


<dependency>
    <groupId>org.apache.axis2groupId>
    <artifactId>axis2artifactId>
    <version>1.6.2version>
dependency>
<dependency>
    <groupId>org.apache.ws.commons.axiomgroupId>
    <artifactId>axiomartifactId>
    <version>1.2.20version>
    <type>pomtype>
dependency>
<dependency>
    <groupId>org.apache.ws.commons.axiomgroupId>
    <artifactId>axiom-apiartifactId>
    <version>1.2.20version>
dependency>
<dependency>
    <groupId>org.apache.ws.commons.axiomgroupId>
    <artifactId>axiom-implartifactId>
    <version>1.2.20version>
dependency>
<dependency>
    <groupId>wsdl4jgroupId>
    <artifactId>wsdl4jartifactId>
    <version>1.6.3version>
dependency>
<dependency>
    <groupId>org.apache.ws.xmlschemagroupId>
    <artifactId>xmlschema-coreartifactId>
    <version>2.2.3version>
dependency>
<dependency>
    <groupId>org.apache.neethigroupId>
    <artifactId>neethiartifactId>
    
    <version>3.1.1version>
dependency>
<dependency>
    <groupId>org.apache.axis2groupId>
    <artifactId>axis2-transport-localartifactId>
    <version>1.7.7version>
dependency>
<dependency>
    <groupId>org.apache.axis2groupId>
    <artifactId>axis2-transport-httpartifactId>
    <version>1.7.7version>
dependency>

第2步:通过SoapUI获取 targetNamespace

使用 Axis2 调用 Java WebService 接口_第1张图片

第3步:调用接口方法

public static final String targetEndpoint = "http://*.*.*.*:8088/wxux?wsdl";
public static final String targetNamespace = "http://*.*.*.com";
public static final String method = "service";

public static String send(String paramData) {
    logger.info("WebService发送请求......");
    try {
        RPCServiceClient client = new RPCServiceClient();
        Options options = client.getOptions();
        options.setTo(new EndpointReference(targetEndpoint));
        options.setTimeOutInMilliSeconds(1000 * 60 * 5);// 毫秒单位
        options.setAction(method);
        Object[] response = client.invokeBlocking(new QName(targetNamespace, method), new Object[]{paramData}, new Class[]{String.class});
        String results = (String) response[0];
        logger.info("WebService请求返回结果: \r\n{}", Xmls.format(results));
        return results;
    } catch (Exception e) {
        logger.error("WebService请求异常, Cause:{}, Message:{}", e.getCause(), e.getMessage());
        e.printStackTrace();
        return null;
    }
}

你可能感兴趣的:(Axis2)