将SpringFox Swagger2升级为SpringDoc Openapi(swagger3)注释



从Swagger2/SpringFox迁移到SpringDoc的文档没有说明如何将@ApiOperation上的responseresponseContainer字段转换为新的@Operation注释。我该怎么做?

在转换为SpringDoc之前,我在SpringFox Swagger 2中使用responseresponseContainer来显式声明,例如response="MyResponseDto.class"responseContainer="List",以确保所有MyResponseDto的属性和包含的集合都正确显示在每个API调用的文档的Responses部分中,而不仅仅是"string""object"

只要我在REST控制器方法签名上为ResponseEntity的泛型添加正确的类型,SpringDoc就会自动处理这一切,并正确显示响应。

因此,取而代之的是:

@GetMapping(value = "/items/{id}")
@Operation(summary = "Get items for component", description = "Retrieves all items for the component by the supplied id")
public ResponseEntity updateMapItem(@PathVariable String id) {
return new ResponseEntity<>(myService.getItemsForComponent(id), HttpStatus.OK);
}

使用此:

@GetMapping(value = "/items/{id}")
@Operation(summary = "Get items for component", description = "Retrieves all items for the component by the supplied id")
public ResponseEntity<List<MyResponseDto>> updateMapItem(@PathVariable String id) {
return new ResponseEntity<>(myService.getItemsForComponent(id), HttpStatus.OK);
}

因此,不再需要response=""responseContainer=""

最新更新