WebService Client端
pom
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.5.0</version> <exclusions> <exclusion> <artifactId>asm</artifactId> <groupId>org.ow2.asm</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>javax.xml</groupId> <artifactId>jaxb-impl</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-xjc</artifactId> <version>2.3.0</version> </dependency>
java是核心包,javax的x是extension的意思,也就是扩展包。
client
package com.test; import org.apache.cxf.endpoint.Client; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class Test { public static void main(String[] args) throws Exception { JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); Client client = factory.createClient("http://localhost:8000/order/api?wsdl"); //打印发出的消息 client.getInInterceptors().add(new LoggingInInterceptor()); //打印进入的消息 client.getOutInterceptors().add(new LoggingOutInterceptor()); Object[] res = client.invoke("yxxczyNoticeWorkOrder", "test"); System.out.println("Echo response: " + res[0]); } }
如果时https请求,cxf需要加
//https //System.setProperty("javax.net.ssl.trustStore","D:\\develop\\ssl\\xy.keystore"); System.setProperty("javax.net.ssl.trustStore","/opt/xy.keystore"); System.setProperty("javax.net.ssl.trustStorePassword","123456"); System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol"); java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
cxf不支持jdk11, 目前只支持jdk1.8, 官网有说明
使用hutool更方便
/**
* webservice
*
* apache-cxf框架的两种调用方式:
* 1.静态调用 org.apache.cxf.jaxws.JaxWsProxyFactoryBean 需要依赖服务端接口
* 2.动态调用 org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory 无需服务端接口
*
* webservice spring boot框架的调用方式 :WebServiceTemplate
* https://docs.spring.io/spring-boot/docs/current/reference/html/io.html#io.webservices
*
* hutool调用方式
* https://hutool.cn/docs/#/http/WebService/Soap%E5%AE%A2%E6%88%B7%E7%AB%AF-SoapClient
*/