第338章 Swagger Java コンポーネント

Camel 2.16 以降で利用可能

Rest DSL は、Swagger を使用して REST サービスとその API を公開するために使用される camel-swagger-java モジュールと統合できます。

Maven ユーザーは、このコンポーネントの pom.xml に以下の依存関係を追加する必要があります。

Camel 2.16 以降、swagger コンポーネントは純粋に Java ベースであり、 

<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 コンポーネントから使用できます (サーブレットは必要ありません)。

例については、Apache Camel ディストリビューションのサンプルディレクトリーにある camel-example-swagger-cdi を参照してください。

338.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");
    }
}