Automating service provisioning and CI/CD using AWS Pipeline, Bitbucket and Terraform (work in progress)
1. OVERVIEW
A while back I published a blog post about Microservices using Spring Boot, Jersey, Swagger and Docker that takes advantage of the Spring ecosystem and a JAX-RS implementation in Jersey 2. In another post, Services Registration and Discovery using Eureka, Ribbon and Feign I also mentioned that Jersey 2-based endpoints attempting to register with Spring Cloud Netflix Eureka registry failed due to the fact that Eureka and Ribbon clients bring in Jersey 1 dependencies and thus conflicting with Jersey 2’s.
Important:Spring Boot adds support to autoconfigure Jersey 2 via spring-boot-starter-jersey. It seems Eureka client and Ribbon depend on Jersey 1 causing conflicts like java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
In this follow-up post I plan to demonstrate how to integrate Apache CXF 3.1.xJAX-RS-based endpoints implementation with Spring Boot and documenting them using Swagger. The services following this setup should be able to register with Spring Cloud Netflix Eureka since no Jersey dependency would be transitively included.
This command will create a Maven project in a folder named springboot-cxf-swagger with the actuator and web-related Spring Boot dependencies. Read on if you are interested in adding Spring Boot support using the BOM approach.
This is a snippet of the application’s start class as defined in pom.xml:
4. IMPLEMENT AND DOCUMENT API ENDPOINTS USING CXF AND SWAGGER
Note: Any reference to Swagger throughout this blog post refers to Swagger 2.0.
Let’s first add Apache CXF and Swagger dependencies:
cxf-spring-boot-starter-jaxrs is a CXF Spring Boot starter that autoconfigures the CXF servlet based on configuration properties found in a properties file such as application.yml.
cxf-rt-rs-service-description allows the WADL to be auto-generated at runtime.
cxf-rt-rs-service-description-swagger allows the generation Swagger 2.0 documents from JAX-RS endpoints.
Important: Including jackson-jaxrs-json-provider dependency along with a JacksonJsonProvider bean fixed ERROR JAXRSUtils:1793 - No message body writer has been found for class com.asimio.cxfdemo.rest.v1.model.Hello, ContentType: application/json.
Let’s include a snippet of the HelloResource.java interface, this is where the JAX-RS and Swagger annotations are configured:
@ApiXXXXXX annotations are Swagger-specific and the remaining are JAX-RS’s.
Also notice the @Path, @Consumes and @Produces annotations, their purpose is to pass the API version either in the URL or in the Accept header.
A snippet of the HelloResourceImpl.java interface implementation follows:
A simple implementation worth mentioning the class should not include any JAX-RS annotation.
Warning: No JAX-RS annotation should be included in the implementation class, method or method arguments.
CXF includes the concept of Feature and according to the documentation, a Feature is used to customize a Server, Client, or Bus, normally adding capabilities.
CXF’s Swagger2Feature is used to wrap Swagger’s BeanConfig in a CXFFeature via a Spring bean to dynamically generate the Swagger definition file used to feed the Swagger UI. Here is a snippet of such configuration:
And lastly the CXF-related configuration settings used by the CXF Spring Boot starter to autoconfigure the CXF servlet:
application.yml:
1
2
3
4
5
6
7
8
9
10
...# Spring MVC dispatcher servlet path. Needs to be different than CXF's to enable/disable Actuator endpoints access (/info, /health, ...)server.servlet-path:/# http://cxf.apache.org/docs/springboot.html#SpringBoot-SpringBootCXFJAX-RSStartercxf:path:/api# CXFServlet URL patternjaxrs:component-scan:true...
Thanks for reading and as always, feedback is very much appreciated. If you found this post helpful and would like to receive updates when content like this gets published, sign up to the newsletter.
6. SOURCE CODE
Accompanying source code for this blog post can be found at:
Orlando L Otero is a Sr Software Engineer Consultant focusing on integration, modernization, cloud adoption and migration, microservices, API design and implementation, and agile delivery.