I'm doing my REST documentation with swagger. I've set it up and got access on SwaggerUi and also see all my configured REST resources with their supported methods.
In my backend I have a ControllerAdvice
, which does a global exception handling for all my controllers. A example exception which gets handled in the controller advice is ResourceAlreadyExistsException
, when I try to create a resource which already exists, obviously. In that case my exception handler responds with a 409 CONFLICT
status code.
@ExceptionHandler(value = ResourceAlreadyExistsException.class)
@ResponseStatus(HttpStatus.CONFLICT)
protected ErrorResponse handleResourceAlreadyExists(ResourceAlreadyExistsException ex, WebRequest request) {
return new ErrorResponse(ex.getMessage());
}
With this pre-condition, my create method which is mapped in the REST controller looks like this:
@RequestMapping(method = POST)
@ResponseStatus(HttpStatus.CREATED)
public RoleDto createRole(@RequestBody RoleDto roleDto) throws ResourceAlreadyExistsException {
return roleManager.createRole(roleDto);
}
With the default configuration, Swagger only shows me 201 as possible response code. Although 409 is possible too.
Of course I could add the @ApiResponse(code = 409, message = "Role already exists")
definition to the createRole()
method, but this seems double information as I already imply that by throwing the exception.
How can I tell swagger, that if a ResourceAlreadyExistsException
can be be thrown, 409 is also a possible response code?
I've tried defining @ApiResponse
on the ResourceAlreadyExistsException
, but that didn't work.
That feature does not exist yet in SpringFox, although they have been looking for someone to implement it for quite some time now.