concept Swagger in category camel

appears as: Swagger
Camel in Action

This is an excerpt from Manning's book Camel in Action.

The CartDto class has two fields to store the item ID and the number of items to purchase. The getter methods ❶ have been annotated with Swagger’s ApiModelProperty to include descriptions of the fields, which will be used in the API documentation of the microservice.

Swagger is both a specification and framework for describing, producing, consuming, and visualizing RESTful services. Applications written with the Swagger framework contain full documentation of methods, parameters, and models directly in the source code. This ensures that the implementation and documentation of RESTful services are always in sync.

In 2016, the Swagger specification was renamed the OpenAPI specification and governed by its founding group under the Linux Foundation. In this chapter, we use the popular term Swagger instead of OpenAPI.

Adding Swagger to a CXF application

To use Swagger with CXF, you need to create a Swagger2Feature and configure it. This can be done in a JAX-RS application class, as shown in the following listing.

Listing 10.17 Using a JAX-RS application to set up a REST service with Swagger
@ApplicationPath("/") public class RestOrderApplication extends Application {     #1 private final RestOrderService orderService; public RestOrderApplication(RestOrderService orderService) { this.orderService = orderService; } @Override public Set<Object> getSingletons() { Swagger2Feature swagger = new Swagger2Feature();     #2 swagger.setBasePath("/");     #2 swagger.setHost("localhost:9000");     #2 swagger.setTitle("Order Service");     #2 swagger.setDescription("Rider Auto Parts Order Service");     #2 swagger.setVersion("2.0.0");     #2 swagger.setContact("rider@autoparts.com");     #2 swagger.setPrettyPrint(true);     #2 Set<Object> answer = new HashSet<>();     #3 answer.add(orderService);     #3 answer.add(new JacksonJsonProvider());     #3 answer.add(swagger);     #3 answer.add(new LoggingFeature());     #3 return answer; } } #1 JAX-RS Application class #2 Configures Swagger feature in CXF #3 Provides a set of features to use with CXF

JAX-RS allows you to set up your JAX-RS RESTful services using your own class implementation that must extend javax.ws.rs.core.Application. You can see this in listing 10.17, where you create the RestOrderApplication class ❶. In the getSingletons method, you can set up the services that the RESTful application should use. To use Swagger with CXF, you need to create and configure an instance of Swagger2Feature ❷. Then you configure the Rider Auto Parts orderService implementation, Jackson for JSON support, and Swagger, and then you enable logging by using the LoggingFeature ❸.