我用swagger做我的REST文档。我已经设置了它并获得了SwaggerUi的访问权限,并且还使用支持的方法查看了我配置的所有REST资源。
在我的后端,我有一个ControllerAdvice
,它为我的所有控制器执行全局异常处理。当我尝试创建已经存在的资源时,在控制器建议中处理的示例异常是ResourceAlreadyExistsException
。在这种情况下,我的异常处理程序以409 CONFLICT
状态代码响应。
@ExceptionHandler(value = ResourceAlreadyExistsException.class)
@ResponseStatus(HttpStatus.CONFLICT)
protected ErrorResponse handleResourceAlreadyExists(ResourceAlreadyExistsException ex, WebRequest request) {
return new ErrorResponse(ex.getMessage());
}
有了这个前提条件,我在REST控制器中映射的create方法如下所示:
@RequestMapping(method = POST)
@ResponseStatus(HttpStatus.CREATED)
public RoleDto createRole(@RequestBody RoleDto roleDto) throws ResourceAlreadyExistsException {
return roleManager.createRole(roleDto);
}
使用默认配置,Swagger仅显示201作为可能的响应代码。虽然409也是可能的。
当然,我可以将@ApiResponse(code = 409, message = "Role already exists")
定义添加到createRole()
方法,但这似乎是双重信息,因为我已经暗示通过抛出异常。
我如何告诉招摇,如果可以抛出ResourceAlreadyExistsException
,409也可能是响应代码?
我已尝试在@ApiResponse
上定义ResourceAlreadyExistsException
,但这不起作用。
答案 0 :(得分:1)
SpringFox中还没有这个功能,尽管他们一直在寻找有人来实现它。