나는 모든 요청을 가로 채고 사용자 권한 검사를 수행하는 HandlerInterceptorAdapter
을 가지고 있습니다. 아주 기본적으로 :스프링 MVC - AcceptException 헤더 기반의 @ExceptionHandler
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
User user = ... // get user
checkIfAuthorized(user); // throws AuthorizationException
return true;
}
나는 그 AuthorizationException
에 대한 @ExceptionHandler
이 있습니다.
@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 쉽게 변경 가능) 경우가 미세하다. 특정 Accept
헤더에 대해 다른 @ExceptionHandler
을 어떻게 만들 수 있습니까?
@RequestMapping
은 produces()
입니다. @ExceptionHandler
과 비슷한 것이 있습니까?
을 할 수있는 컨트롤러로, 모두 동일, HTML를 들어, [RequestToViewNameTranslator] (HTTP를 사용합니다 : // 정적 .springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/RequestToViewNameTranslator.html). 나는 경로 뒤에 이름 붙여진 jsp를 원하지 않는다. 귀하의 첫 번째 예제를 통해'text/html'에 대한 뷰 이름을 반환하지 않으려는 경우'ResponseEntity'로 가능합니까? –
ResponseEntity가 REST 응답에 더 중점을 두는 것처럼 보입니다. 이 경우에는 ModelAndView (또는 View 만)를 반환해야합니다. – Luciano
반환 형식을 단순히'Object'로 만들고 헤더를 기준으로 반환하는 것이 좋습니다? –