从Swagger2/SpringFox迁移到SpringDoc的文档没有说明如何将@ApiOperation
上的response
和responseContainer
字段转换为新的@Operation
注释。我该怎么做?
在转换为SpringDoc之前,我在SpringFox Swagger 2中使用response
和responseContainer
来显式声明,例如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=""
。