spring boot 整合cfx,axis2创建webservice客户端

两种实现:

先定义一个服务接口:

/**
 * webservice 服务接口
 * @author hkk
 */
public interface IServiceAgent {
    String invoke(Object... params) throws Exception;
}

一. cfx实现:

/**
 * WebService访问代理类:cxf
 */
@Component
@Primary
@Slf4j
public class WebServiceCFXAgent implements IServiceAgent {

    /**
     * 创建动态客户端工厂
     */
    private CisJaxWsDynamicClientFactory dynamicClientFactory;

    /**
     * 创建动态客户端
     */
    private Client client;

    /**
     * 配置文件Bean,获取webservice url
     */
    private AppConfig appConfig;

    public WebServiceCFXAgent(AppConfig appConfig) {
        this.appConfig = appConfig;
        initConfig();
    }

    private void initConfig() {
        this.dynamicClientFactory = CisJaxWsDynamicClientFactory.newInstance();

        this.client = dynamicClientFactory.createClient(appConfig.getServiceUrl());

        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();

        // 设置超时时间
        HTTPConduit conduit = (HTTPConduit) this.client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        //连接超时时间:30s
        httpClientPolicy.setConnectionTimeout(30000);
        //请求响应超时时间:60s
        httpClientPolicy.setReceiveTimeout(60000);
        conduit.setClient(httpClientPolicy);
    }

    /**
     * Webservice调用方法
     * @param methodName
     * @param params
     * @return
     * @throws Exception
     */
    public String invoke(String methodName, Object... params) throws Exception {

        Object[] objects = new Object[0];
        try {
            objects = this.client.invoke(methodName, params);
            return  objects[0].toString();
        }
        catch (Exception ex){
            log.error("上传报文:" + Arrays.toString(params));
            log.error("返回报文:" + Arrays.toString(objects));
            log.error(ex.getMessage());
            throw ex;
        }
    }

    /**
     * Webservice调用方法
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public String invoke(Object... params) throws Exception {
        return invoke(appConfig.getMethodName(), params);
    }
}

二. axis2实现

/**
 * WebService访问代理类:axis2
 */
@Component
public class WebServiceAxis2Agent implements IServiceAgent {

    private RPCServiceClient serviceClient;

    private Class[] responseParam;

    private QName requestMethod;

    /**
     * 配置文件Bean,获取webservice url
     */
    private AppConfig appConfig;

    public WebServiceAxis2Agent(AppConfig appConfig) throws AxisFault {
        this.appConfig = appConfig;
        serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();

        // 指定调用WebService的URL
        EndpointReference targetEPR = new EndpointReference(appConfig.getServiceUrl());
        options.setTo(targetEPR);

        // 指定方法返回值的数据类型的Class对象
        responseParam = new Class[] { String.class };
        // 指定要调用的getGreeting方法及WSDL文件的命名空间
        requestMethod = new QName(appConfig.getNamespaceUri(), appConfig.getMethodName());
    }
    @Override
    public String invoke(Object... params) {

        String result = null;
        try {
            // 调用方法并输出该方法的返回值
            result = "" + serviceClient.invokeBlocking(requestMethod, params, responseParam)[0];

        } catch (AxisFault e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

以上的两种方法调用方法都是一样:通过接口注入,注意@Primary,将优先使用

    @Autowired
    private IServiceAgent webAgent;

    @RequestMapping("/test")
    public String webServiceTest() throws Exception {

        Object[] objs = {"2343", "435", "567", "678", "789", "789"};

        String result = webAgent.invoke(objs);

        return result;
    }

这个调用invoke方法其实不太友好,所以

/**
 * WebService访问代理类:cxf
 * @author hkk
 */
@Configuration
@Slf4j
public class WebServiceCFXConfiguration {

    /**
     * 配置文件Bean,获取webservice url
     */
    private AppConfig appConfig;

    public WebServiceCFXConfiguration(AppConfig appConfig) {
        this.appConfig = appConfig;
    }

    @Bean
    public Mhs5service mhs5service() {

        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setServiceClass(Mhs5service.class);
        factoryBean.setAddress(appConfig.getServiceUrl());

        Mhs5service mhs5service = (Mhs5service) factoryBean.create();

        // 设置超时时间
//        HTTPConduit conduit = (HTTPConduit) this.client.getConduit();
//        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
//        //连接超时时间:30s
//        httpClientPolicy.setConnectionTimeout(30000);
//        //请求响应超时时间:60s
//        httpClientPolicy.setReceiveTimeout(60000);
//        conduit.setClient(httpClientPolicy);
        return mhs5service;
    }

}

接口类定义:

@WebService(targetNamespace = "http://service.server.hkk.com/")
public interface Mhs5service {
 
    @WebMethod(operationName="invokeCotton")
    String invokeCotton(String a, String b, String c,
                               String d, String e, String f);
}

注意要加:targetNamespace 

这个调用就友好多了

    @Autowired
    private Mhs5service mhs5service;    

    @RequestMapping("/mhs5service")
    public String mhs5service() throws Exception {

        String result = mhs5service.invokeCotton("2343", "435", "567", "678", "789", "789");

        return result;
    }

POM文件:

<properties>
        <java.version>1.8</java.version>
        <org.apache.axis2.version>1.7.8</org.apache.axis2.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <ojdbc8.version>12.2.0.1</ojdbc8.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-typehandlers-jsr310</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!--配置oracle数据库,从maven仓库中获取-->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>${ojdbc8.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.7</version>
        </dependency>

        <!--axis2 begin-->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-spring</artifactId>
            <version>${org.apache.axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>${org.apache.axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>${org.apache.axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-xmlbeans</artifactId>
            <version>${org.apache.axis2.version}</version>
        </dependency>
        <!--axis2 end-->
    </dependencies>
原文地址:https://www.cnblogs.com/hankuikui/p/14276951.html