我有一个HandlerInterceptorAdapter
拦截所有请求并执行用户授权检查的。非常基本:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
User user = ... // get user
checkIfAuthorized(user); // throws AuthorizationException
return true;
}
然后我有@ExceptionHandler
一个AuthorizationException
。
@ExceptionHandler(value = AuthorizationException.class)
public ResponseEntity<String> handleNotAuthorized(AuthorizationException e) {
// TODO Custom EXCEPTION HANDLER for json/jsp/xml/other types, based on content type
ResponseEntity<String> responseEntity = new ResponseEntity<>("You are not authorized to access that page.", HttpStatus.UNAUTHORIZED);
return responseEntity;
}
text/plain
如果(未经授权的)请求接受(并且可以轻松更改为 json),这很好。我怎样才能@ExceptionHandler
为特定的s制作不同的sAccept
标题制作不同的 s?
@RequestMapping
有produces()
。有类似的东西@ExceptionHandler
吗?