服务端pom引入
<!-- 引入web service --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></dependency><!-- cxf 依赖 --> <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.3.4</version></dependency>
服务端编写需要发布的接口
UserEntity
@Datapublic class UserEntity implements Serializable {private static final long serialVersionUID = 1L;private Long userId;private String username;private String mobile;@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)private String password;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date createTime;}
接口
@WebService // 标明是个webservice服务,发布的时候会带上这个类public interface UserWebService {@WebMethod // @WebMethod发布的方法,@WebParam:对参数的别名UserEntity getUserById(@WebParam(name = "id") long id);@WebMethodList<UserEntity> getAllUser();}
接口实现类
@WebService(serviceName = "cfxUserWebService", // 对外发布的服务名称targetNamespace = "http://cxf./") // targetNamespace指定名称空间,接口/实现类/调用方要保持一致,注意以/结尾@Componentpublic class UserWebServiceImpl implements UserWebService {@WebMethod@Overridepublic UserEntity getUserById(@WebParam(name = "id") long id) {UserEntity user1 = new UserEntity();user1.setUserId(1L);user1.setUsername("Tom");UserEntity user2 = new UserEntity();user2.setUserId(2L);user2.setUsername("Marry");UserEntity user3 = new UserEntity();user3.setUserId(3L);user3.setUsername("Lucy");List<UserEntity> userList = new ArrayList<UserEntity>();userList.add(user1);userList.add(user2);userList.add(user3);for (UserEntity entity : userList) {// 判断一下用户输入的id是否可以匹配上某个User的idif (entity.getUserId() == id) {return entity;}}// 未找到直接返回一个错误消息return null;}@WebMethod@Overridepublic List<UserEntity> getAllUser() {UserEntity user1 = new UserEntity();user1.setUserId(1L);user1.setUsername("Tom");UserEntity user2 = new UserEntity();user2.setUserId(2L);user2.setUsername("Marry");UserEntity user3 = new UserEntity();user3.setUserId(3L);user3.setUsername("Lucy");List<UserEntity> userList = new ArrayList<UserEntity>();userList.add(user1);userList.add(user2);userList.add(user3);return userList;}}
配置类
@Configurationpublic class CxfConfig {/*** 1.自定义注册一个Servlet<br/>* 如果不自定义注册,则默认的匹配url是services,详见下方摘录的官文描述<br/>* 2.也可以在properties或yml文件中修改该路径,如cxf.path=/webservice,详见下方摘录的官文描述* 注意:beanName不能为dispatcherServlet* @return*/@Bean(name = "cxfServlet")public ServletRegistrationBean cxfServlet() {return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");}@Autowiredprivate Bus bus;@Autowiredprivate UserWebService userWebService;// 配置CXF服务发布,默认服务是在host:port/services/发布地址// 这里访问地址 http://localhost:8089/renren-api/webservice/userWebService?wsdl@Beanpublic Endpoint userWebService() {EndpointImpl endpoint = new EndpointImpl(bus, userWebService);endpoint.publish("/userWebService");return endpoint;}}
查看服务wsdl文件
点击WSDL后的连接看服务文件描述
http://localhost:8089/renren-api/webservice/userWebService?wsdl
WSDL简述
<wsdl:binding> 元素有两个属性 - name 属性和 type 属性。name 属性定义 binding 的名称,
type 属性指向用于 binding 的端口,在这个例子中是 "glossaryTerms" 端口。
<soap:binding> 元素有两个属性 - style 属性和 transport 属性。
style 属性可取值 "rpc" 或 "document"。在这个例子中我们使用 document。
transport 属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP通信,SOAP=HTTP+XML。
<wsdl:operation> 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义。同时您必须如何对输入和输出进行编码。
客户端pom引入
<!-- cxf 依赖 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.3.4</version></dependency>
编写客户端调用
@RunWith(SpringRunner.class)@SpringBootTestpublic class WebServiceTest {/*** 方法1:动态客户端调用*/@Testpublic void DynamicClient() {JaxWsDynamicClientFactory jwdcf = JaxWsDynamicClientFactory.newInstance();Client client = jwdcf.createClient("http://localhost:8089/renren-api/webservice/userWebService?wsdl");Object obj = null;try {obj = client.invoke("getUserById", 1);if (obj instanceof UserEntity) {System.out.println(((UserEntity) obj).toString());}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/***方法2:代理工厂*/@Testpublic void ProxyFactory() {String address = "http://localhost:8089/renren-api/webservice/userWebService?wsdl";// 代理工厂JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();// 设置代理地址factoryBean.setAddress(address);// 设置接口类型factoryBean.setServiceClass(UserWebService.class);// 创建一个代理接口实现UserWebService userWebService = (UserWebService) factoryBean.create();UserEntity userEntity = userWebService.getUserById(1);System.out.println(userEntity.toString());List<UserEntity> userList = userWebService.getAllUser();for (UserEntity entity : userList) {System.out.println(entity.toString());}}/*** 方式3.直接根据代理工厂将服务接口做成bean,是对方式2的优化* 这里需要添加一个Configuration类来定义bean*/@Testpublic void UserWebServiceProxy() {UserEntity userEntity = userWebService.getUserById(1);System.out.println(userEntity.toString());List<UserEntity> userList = userWebService.getAllUser();for (UserEntity entity : userList) {System.out.println(entity.toString());}}}
方法3的配置类
@Configurationpublic class ServiceConfig {@Bean(name = "userWebService")UserWebService userWebService() {String address = "http://localhost:8089/renren-api/webservice/userWebService?wsdl";// 代理工厂JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();// 设置代理地址factoryBean.setAddress(address);// 设置接口类型factoryBean.setServiceClass(UserWebService.class);// 创建一个代理接口实现UserWebService userWebService = (UserWebService) factoryBean.create();return userWebService;}}
CXF JAX-WS官文描述
Spring Boot CXF JAX-WS Starter
Features
Registers CXFServlet with a "/services/*" URL pattern for serving CXF JAX-WS endpoints.
Setup
JAX-WS Starter
Additional Configuration
Use "cxf.path" property to customize a CXFServlet URL pattern
Use "cxf.servlet.init" map property to customize CXFServlet properties such as "services-list-path" (available by default at "/services"), etc.
If needed, one can use Spring ImportResource annotation to import the existing JAX-WS contexts available on the classpath.
API Documentation
JAX-WS endpoints support WSDL.
Service Registry Publication
Publication of JAX-WS endpoints into well-known service registries such as Netflix Eureka Registry can be achieved similarly to the way JAX-RS endpoints are registered and is shown in aJAX-RS Spring Boot Scandemo.
Examples
Consider the following Configuration instance:
JAX-WS Configuration
Having a CXF JAX-WS starter alongside this Configuration is sufficient for enabling a CXF JAX-WS endpoint which will respond to SOAP request URI such as
"http://localhost:8080/services/Hello".
Please also see aJAX-WS Spring Bootdemo.
参考
/shangshen/p/11106066.html
/bicheng4769/article/details/104362433
/createfly111/article/details/90552058
/docs/springboot.html