第 81 章 OpenAPI Java
Rest DSL 可以与 camel-openapi-java
模块集成,该模块用于使用 OpenApi 来公开 REST 服务及其 API。
Maven 用户需要将以下依赖项添加到其 pom.xml
中。
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-openapi-java</artifactId> <version>{CamelSBProjectVersion}</version> <!-- use the same version as your Camel core version --> </dependency>
camel-openapi-java 模块可以从 REST 组件使用(无需 servlet)
81.1. 在 rest-dsl 中使用 OpenApi
您可以通过配置 apiContextPath
dsl 从 rest-dsl 启用 OpenApi 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("netty-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 OpenApi 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").outType(User[].class) .to("bean:userService?method=listUsers"); } }