第 341 章 Swagger Java 组件
从 Camel 2.16 开始提供
Rest DSL 可以与 camel-swagger-java
模块集成,该模块使用 Swagger 来公开 REST 服务及其 API。
Maven 用户需要将以下依赖项添加到这个组件的 pom.xml
中:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-swagger-java</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
camel-swagger-java 模块可从 REST 组件中使用(不需要 servlet)
341.1. 在 rest-dsl 中使用 Swagger
您可以通过配置 apiContextPath
dsl 来启用 rest-dsl 中的 swagger api,如下所示:
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"); } }