views:

281

answers:

2

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?

A: 

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)
    {
    }
}
Сергій
I'm guessing that's Spring.NET - there's no `HttpException` class in Spring.
skaffman
+2  A: 

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.

Pascal Thivent
Really slick way of handling this.
BrennaSoft