Set ResponseStatusExceptionResolver.messageSource in the MVC Java config
Issue: SPR-12380
(cherry picked from commit 17b9bde)
This commit is contained in:
@@ -124,16 +124,17 @@ import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolv
|
|||||||
* {@link ExceptionHandlerExceptionResolver} are configured with default
|
* {@link ExceptionHandlerExceptionResolver} are configured with default
|
||||||
* instances of the following by default:
|
* instances of the following by default:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>A {@link ContentNegotiationManager}
|
* <li>a {@link ContentNegotiationManager}
|
||||||
* <li>A {@link DefaultFormattingConversionService}
|
* <li>a {@link DefaultFormattingConversionService}
|
||||||
* <li>A {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}
|
* <li>a {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}
|
||||||
* if a JSR-303 implementation is available on the classpath
|
* if a JSR-303 implementation is available on the classpath
|
||||||
* <li>A range of {@link HttpMessageConverter}s depending on the 3rd party
|
* <li>a range of {@link HttpMessageConverter}s depending on the third-party
|
||||||
* libraries available on the classpath.
|
* libraries available on the classpath.
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @author Brian Clozel
|
* @author Brian Clozel
|
||||||
|
* @author Sebastien Deleuze
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see EnableWebMvc
|
* @see EnableWebMvc
|
||||||
* @see WebMvcConfigurer
|
* @see WebMvcConfigurer
|
||||||
@@ -663,13 +664,16 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
|||||||
*/
|
*/
|
||||||
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
|
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
|
||||||
ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver();
|
ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver();
|
||||||
exceptionHandlerExceptionResolver.setApplicationContext(this.applicationContext);
|
|
||||||
exceptionHandlerExceptionResolver.setContentNegotiationManager(mvcContentNegotiationManager());
|
exceptionHandlerExceptionResolver.setContentNegotiationManager(mvcContentNegotiationManager());
|
||||||
exceptionHandlerExceptionResolver.setMessageConverters(getMessageConverters());
|
exceptionHandlerExceptionResolver.setMessageConverters(getMessageConverters());
|
||||||
|
exceptionHandlerExceptionResolver.setApplicationContext(this.applicationContext);
|
||||||
exceptionHandlerExceptionResolver.afterPropertiesSet();
|
exceptionHandlerExceptionResolver.afterPropertiesSet();
|
||||||
|
|
||||||
exceptionResolvers.add(exceptionHandlerExceptionResolver);
|
exceptionResolvers.add(exceptionHandlerExceptionResolver);
|
||||||
exceptionResolvers.add(new ResponseStatusExceptionResolver());
|
|
||||||
|
ResponseStatusExceptionResolver responseStatusExceptionResolver = new ResponseStatusExceptionResolver();
|
||||||
|
responseStatusExceptionResolver.setMessageSource(this.applicationContext);
|
||||||
|
exceptionResolvers.add(responseStatusExceptionResolver);
|
||||||
|
|
||||||
exceptionResolvers.add(new DefaultHandlerExceptionResolver());
|
exceptionResolvers.add(new DefaultHandlerExceptionResolver());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,28 +17,35 @@
|
|||||||
package org.springframework.web.servlet.config.annotation;
|
package org.springframework.web.servlet.config.annotation;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
import org.springframework.context.annotation.ScopedProxyMode;
|
import org.springframework.context.annotation.ScopedProxyMode;
|
||||||
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
import org.springframework.context.support.StaticMessageSource;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||||
import org.springframework.format.support.FormattingConversionService;
|
import org.springframework.format.support.FormattingConversionService;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||||
|
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||||
import org.springframework.mock.web.test.MockServletContext;
|
import org.springframework.mock.web.test.MockServletContext;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.validation.Validator;
|
import org.springframework.validation.Validator;
|
||||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
@@ -63,6 +70,7 @@ import static org.junit.Assert.*;
|
|||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Sebastien Deleuze
|
||||||
*/
|
*/
|
||||||
public class WebMvcConfigurationSupportTests {
|
public class WebMvcConfigurationSupportTests {
|
||||||
|
|
||||||
@@ -172,7 +180,6 @@ public class WebMvcConfigurationSupportTests {
|
|||||||
this.wac.getBean("handlerExceptionResolver", HandlerExceptionResolverComposite.class);
|
this.wac.getBean("handlerExceptionResolver", HandlerExceptionResolverComposite.class);
|
||||||
|
|
||||||
assertEquals(0, compositeResolver.getOrder());
|
assertEquals(0, compositeResolver.getOrder());
|
||||||
|
|
||||||
List<HandlerExceptionResolver> expectedResolvers = compositeResolver.getExceptionResolvers();
|
List<HandlerExceptionResolver> expectedResolvers = compositeResolver.getExceptionResolvers();
|
||||||
|
|
||||||
assertEquals(ExceptionHandlerExceptionResolver.class, expectedResolvers.get(0).getClass());
|
assertEquals(ExceptionHandlerExceptionResolver.class, expectedResolvers.get(0).getClass());
|
||||||
@@ -181,6 +188,18 @@ public class WebMvcConfigurationSupportTests {
|
|||||||
|
|
||||||
ExceptionHandlerExceptionResolver eher = (ExceptionHandlerExceptionResolver) expectedResolvers.get(0);
|
ExceptionHandlerExceptionResolver eher = (ExceptionHandlerExceptionResolver) expectedResolvers.get(0);
|
||||||
assertNotNull(eher.getApplicationContext());
|
assertNotNull(eher.getApplicationContext());
|
||||||
|
|
||||||
|
LocaleContextHolder.setLocale(Locale.ENGLISH);
|
||||||
|
try {
|
||||||
|
ResponseStatusExceptionResolver rser = (ResponseStatusExceptionResolver) expectedResolvers.get(1);
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
rser.resolveException(request, response, this.wac.getBean(TestController.class), new UserAlreadyExistsException());
|
||||||
|
assertEquals("User already exists!", response.getErrorMessage());
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
LocaleContextHolder.resetLocaleContext();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -192,6 +211,13 @@ public class WebMvcConfigurationSupportTests {
|
|||||||
public TestController testController() {
|
public TestController testController() {
|
||||||
return new TestController();
|
return new TestController();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageSource messageSource() {
|
||||||
|
StaticMessageSource messageSource = new StaticMessageSource();
|
||||||
|
messageSource.addMessage("exception.user.exists", Locale.ENGLISH, "User already exists!");
|
||||||
|
return messageSource;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -229,4 +255,9 @@ public class WebMvcConfigurationSupportTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "exception.user.exists")
|
||||||
|
public static class UserAlreadyExistsException extends RuntimeException {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user