Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 3 - Respond to request with a 404? [duplicate]

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?

like image 261
Kyle Avatar asked Feb 10 '10 17:02

Kyle


1 Answers

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.

like image 146
Pascal Thivent Avatar answered Oct 28 '22 21:10

Pascal Thivent