我正在使用Swagger来处理我的REST文档。我已经设置好了,并且可以在SwaggerUi上访问,并且还可以看到我配置的所有REST资源及其受支持的方法。Swagger - 声明异常的渲染响应状态
在我的后端我有一个ControllerAdvice
,它为我的所有控制器执行全局异常处理。当我尝试创建一个已经存在的资源时,在控制器建议中处理的示例异常是ResourceAlreadyExistsException
。在这种情况下,我的异常处理程序会以409 CONFLICT
状态码进行响应。
@ExceptionHandler(value = ResourceAlreadyExistsException.class)
@ResponseStatus(HttpStatus.CONFLICT)
protected ErrorResponse handleResourceAlreadyExists(ResourceAlreadyExistsException ex, WebRequest request) {
return new ErrorResponse(ex.getMessage());
}
有了这个先决条件,我的创建被映射在REST控制器看起来像这样方法:
@RequestMapping(method = POST)
@ResponseStatus(HttpStatus.CREATED)
public RoleDto createRole(@RequestBody RoleDto roleDto) throws ResourceAlreadyExistsException {
return roleManager.createRole(roleDto);
}
在默认配置,扬鞭仅示出了箱201尽可能响应代码。尽管409也是可能的。
当然,我可以将@ApiResponse(code = 409, message = "Role already exists")
定义添加到createRole()
方法,但这似乎是双重信息,因为我已经暗示抛出异常。
我该如何告诉大家,如果一个ResourceAlreadyExistsException
可以被抛出,409也是一个可能的响应码?
我已经尝试在ResourceAlreadyExistsException
上定义@ApiResponse
,但这并不奏效。
杜林的Derb角色实体被标记为独特的(角色名)?因为休眠不知道角色已经存在或没有给出一个唯一的约束 –