- Small modification on exception handler, because we have only one resolver.
- Added exception resolver to web configuration.
This commit is contained in:
Christian Rädel 2016-10-27 22:17:20 +02:00 committed by slavisa-baeldung
parent 6f2c2a7d1d
commit 86e387e9a4
2 changed files with 8 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.ui.context.support.ResourceBundleThemeSource; import org.springframework.ui.context.support.ResourceBundleThemeSource;
import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ThemeResolver; import org.springframework.web.servlet.ThemeResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ -16,6 +17,7 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver; import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import org.springframework.web.servlet.theme.CookieThemeResolver; import org.springframework.web.servlet.theme.CookieThemeResolver;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor; import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.SpringTemplateEngine;
@ -70,6 +72,7 @@ public class WebConfiguration extends WebMvcConfigurerAdapter {
public MessageSource messageSource() { public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages"); messageSource.setBasename("messages");
messageSource.setFallbackToSystemLocale(false);
return messageSource; return messageSource;
} }
@ -118,4 +121,9 @@ public class WebConfiguration extends WebMvcConfigurerAdapter {
public MultipartResolver multipartResolver() { public MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver(); return new StandardServletMultipartResolver();
} }
@Bean
public HandlerExceptionResolver handlerExceptionResolver() {
return new ExceptionHandlerExceptionResolver();
}
} }

View File

@ -1,9 +1,7 @@
package com.baeldung.spring.dispatcher.servlet.web; package com.baeldung.spring.dispatcher.servlet.web;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -12,9 +10,6 @@ import javax.servlet.http.HttpServletRequest;
public class GlobalDefaultExceptionHandler { public class GlobalDefaultExceptionHandler {
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception { public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception {
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
throw e;
}
ModelAndView modelAndView = new ModelAndView(); ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("exception", e); modelAndView.addObject("exception", e);
modelAndView.addObject("url", request.getRequestURL()); modelAndView.addObject("url", request.getRequestURL());