@Override public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { exceptionResolvers.add(0, new ResponseStatusExceptionResolver()); }
@Override @Nullable protected ModelAndView doResolveException( HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) { try { if (ex instanceof ResponseStatusException) { return resolveResponseStatusException((ResponseStatusException) ex, request, response, handler); } ResponseStatus status = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class); if (status != null) { return resolveResponseStatus(status, request, response, handler, ex); } if (ex.getCause() instanceof Exception) { return doResolveException(request, response, handler, (Exception) ex.getCause()); } } catch (Exception resolveEx) { if (logger.isWarnEnabled()) { logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", resolveEx); } } return null; }
/** * Template method that handles the {@link ResponseStatus @ResponseStatus} annotation. * <p>The default implementation delegates to {@link #applyStatusAndReason} * with the status code and reason from the annotation. * @param responseStatus the {@code @ResponseStatus} annotation * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen at the * time of the exception, e.g. if multipart resolution failed * @param ex the exception * @return an empty ModelAndView, i.e. exception resolved */ protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) throws Exception { int statusCode = responseStatus.code().value(); String reason = responseStatus.reason(); return applyStatusAndReason(statusCode, reason, response); }
exceptionResolvers.add(exceptionHandlerResolver); ResponseStatusExceptionResolver responseStatusResolver = new ResponseStatusExceptionResolver(); responseStatusResolver.setMessageSource(this.applicationContext); exceptionResolvers.add(responseStatusResolver);
@Test public void statusCodeAndReasonMessage() { Locale locale = Locale.CHINESE; LocaleContextHolder.setLocale(locale); try { StaticMessageSource messageSource = new StaticMessageSource(); messageSource.addMessage("gone.reason", locale, "Gone reason message"); exceptionResolver.setMessageSource(messageSource); StatusCodeAndReasonMessageException ex = new StatusCodeAndReasonMessageException(); exceptionResolver.resolveException(request, response, null, ex); assertEquals("Invalid status reason", "Gone reason message", response.getErrorMessage()); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Override protected ModelAndView doResolveException( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ResponseStatus status = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class); if (status != null) { try { return resolveResponseStatus(status, request, response, handler, ex); } catch (Exception resolveEx) { logger.warn("ResponseStatus handling resulted in exception", resolveEx); } } else if (ex.getCause() instanceof Exception) { return doResolveException(request, response, handler, (Exception) ex.getCause()); } return null; }
exceptionResolvers.add(exceptionHandlerResolver); ResponseStatusExceptionResolver responseStatusResolver = new ResponseStatusExceptionResolver(); responseStatusResolver.setMessageSource(this.applicationContext); exceptionResolvers.add(responseStatusResolver);
/** * A method available to subclasses for adding default * {@link HandlerExceptionResolver HandlerExceptionResolvers}. * <p>Adds the following exception resolvers: * <ul> * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions through * {@link org.springframework.web.bind.annotation.ExceptionHandler} methods. * <li>{@link ResponseStatusExceptionResolver} for exceptions annotated with * {@link org.springframework.web.bind.annotation.ResponseStatus}. * <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring exception types * </ul> */ protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { ExceptionHandlerExceptionResolver exceptionHandlerResolver = createExceptionHandlerExceptionResolver(); exceptionHandlerResolver.setContentNegotiationManager(mvcContentNegotiationManager()); exceptionHandlerResolver.setMessageConverters(getMessageConverters()); exceptionHandlerResolver.setCustomArgumentResolvers(getArgumentResolvers()); exceptionHandlerResolver.setCustomReturnValueHandlers(getReturnValueHandlers()); if (jackson2Present) { exceptionHandlerResolver.setResponseBodyAdvice( Collections.<ResponseBodyAdvice<?>>singletonList(new JsonViewResponseBodyAdvice())); } exceptionHandlerResolver.setApplicationContext(this.applicationContext); exceptionHandlerResolver.afterPropertiesSet(); exceptionResolvers.add(exceptionHandlerResolver); ResponseStatusExceptionResolver responseStatusResolver = new ResponseStatusExceptionResolver(); responseStatusResolver.setMessageSource(this.applicationContext); exceptionResolvers.add(responseStatusResolver); exceptionResolvers.add(new DefaultHandlerExceptionResolver()); }
@Override @Nullable protected ModelAndView doResolveException( HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) { try { if (ex instanceof ResponseStatusException) { return resolveResponseStatusException((ResponseStatusException) ex, request, response, handler); } ResponseStatus status = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class); if (status != null) { return resolveResponseStatus(status, request, response, handler, ex); } if (ex.getCause() instanceof Exception) { return doResolveException(request, response, handler, (Exception) ex.getCause()); } } catch (Exception resolveEx) { if (logger.isWarnEnabled()) { logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", resolveEx); } } return null; }
@Override public void configureHandlerExceptionResolvers( final List<HandlerExceptionResolver> exceptionResolvers) { ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver(); exceptionHandlerExceptionResolver.setMessageConverters(getMessageConverters()); exceptionHandlerExceptionResolver.afterPropertiesSet(); exceptionResolvers.add(exceptionHandlerExceptionResolver); exceptionResolvers.add(new ResponseStatusExceptionResolver()); exceptionResolvers.add(new LogUncaughtException(applicationContext)); }
/** * Template method that handles an {@link ResponseStatusException}. * <p>The default implementation delegates to {@link #applyStatusAndReason} * with the status code and reason from the exception. * @param ex the exception * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen at the * time of the exception, e.g. if multipart resolution failed * @return an empty ModelAndView, i.e. exception resolved * @since 5.0 */ protected ModelAndView resolveResponseStatusException(ResponseStatusException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws Exception { int statusCode = ex.getStatus().value(); String reason = ex.getReason(); return applyStatusAndReason(statusCode, reason, response); }
/** * @param resolvers exceptionResolvers * @deprecated see: {@link org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport * #addDefaultHandlerExceptionResolvers(List) addDefaultHandlerExceptionResolvers} */ @Deprecated protected final void addDefaultHandlerExceptionResolvers( // final List<HandlerExceptionResolver> resolvers // ) { final List<ResponseBodyAdvice<?>> interceptors = newArrayList(); if (getJackson2Present()) { interceptors.add(new JsonViewResponseBodyAdvice()); } final ExceptionHandlerExceptionResolver handlerExceptionResolver = new ExceptionHandlerExceptionResolver(); handlerExceptionResolver.setMessageConverters(this.defaultHttpEntityMethodProcessor.getMessageConverters()); handlerExceptionResolver.setResponseBodyAdvice(interceptors); handlerExceptionResolver.setApplicationContext(this.applicationContext); handlerExceptionResolver.afterPropertiesSet(); final ResponseStatusExceptionResolver responseStatusExceptionResolver = new ResponseStatusExceptionResolver(); responseStatusExceptionResolver.setMessageSource(this.applicationContext); resolvers.add(handlerExceptionResolver); // resolvers add responseStatusExceptionResolver ? // resolvers add new DefaultHandlerExceptionResolver ? }
/** * Template method that handles the {@link ResponseStatus @ResponseStatus} annotation. * <p>The default implementation delegates to {@link #applyStatusAndReason} * with the status code and reason from the annotation. * @param responseStatus the {@code @ResponseStatus} annotation * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen at the * time of the exception, e.g. if multipart resolution failed * @param ex the exception * @return an empty ModelAndView, i.e. exception resolved */ protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) throws Exception { int statusCode = responseStatus.code().value(); String reason = responseStatus.reason(); return applyStatusAndReason(statusCode, reason, response); }
/** * @param resolvers exceptionResolvers * @deprecated see: {@link org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport * #addDefaultHandlerExceptionResolvers(List) addDefaultHandlerExceptionResolvers} */ @Deprecated protected final void addDefaultHandlerExceptionResolvers( // final List<HandlerExceptionResolver> resolvers // ) { final List<ResponseBodyAdvice<?>> interceptors = newArrayList(); if (getJackson2Present()) { interceptors.add(new JsonViewResponseBodyAdvice()); } final ExceptionHandlerExceptionResolver handlerExceptionResolver = new ExceptionHandlerExceptionResolver(); handlerExceptionResolver.setMessageConverters(this.defaultHttpEntityMethodProcessor.getMessageConverters()); handlerExceptionResolver.setResponseBodyAdvice(interceptors); handlerExceptionResolver.setApplicationContext(this.applicationContext); handlerExceptionResolver.afterPropertiesSet(); final ResponseStatusExceptionResolver responseStatusExceptionResolver = new ResponseStatusExceptionResolver(); responseStatusExceptionResolver.setMessageSource(this.applicationContext); resolvers.add(handlerExceptionResolver); // resolvers add responseStatusExceptionResolver ? // resolvers add new DefaultHandlerExceptionResolver ? }
/** * Template method that handles an {@link ResponseStatusException}. * <p>The default implementation delegates to {@link #applyStatusAndReason} * with the status code and reason from the exception. * @param ex the exception * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen at the * time of the exception, e.g. if multipart resolution failed * @return an empty ModelAndView, i.e. exception resolved * @since 5.0 */ protected ModelAndView resolveResponseStatusException(ResponseStatusException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws Exception { int statusCode = ex.getStatus().value(); String reason = ex.getReason(); return applyStatusAndReason(statusCode, reason, response); }