Java JDK 1.8、Spring boot 2.1.6、Apache CXF 3.1.6
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- <version>2.1.6version>
- dependency>
- <dependency>
- <groupId>org.apache.cxfgroupId>
- <artifactId>cxf-rt-frontend-jaxwsartifactId>
- <version>3.1.6version>
- dependency>
- <dependency>
- <groupId>org.apache.cxfgroupId>
- <artifactId>cxf-rt-transports-httpartifactId>
- <version>3.1.6version>
- dependency>
- dependencies>
使用@WebService声明式注解声明这是一个webService接口,并设置:
注解@WebMethod是声明接口方法,可以通过operationName为接口方法设置别名,默认是方法名。
此外他还有一个参数exclude,为true时则忽略该方法作为webService接口方法,默认为false。
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebResult;
- import javax.jws.WebService;
-
- @WebService(name = "TestService",
- targetNamespace = "http://server.dandelion.com")
- public interface TestService {
- @WebMethod(operationName="getData")
- @WebResult(targetNamespace = "http://server.dandelion.com")
- String execute(@WebParam(name = "action") String action, @WebParam(name = "msg") String msg);
- }
创建了webService接口TestService,其中方法execute有两个参数分别为action和msg,用注解@WebParam进行声明。
创建接口实现类TestServiceImpl实现TestService接口,其中endpointInterface为webService接口的路径。
- import javax.jws.WebService;
-
- @WebService(serviceName = "TestService"
- targetNamespace = "http://server.dandelion.com",
- endpointInterface = "com.dandelion.server.TestService")
- public class TestServiceImpl implements TestService{
-
- @Override
- public String execute(String action, String msg) {
- System.out.println(String.format("action: %s", action));
- System.out.println(String.format("msg: %s", msg));
- return "
1同步成功 "; - }
- }
创建webService的配置类WebServiceConfig,将接口服务注入容器并进行发布。
- import com.dandelion.server.TestServiceImpl;
- import org.apache.cxf.Bus;
- import org.apache.cxf.bus.spring.SpringBus;
- import org.apache.cxf.jaxws.EndpointImpl;
- import org.apache.cxf.transport.servlet.CXFServlet;
- import org.springframework.boot.web.servlet.ServletRegistrationBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import javax.xml.ws.Endpoint;
-
- @Configuration
- public class WebServiceConfig {
-
- @Bean
- public ServletRegistrationBean
disServlet() { - ServletRegistrationBean
servletRegistrationBean = new ServletRegistrationBean<>(); - servletRegistrationBean.setServlet(new CXFServlet());
- servletRegistrationBean.addUrlMappings("/webService/*");
- return servletRegistrationBean;
- }
-
- @Bean(name = Bus.DEFAULT_BUS_ID)
- public SpringBus springBus() {
- return new SpringBus();
- }
-
- @Bean
- public Endpoint endpoint(SpringBus springBus) {
- EndpointImpl endpoint = new EndpointImpl(springBus, new TestServiceImpl());
- endpoint.publish("/testService");
- return endpoint;
- }
- }
项目启动效果:

配置完成后启动项目,访问http://localhost:服务端口/webService,如下图所示说明创建webService服务成功。

WSDL文件:访问http://localhost:服务端口/webService/testService?wsdl


- import org.apache.cxf.endpoint.Client;
- import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
-
- public class WsClientUtil {
-
- /**
- * 调用webservice服务
- * @param wsdUrl 服务地址
- * @param operationName 方法名称
- * @param params 参数
- * @return 服务响应结果
- */
- public static String callWebService(String wsdUrl, String operationName, Object... params){
- JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
- Client client = dcf.createClient(wsdUrl);
- try {
- Object[] objects = client.invoke(operationName, params);
- return objects[0].toString();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- }
API测试
- @RestController
- @RequestMapping(value = "/client")
- public class TestClientController {
-
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-
- @RequestMapping(value = "test")
- public String test(){
- //在获取连接之前 还原上下文
- Thread.currentThread().setContextClassLoader(classLoader);
- // 服务地址
- String url = "http://localhost:9010/webService/testService?wsdl";
- // 方法名称
- String methodName = "getData";
- // 参数1
- String action = "同步用户信息";
- // 参数2
- String msg = "
10012334223 蒲公英不是梦 1 "; - try {
- // 发起请求
- return WsClientUtil.callWebService(url, methodName, action, msg);
- } catch (Exception e) {
- return String.format("webService调用异常:%s", e.getMessage());
- }
- }
- }
效果