第7章 MicroProfile OpenAPI アノテーションの REST コントローラーコードへの追加
MicroProfile OpenAPI アノテーションを REST コントローラーコードに追加し、REST エンドポイントの詳細な OpenAPI スキーマを生成できます。
手順
@OpenApiDefinition
アノテーションをGreetingController
のクラスレベルに追加します。アノテーションの例に示されているデータを含めます。@OpenAPIDefinition( info = @Info( title="Greeting API", version = "1.0.1", contact = @Contact( name = "Greeting API Support", url = "http://exampleurl.com/contact", email = "techsupport@example.com"), license = @License( name = "Apache 2.0", url = "https://www.apache.org/licenses/LICENSE-2.0.html")) )
@Tag
アノテーションを使用してエンドポイント定義にアノテーションを付けます。エンドポイントごとに名前と説明を指定します。@Tag(name = "Hello", description = "Just say hello") @GetMapping(produces=MediaType.TEXT_PLAIN_VALUE) public String hello() { return "hello"; } @GetMapping(value = "/{name}", produces=MediaType.APPLICATION_JSON_VALUE) @Tag(name = "Hello to someone", description = "Just say hello to someone") public Greeting hello(@PathVariable(name = "name") String name) { return new Greeting("hello " + name); }
アノテーションで指定したデータは、生成された OpenAPI スキーマに表示されます。
openapi: 3.0.3 info: title: Greeting API contact: name: Greeting API Support url: http://exampleurl.com/contact email: techsupport@example.com license: name: Apache 2.0 url: https://www.apache.org/licenses/LICENSE-2.0.html version: 1.0.1 tags: - name: Hello description: Just say hello - name: Hello to someone description: Just say hello to someone paths: /greeting: get: tags: - Hello responses: '200': description: OK content: text/plain: schema: type: string /greeting/{name}: get: tags: - Hello to someone parameters: - name: name in: path required: true schema: type: string responses: '200': description: OK content: application/json: schema: $ref: '#/components/schemas/Greeting' components: schemas: Greeting: type: object properties: message: type: string