我正在尝试将我们手动编写的OpenAPI (swagger)迁移到使用springdoc-openapi为我们的Spring-Boot应用程序生成的OpenAPI。我们遇到了一些问题,因为控制器响应(主要是ErrorCodes)与文档不匹配。
我们已经使用了一个带@ControllerAdvice注释的处理程序配置。下面是一个片段:
@ControllerAdvice
public class ExceptionHandler {
@ResponseStatus(code = HttpStatus.NOT_FOUND)
@ApiResponse(responseCode = "404", description = "(NOT FOUND) Resource does not exist!", content = @Content)
@ExceptionHandler(NotFoundException.class)
public void handleException(NotFoundException e) {
log.warn("Returning {} due to a NotFoundException: {}", HttpStatus.NOT_FOUND, e.toString());
}
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ApiResponse(responseCode = "400", description = "(BAD REQUEST) Given resource is invalid!", content = @Content)
@ExceptionHandler(InvalidResourceException.class)
public void handleException(InvalidResourceExceptione) {
log.error("Invalid resource: {}", e.toString());
}
生成的API现在将所有定义的ApiResponses显示为所有控制器和端点的响应。因此,我使用@ControllerAdvice(basepackageclass = MyController.class)拆分处理程序配置,以对可能的异常进行分组。但是仍然有响应不适合控制器的所有端点。如:@RestController
public class MyController {
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "Create", description = "Create myResource!")
@PostMapping(value = "/myResources/", produces = {"application/json"})
@ResponseBody
public Integer create(@RequestBody MyResource newResource) throws InvalidResourceException {
return creationService.createResource(newResource).getId();
}
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Update", description = "Update myResource!")
@PutMapping(value = "/myResources/{id}", produces = {"application/json"})
public void update(@PathVariable("id") Integer id, @RequestBody MyResource newResource)
throws ResourceNotFoundException, InvalidResourceException {
return updateService.updateResource(id, newResource);
}
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Get", description = "Get myResource!")
@GetMapping(value = "/myResources/{id}", produces = {"application/json"})
@ResponseBody
public MyResource get(@PathVariable("id") Integer id) throws ResourceNotFoundException {
return loadingService.getResource(id);
}
}
POST永远不会响应我的'business' 404, GET永远不会响应我的'business' 400。是否有可能对端点进行注释,以便在API中隐藏不可能的响应代码?
我试图覆盖响应,但没有按预期工作:
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Get", description = "Get myResource!")
@ApiResponses({@ApiResponse(responseCode = "200", description = "(OK) Returning myResource"),
@ApiResponse(responseCode = "404", description = "(NOT FOUND) Resource does not exist!")})
@GetMapping(value = "/myResources/{id}", produces = {"application/json"})
@ResponseBody
public MyResource get(@PathVariable("id") Integer id) throws ResourceNotFoundException {
return loadingService.getResource(id);
}
400仍然显示…
您需要从您的@ControllerAdvice
类中删除@ApiResponse
,并需要在您的控制器类中添加相应的响应,如您所提到的。
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Get", description = "Get myResource!")
@ApiResponses({@ApiResponse(responseCode = "200", description = "(OK) Returning myResource"),
@ApiResponse(responseCode = "404", description = "(NOT FOUND) Resource does not exist!")})
@GetMapping(value = "/myResources/{id}", produces = {"application/json"})
@ResponseBody
public MyResource get(@PathVariable("id") Integer id) throws ResourceNotFoundException {
return loadingService.getResource(id);
}