Swagger 3 / OpenApi如何将两个描述添加到同一个状态代码

5kgi1eie  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在尝试对我的小api做一些文档。当状态代码400时,我可以有2个可能的描述,我应该怎么做?我想做一些类似的事情:

@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "description",
                content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                        schema = @Schema(implementation = StudentFullDTO.class))}),
        @ApiResponse(responseCode = "400", description = "description" +
                "ExceptionResponseObject", content = @Content),
        @ApiResponse(responseCode = "400", description = "Odescription",
                content = @Content)})
@PatchMapping("/{id}")
public ResponseEntity<StudentFullDTO> patch(@PathVariable String id,
                                            @RequestBody @Valid Map<Object, Object> fields) {
    StudentEntity studentEntity = studentEntityService.patchStudentEntity(id, fields);
    StudentFullDTO studentFullDTO = modelMapperService.mapObjectToObjectOfEnteredClass(studentEntity, StudentFullDTO.class);
    return new ResponseEntity<>(studentFullDTO, HttpStatus.OK);
}

是否可以多次定义相同的状态?

c8ib6hqw

c8ib6hqw1#

您需要合并这两个描述,并在同一个@ApiResponse(description = "...")注解中指定它们,这是因为OpenAPI规范只允许每个操作定义一次HTTP状态代码。

@ApiResponse(responseCode = "400",
    description = "Possible reasons: reason 1; reason 2",
    content = @Content),

相关问题