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(); }