1. springboot集成cxf
#只使用第一个应该可以,具体没测试
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.3</version>
</dependency>
2. 创建cxf工具类
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.springframework.stereotype.Service;
import javax.xml.namespace.QName;
@Service
public class CXFUtils {
/**
*
* @param url webservice接口地址
* @param operation 方法名
* @param parameters 参数
* @return 接口返回值
*/
public static Object[] invokeRemoteMethod(String url, String operation, Object[] parameters) {
Object[] res = null;
try {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
if (!url.endsWith("wsdl")) {
url += "?wsdl";
}
org.apache.cxf.endpoint.Client client = dcf.createClient(url);
//处理webService接口和实现类namespace不同的情况,CXF动态客户端在处理此问题时,会报No operation was found with the name的异常
Endpoint endpoint = client.getEndpoint();
QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), operation);
BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
if (bindingInfo.getOperation(opName) == null) {
for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {
if (operation.equals(operationInfo.getName().getLocalPart())) {
opName = operationInfo.getName();
break;
}
}
}
res = client.invoke(opName, parameters);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
3. 踩坑记录
1、 idea编辑器中测试正常,jar包运行时调用webservice提示 Unable to create schema compiler
解决办法:
指定jre启动,为避免引起其他项目出错,复制jdk目录下jre包到jar包目录中。
复制jdk下lib中tools.jar到jre的lib和lib\ext文件内(网上有人说只复制到lib包下即可,实测不行)
指定jre启动jar包
start jre1.8.0/bin/java -jar -Dspring.config.location=application.yml -Dfile.encoding=utf-8 xxx.jar