|
31 | 31 | import org.springframework.beans.DirectFieldAccessor;
|
32 | 32 | import org.springframework.beans.factory.BeanFactoryUtils;
|
33 | 33 | import org.springframework.context.ApplicationContext;
|
| 34 | +import org.springframework.context.MessageSource; |
34 | 35 | import org.springframework.context.annotation.Bean;
|
35 | 36 | import org.springframework.context.annotation.Configuration;
|
36 | 37 | import org.springframework.context.annotation.Scope;
|
37 | 38 | import org.springframework.context.annotation.ScopedProxyMode;
|
| 39 | +import org.springframework.context.i18n.LocaleContextHolder; |
| 40 | +import org.springframework.context.support.StaticMessageSource; |
38 | 41 | import org.springframework.core.Ordered;
|
39 | 42 | import org.springframework.core.convert.ConversionService;
|
40 | 43 | import org.springframework.format.annotation.DateTimeFormat;
|
41 | 44 | import org.springframework.format.annotation.DateTimeFormat.ISO;
|
42 | 45 | import org.springframework.format.support.FormattingConversionService;
|
43 | 46 | import org.springframework.http.HttpEntity;
|
| 47 | +import org.springframework.http.HttpStatus; |
44 | 48 | import org.springframework.http.converter.HttpMessageConverter;
|
45 | 49 | import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
|
46 | 50 | import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
|
47 | 51 | import org.springframework.mock.web.test.MockHttpServletRequest;
|
| 52 | +import org.springframework.mock.web.test.MockHttpServletResponse; |
48 | 53 | import org.springframework.mock.web.test.MockServletContext;
|
49 | 54 | import org.springframework.stereotype.Controller;
|
50 | 55 | import org.springframework.util.AntPathMatcher;
|
|
53 | 58 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
54 | 59 | import org.springframework.web.bind.annotation.PathVariable;
|
55 | 60 | import org.springframework.web.bind.annotation.RequestMapping;
|
| 61 | +import org.springframework.web.bind.annotation.ResponseStatus; |
56 | 62 | import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
57 | 63 | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
58 | 64 | import org.springframework.web.method.support.CompositeUriComponentsContributor;
|
@@ -219,6 +225,19 @@ public void handlerExceptionResolver() throws Exception {
|
219 | 225 | List<Object> interceptors = (List<Object>) fieldAccessor.getPropertyValue("responseBodyAdvice");
|
220 | 226 | assertEquals(1, interceptors.size());
|
221 | 227 | assertEquals(JsonViewResponseBodyAdvice.class, interceptors.get(0).getClass());
|
| 228 | + |
| 229 | + LocaleContextHolder.setLocale(Locale.ENGLISH); |
| 230 | + try { |
| 231 | + ResponseStatusExceptionResolver rser = (ResponseStatusExceptionResolver) expectedResolvers.get(1); |
| 232 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); |
| 233 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 234 | + rser.resolveException(request, response, context.getBean(TestController.class), new UserAlreadyExistsException()); |
| 235 | + assertEquals("User already exists!", response.getErrorMessage()); |
| 236 | + } |
| 237 | + finally { |
| 238 | + LocaleContextHolder.resetLocaleContext(); |
| 239 | + } |
| 240 | + |
222 | 241 | }
|
223 | 242 |
|
224 | 243 | @Test
|
@@ -289,6 +308,13 @@ public static class WebConfig {
|
289 | 308 | public TestController testController() {
|
290 | 309 | return new TestController();
|
291 | 310 | }
|
| 311 | + |
| 312 | + @Bean |
| 313 | + public MessageSource messageSource() { |
| 314 | + StaticMessageSource messageSource = new StaticMessageSource(); |
| 315 | + messageSource.addMessage("exception.user.exists", Locale.ENGLISH, "User already exists!"); |
| 316 | + return messageSource; |
| 317 | + } |
292 | 318 | }
|
293 | 319 |
|
294 | 320 | @Configuration
|
@@ -345,4 +371,8 @@ public void handle() {
|
345 | 371 | }
|
346 | 372 | }
|
347 | 373 |
|
| 374 | + @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "exception.user.exists") |
| 375 | + public static class UserAlreadyExistsException extends RuntimeException { |
| 376 | + } |
| 377 | + |
348 | 378 | }
|
0 commit comments