[随写笔记] spring boot整合cxf发布webService服务端
一、pom.xml依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>webservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webservice</name>
<description>Webservice project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- cxf依赖 -->
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.7</version>
</dependency>
<!-- cxf依赖 end spring-boot-starter-web-services-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、创建WebService接口类
package com.web.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(targetNamespace = "http://service.web.com") // 如果不添加的话,动态调用invoke的时候,会报找不到接口内的方法
public interface WebServiceDemo {
// @WebMethod 该注解用于标记方法名
// @WebParam 该注解用于标记方法入参
// @WebResult 该注解用于标记方法返回值
@WebMethod(operationName = "hello")
@WebResult(name = "String")
String hello(@WebParam(name = "code") String code, @WebParam(name = "name") String name);
}
三、创建WebService接口实现类
package com.web.service.impl;
import com.web.service.WebServiceDemo;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
@Service
@WebService(
serviceName = "WebServiceDemo" , // 对外发布的服务名,与接口名一致
targetNamespace = "http://service.web.com" , // 名称空间,一般为包名反转
endpointInterface = "com.web.service.WebServiceDemo" // 接口地址,定做SEI(Service EndPoint Interface)服务端点接口
)
public class WebServiceDemoImpl implements WebServiceDemo {
@Override
public String hello(String code , String name) {
return "hello word : " + name;
}
}
四、创建spring boot配置类
package com.config;
import com.web.service.WebServiceDemo;
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.beans.factory.annotation.Autowired;
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 {
@Autowired
private WebServiceDemo webServiceDemo;
/**
* 此方法的作用是覆盖webService服务的前缀
* 默认webService服务前缀为:services
*/
@Bean(name = "myServlet")
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/xygg/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 注册WebServiceDemoService接口到webservice服务
*/
@Bean(name = "WebServiceDemoEndpoint")
public Endpoint sweptPayEndpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), webServiceDemo);
endpoint.publish("/webservice");
return endpoint;
}
}
注:在spring boot配置中,这个方法Bean名称不能为:dispatcherServlet,需要更改其Bean名称
/**
* 此方法的作用是覆盖webService服务的前缀
* 默认webService服务前缀为:services
*/
@Bean(name = "myServlet")
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/xygg/*");
}
否则会报异常:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method errorPageCustomizer in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'dispatcherServletRegistration' in 'DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.
Process finished with exit code 1
启动项目后访问:ip:port/项目上下文/覆盖的地址/服务地址
本例访问地址:127.0.0.1/8018/xygg/webservice?wsdl
使用cxf远程调用WebService
一、创建cxf调用客户端静态工具类
package com.utlis;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
/**
* 使用cfx动态调用WebService
*/
public class CXFUtil {
// 私有化构造方法,仅提供静态工具方法
private CXFUtil(){}
/**
* @param wsdlUrl 动态WebService地址
* @param method 方法名
* @param param 入参,多个入参按顺序添如数组
* @return
*/
public static Object[] invoke(String wsdlUrl , String method , Object[] param){
// 创建实例
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
// 生成客户端
Client client = dcf.createClient(wsdlUrl);
// 创建客户端连接策略
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
// 取消块编码
httpClientPolicy.setAllowChunking(false);
// 连接超时时间,毫秒
httpClientPolicy.setConnectionTimeout(30000);
// 响应超时时间,毫秒
httpClientPolicy.setReceiveTimeout(30000);
// 设置进入客户端
HTTPConduit conduit = (HTTPConduit)client.getConduit();
conduit.setClient(httpClientPolicy);
// 执行调用
try {
return client.invoke(method, param);
} catch (Exception e) {
System.out.println("远程调用异常:" + e.getMessage());
}
return null;
}
}
二、在测试类中调用该工具方法
package com;
import com.utlis.CXFUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
//@SpringBootTest
class WebserviceApplicationTests {
@Test
void contextLoads() {
String wsUrl = "http://127.0.0.1:8018/xygg/webservice?wsdl";
String method = "hello";
Object[] param = {"","晓宇哥哥"};
Object[] invoke = CXFUtil.invoke(wsUrl, method, param);
System.out.println("ws返回:" + Arrays.toString(invoke));
}
}
调用成功