Spring Boot & Apache CXF 开发具有多Endpoint的 WebService
2017-08-14 23:06
639 查看
通过Spring Boot 整合Apache CXF开发WebService,可以避免以前Spring大量的xml配置文件。采用这种方式开发 WebService的公开例子不少,例如:
详解Spring boot+CXF开发WebService Demo
Spring Boot & Apache CXF——简单的webservice,并实现用户验证
Spring boot 整合CXF开发web service
spring boot整合cxf发布webservice服务和cxf客户端调用
Spring Boot & Apache CXF – How to SOAP in 2016
上述用例简单直观,能很快地实现单个Endpint的WebService服务端,本文不再赘述。但实际情况中,往往是在一个Web服务中具有多类业务的Endpoints,甚至存在相同业务但不同版本的Endpoints。这时应该怎么实现?
实际上,只要在CXF Configuration文件继续添加Endpoint 和相应的WebService实现即可。如下是目前已验证的实例代码。
如果希望存在多个版本的Endpoint,可通过创建不同版本的 CxfConfig 类来实现。每个CxfConfig类中都包含一个SpringBus和 ServletRegistrationBean。
例如,对于如下两套版本的API:
可以通过SpringBoot定义如下两个Configuration 类:
SomeApiV1Config 类:
SomeApiV2Config 类:
上述示例,来源于Multiple endpoints using CXF and Spring Boot 一文。
详解Spring boot+CXF开发WebService Demo
Spring Boot & Apache CXF——简单的webservice,并实现用户验证
Spring boot 整合CXF开发web service
spring boot整合cxf发布webservice服务和cxf客户端调用
Spring Boot & Apache CXF – How to SOAP in 2016
上述用例简单直观,能很快地实现单个Endpint的WebService服务端,本文不再赘述。但实际情况中,往往是在一个Web服务中具有多类业务的Endpoints,甚至存在相同业务但不同版本的Endpoints。这时应该怎么实现?
实际上,只要在CXF Configuration文件继续添加Endpoint 和相应的WebService实现即可。如下是目前已验证的实例代码。
@Configuration public class CxfConfig { @Bean public ServletRegistrationBean dispatcherServlet() { return new ServletRegistrationBean(new CXFServlet(), "/services/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public MyService myService() { return new MyServiceImpl(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), myService()); endpoint.publish("/myservice"); return endpoint; } /** * Another Webservice of Mine. * @return */ @Bean public AnotherService anotherService() { return new AnotherServiceImpl(); } @Bean public EndpointImpl anotherEndpoint(AnotherService anotherService) { EndpointImpl endpoint = new EndpointImpl(springBus(), anotherService); endpoint.publish("/another"); return endpoint; } }
如果希望存在多个版本的Endpoint,可通过创建不同版本的 CxfConfig 类来实现。每个CxfConfig类中都包含一个SpringBus和 ServletRegistrationBean。
例如,对于如下两套版本的API:
/api/v1/orders/get /api/v1/orders/create /api/v2/orders/get /api/v2/orders/create
可以通过SpringBoot定义如下两个Configuration 类:
SomeApiV1Config 类:
@Configuration public class SomeApiV1Config { @Bean public SpringBus springBusV1() { SpringBus bus = new SpringBus(); bus.setId("v1"); return bus; } @Bean public ServletRegistrationBean v1Servlet() { CXFServlet cxfServlet = new CXFServlet(); cxfServlet.setBus(springBusV1()); ServletRegistrationBean servletBean = new ServletRegistrationBean(cxfServlet, "/api/v1/*"); servletBean.setName("v1"); return servletBean; } @Bean public EndpointImpl getOrderV1(GetOrderServiceV1 service) { EndpointImpl endpoint = new EndpointImpl(springBusV1(), service); endpoint.publish("/orders/get"); return endpoint; } @Bean public EndpointImpl createOrderV1(CreateOrderServiceV1 service) { EndpointImpl endpoint = new EndpointImpl(springBusV1(), service); endpoint.publish("/orders/create"); return endpoint; } }
SomeApiV2Config 类:
@Configuration public class SomeApiV2Config { @Bean public SpringBus springBusV2() { SpringBus bus = new SpringBus(); bus.setId("v2"); return bus; } @Bean public ServletRegistrationBean v2Servlet() { CXFServlet cxfServlet = new CXFServlet(); cxfServlet.setBus(springBusV2()); ServletRegistrationBean servletBean = new ServletRegistrationBean(cxfServlet, "/api/v2/*"); servletBean.setName("v2"); return servletBean; } @Bean public EndpointImpl getOrderV2(GetOrderServiceV2 service) { EndpointImpl endpoint = new EndpointImpl(springBusV2(), service); endpoint.publish("/orders/get"); return endpoint; } @Bean public EndpointImpl createOrderV2(CreateOrderServiceV2 service) { EndpointImpl endpoint = new EndpointImpl(springBusV2(), service); endpoint.publish("/orders/create"); return endpoint; } }
上述示例,来源于Multiple endpoints using CXF and Spring Boot 一文。
相关文章推荐
- Spring Boot & Apache CXF——简单的webservice,并实现用户验证
- Spring Boot & Apache CXF——简单的webservice,并实现用户验证
- SpringBoot 集成CXF开发WebService
- 使用Spring Boot快速开发模式开发简单的WebService服务API
- 【Spring Boot&& Spring Cloud系列】单点登录SSO之OAuth2官方开发文档翻译
- springboot+CXF开发webservice对外提供接口(转)
- 详解Spring boot+CXF开发WebService Demo
- Spring boot 整合CXF开发webService
- 用Spring Boot & Angular2快速开发文件上传服务
- 【BSCL】Android与webservice开发技术总结(eclipse& visual studio 2008)
- RabbitMQ的Java应用(3) -- 使用spring-boot-starter-amqp开发生产者应用
- Spring Boot干货系列:(四)开发Web应用之Thymeleaf篇
- Idea开发工具 导入spring_boot测试项目例子报错
- springboot & disconf
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- springboot开发日志(4): mybatis找不到mapper
- Spring Boot SOAP Webservice例子
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
- springboot开发环境热部署(idea下)
- spring-boot 热部署 intellij IDE(开发过程)