diff --git a/spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/WebConfiguration.java b/spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/WebConfiguration.java index efc2461050..419c1a2908 100644 --- a/spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/WebConfiguration.java +++ b/spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/WebConfiguration.java @@ -8,6 +8,7 @@ import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.ui.context.support.ResourceBundleThemeSource; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver; +import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.ThemeResolver; 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.i18n.CookieLocaleResolver; 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.ThemeChangeInterceptor; import org.thymeleaf.spring4.SpringTemplateEngine; @@ -70,6 +72,7 @@ public class WebConfiguration extends WebMvcConfigurerAdapter { public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages"); + messageSource.setFallbackToSystemLocale(false); return messageSource; } @@ -118,4 +121,9 @@ public class WebConfiguration extends WebMvcConfigurerAdapter { public MultipartResolver multipartResolver() { return new StandardServletMultipartResolver(); } + + @Bean + public HandlerExceptionResolver handlerExceptionResolver() { + return new ExceptionHandlerExceptionResolver(); + } } diff --git a/spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/web/GlobalDefaultExceptionHandler.java b/spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/web/GlobalDefaultExceptionHandler.java index f25fe11b53..f25eb601a7 100644 --- a/spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/web/GlobalDefaultExceptionHandler.java +++ b/spring-dispatcher-servlet/src/main/java/com/baeldung/spring/dispatcher/servlet/web/GlobalDefaultExceptionHandler.java @@ -1,9 +1,7 @@ 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.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; @@ -12,9 +10,6 @@ import javax.servlet.http.HttpServletRequest; public class GlobalDefaultExceptionHandler { @ExceptionHandler(Exception.class) public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception { - if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { - throw e; - } ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("exception", e); modelAndView.addObject("url", request.getRequestURL());