What is happening is an AccessDeniedException
is being thrown, so you want to configure your system to intercept that exception and instead return JSON.
You can set up an @ExceptionHandler
method within your controller which catches the AccessDeniedException
. However, you probably want to do the same thing in all your controllers, so if you are using Spring 3.2, you can use @ControllerAdvice
annotation on a separate 'advice' class and then include the @ExceptionHandler
method in there.
@ControllerAdvice
public class ExceptionControllerAdvice {
@ExceptionHandler(AccessDeniedException.class)
@ResponseBody
public String exception(AccessDeniedException e) {
return "{\"status\":\"access denied\"}";
}
}