第92章 Openapi Java
Rest DSL は、OpenApi を使用して REST サービスとその API を公開するために使用される camel-openapi-java モジュールと統合できます。
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>
<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 コンポーネントから使用できます (サーブレットは必要ありません)。
92.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");
}
}
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");
}
}