4

Java アノテーションを使用して、複数の 404 応答 (より広義には複数の同じ HTTP コード応答) を作成する方法。

私はもう試した:

@ApiResponse(
    responseCode = "404",
    description = "Not Found 1"
)
@ApiResponse(
    responseCode = "404",
    description = "Not Found 2"
)

また、複数@Content

@ApiResponse(
    responseCode = "404",
    content = {
        @Content(schema = @Schema(name = "404-1", description = "404-1")),
        @Content(schema = @Schema(name = "404-2", description = "404-2"))
    }
)

複数に似たものを取得できる唯一の方法は、次を使用すること@ExampleObject[]です。

@ApiResponse(
    responseCode = "404",
    content = @Content(
        mediaType = "application/json",
        examples = {
            @ExampleObject(name = "404-1", description = "Not Found 1 desc"),
            @ExampleObject(name = "404-2", description = "Not Found 2 desc")
        }
    )
)

これは理想的ではありません。それらすべてを表示するには人間の操作が必要であり、望ましくないからです期待は次のとおりです。

- 200
- 404 Description 1
- 404 Description 2
- 404 Description 3

またはさらに良い:

- 200
- 404 Description 1
      Description 2
      Description 3

私は springdoc と次の dep を使用しています:

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-ui</artifactId>
  <version>1.4.3</version>
</dependency>
4

2 に答える 2

0

<br/>新しい行が必要な説明に HTML タグを追加するだけで問題を解決しました。

@Operation(
   responses = {
      @ApiResponse(responseCode = "404", content = @Content,
         description = 
            "This is potential 404 #1 <br/>" +
            "This is potential 404 #2"
      )
   }
)

スナップショット

あるいは、

これをより読みやすくするための注釈を作成できます。たとえば、次のよう@ApiResponse404にして操作に追加できますOperationCustomizer

@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
    ApiResponse404 notFounds = handlerMethod.getMethodAnnotation(ApiResponse404.class);
    if (notFounds != null)
        operation.getResponses()
                 .addApiResponse("404", new ApiResponse()
                                            .description(String.join("<br/>", notFounds.value()))
                                );
    return operation;
}

もちろん@Content、注釈に簡単に追加できる を考慮する必要がありますが、私のシナリオでは必要ありません。説明が必要なだけです。

次に、コントローラーで注釈を使用できます。

@GetMapping("/helloworld")
@ApiResponse404({"This is potential 404 #1", "This is potential 404 #2"})
String getHelloWorld() {
    return "Hello. World.";
}
于 2020-08-10T14:08:42.387 に答える