...
You configure the swagger in the web.xml as shown below:
Code Block |
---|
<!-- to setup Camel Swagger api servlet when using Spring --> <servlet> <!-- Camel 2.15 onwards --> <servlet-name>ApiDeclarationServlet<name>SwaggerServlet</servlet-name> <servlet-class>org.apache.camel.swagger.servlet.RestSwaggerServlet</servlet-class> <init-param> <!-- we specify the base.path using relative notation, that means the actual path will be calculated at runtime as http://server:port/contextpath/rest --> <param-name>base.path</param-name> <param-value>rest</param-value> </init-param> <init-param> <!-- we specify the api.path using relative notation, that means the actual path will be calculated at runtime as http://server:port/contextpath/api-docs --> <param-name>api.path</param-name> <param-value>api-docs</param-value> </init-param> <init-param> <param-name>api.version</param-name> <param-value>1.2.3</param-value> </init-param> <init-param> <param-name>api.title</param-name> <param-value>User Services</param-value> </init-param> <init-param> <param-name>api.description</param-name> <param-value>Camel Rest Example with Swagger that provides an User REST service</param-value> </init-param> <load-on-startup>2<startup>1</load-on-startup> </servlet> <!-- swagger api declaration --> <servlet-mapping> <servlet-name>ApiDeclarationServlet<name>SwaggerServlet</servlet-name> <url-pattern>/api-docs/*</url-pattern> </servlet-mapping> |
Using Swagger in rest-dsl
You can enable the swagger api from the rest-dsl by configuring the apiContextPath
dsl as shown below:
Code Block | ||||
---|---|---|---|---|
| ||||
public class UserRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
// configure we want to use servlet as the component for the rest DSL
// and we enable json binding mode
restConfiguration().component("netty4-http").bindingMode(RestBindingMode.json)
// and output using pretty print
.dataFormatProperty("prettyPrint", "true")
// setup context path and port number that netty will use
.contextPath("/").port(8080)
// add swagger api-doc out of the box
.apiContextPath("/api-doc")
.apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
// and enable CORS
.apiProperty("cors", "true");
// this user REST service is json only
rest("/user").description("User rest service")
.consumes("application/json").produces("application/json")
.get("/{id}").description("Find user by id").outType(User.class)
.param().name("id").type(path).description("The id of the user to get").dataType("int").endParam()
.to("bean:userService?method=getUser(${header.id})")
.put().description("Updates or create a user").type(User.class)
.param().name("body").type(body).description("The user to update or create").endParam()
.to("bean:userService?method=updateUser")
.get("/findAll").description("Find all users").outTypeList(User.class)
.to("bean:userService?method=listUsers");
}
} |
Options
The swagger module can be configured using the following options. To configure using a servlet you use the init-param as shown above. When configuring directly in the rest-dsl, you use the apiProperty
dsl.
...
When contextIdListing is enabled then its detecting all the running CamelContexts in the same JVM. These contexts are listed in the root path, eg `/api-docs` as a simple list of names in json format. To access the swagger documentation then the context-path must be appended with the Camel context id, such as `api-docs/myCamel`. The option apiContextIdPattern can be used to filter the names in this list.
Examples
In the Apache Camel distribution we ship the camel-example-swagger-cdi
and camel-example-swagger-java
which demonstrates using this Swagger component.
...