记一次使用cxf实现调用webservice

1、添加pom.xml

<properties>
   <cxf.version>3.0.0</cxf.version>
</properties>
<dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-frontend-jaxws</artifactId>
   <version>${cxf.version}</version>
</dependency>

<dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-transports-http</artifactId>
   <version>${cxf.version}</version>
</dependency>

 

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-codegen-plugin</artifactId>
   <version>3.0.16</version>
   <executions>
      <execution>
         <id>generate-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <!-- 生成代理类的目录 -->
            <sourceRoot>src/main/java</sourceRoot>
            <wsdlOptions>
               <wsdlOption>
                  <!-- wsdl地址 -->
                  <wsdl></wsdl>
                  <!-- 指定代理类包名 -->
                  <packagenames>
                     <packagename>com.*****.generated</packagename>
                  </packagenames>
                  <!-- 是否生成soapheader -->
                  <extendedSoapHeaders>true</extendedSoapHeaders>
               </wsdlOption>
            </wsdlOptions>
         </configuration>
         <goals>
            <goal>wsdl2java</goal>
         </goals>
      </execution>
   </executions>
</plugin>

2、关键代码

 

public <T> BaseBackResponseBody<T> exchange(String url, String serviceName, BaseBackRequestBody<?> requestEntity, Class<?> responseType) throws ResourceAccessException {
    RequestLog log = new RequestLog();
    BaseBackResponseBody<T> responseEntity = null;
    try {
        String resultJson = callWebservice(url, serviceName, JSONUtil.objToJson(requestEntity));
        responseEntity = (BaseBackResponseBody)JSONUtil.jsonToObj(resultJson, responseType);
        this.saveLog(log, url + "?op=" + serviceName, requestEntity, responseEntity);
    } catch (ResourceAccessException e) {
        log.setErrorMsg(ExceptionUtil.getStackTrace(e));
        this.saveLog(log, url + "?op=" + serviceName, requestEntity, responseEntity);
        logger.error("call exchange Exception: {}", ExceptionUtil.getStackTrace(e));
        throw e;
    }

    return responseEntity;
}
private String callWebservice(String url, String serviceName, String jsonPara) {
    AGVInterfaceServiceSoap myWebService = getInterface(url);
    Class<?> serviceSoapClass = AGVInterfaceServiceSoap.class;

    try {
        Method method = serviceSoapClass.getMethod(serviceName, String.class);
        String resultJson = (String) method.invoke(myWebService, jsonPara);
        logger.debug("resultJson = " + resultJson);

        return resultJson;
    } catch (Exception e) {
        logger.error("call callWebservice Exception: {}", ExceptionUtil.getStackTrace(e));
    }

    return null;
}

 

public AGVInterfaceServiceSoap getInterface(String url) {
    JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
    factoryBean.setServiceClass(AGVInterfaceServiceSoap.class);
    factoryBean.setAddress(url);

    // 请求报文拦截器
    factoryBean.getInInterceptors().add(new LoggingInInterceptor());
    // 响应报文拦截器
    factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
    return (AGVInterfaceServiceSoap) factoryBean.create();
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值