In my controller I do a permissions check to see if the user can do the certain action. If they can't, I'd like to return a 404.
How do I tell Spring to return a 404?
In my controller I do a permissions check to see if the user can do the certain action. If they can't, I'd like to return a 404.
How do I tell Spring to return a 404?
You can throw an exception and handle it in a controller-level method:
@Controller
public class MyController {
@ResponseStatus(NOT_FOUND)
@ExceptionHandler({UnauthorizedException.class})
public void handle() {
// ...
}
}
If any controller method throw a UnauthorizedException.
, the above handler method will be invoked to handle it and return a 404 error.
You can derive your exception from HttpException and path 404 code to base constructor:
public class MyNotFoundException : HttpException
{
public MyNotFoundException(string message, Exception inner)
: base(404, message, inner)
{
}
}