From 1cbef022360a88f8cd45b838c2d615a8450465f9 Mon Sep 17 00:00:00 2001 From: Marcel Overdijk Date: Mon, 28 Apr 2014 21:00:07 +0200 Subject: [PATCH] Add messagecode resolver format based on application property --- .../web/WebMvcAutoConfiguration.java | 15 +++++++- .../web/WebMvcAutoConfigurationTests.java | 37 ++++++++++++++++--- .../appendix-application-properties.adoc | 1 + 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index 6e774efcd08..e6b9c503e59 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -31,7 +31,6 @@ import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; @@ -50,6 +49,8 @@ import org.springframework.format.Formatter; import org.springframework.format.FormatterRegistry; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.util.StringUtils; +import org.springframework.validation.DefaultMessageCodesResolver; +import org.springframework.validation.MessageCodesResolver; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.context.request.RequestContextListener; import org.springframework.web.filter.HiddenHttpMethodFilter; @@ -135,6 +136,9 @@ public class WebMvcAutoConfiguration { @Value("${spring.resources.cachePeriod:}") private Integer cachePeriod; + @Value("${spring.mvc.message-codes-resolver.format:}") + private String messageCodesResolverFormat = ""; + @Value("${spring.mvc.locale:}") private String locale = ""; @@ -195,6 +199,15 @@ public class WebMvcAutoConfiguration { return new FixedLocaleResolver(StringUtils.parseLocaleString(this.locale)); } + @Bean + @ConditionalOnMissingBean(MessageCodesResolver.class) + @ConditionalOnExpression("'${spring.mvc.message-codes-resolver.format:}' != ''") + public MessageCodesResolver messageCodesResolver() { + DefaultMessageCodesResolver resolver = new DefaultMessageCodesResolver(); + resolver.setMessageCodeFormatter(DefaultMessageCodesResolver.Format.valueOf(messageCodesResolverFormat)); + return resolver; + } + @Override public void addFormatters(FormatterRegistry registry) { for (Converter converter : getBeansOfType(Converter.class)) { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java index eeef548d041..9a863f85f05 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java @@ -16,6 +16,12 @@ package org.springframework.boot.autoconfigure.web; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; + import java.lang.reflect.Field; import java.util.LinkedHashMap; import java.util.List; @@ -43,6 +49,7 @@ import org.springframework.core.io.Resource; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; +import org.springframework.validation.MessageCodesResolver; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.LocaleResolver; @@ -55,12 +62,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.view.AbstractView; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.instanceOf; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; - /** * Tests for {@link WebMvcAutoConfiguration}. * @@ -180,6 +181,30 @@ public class WebMvcAutoConfigurationTests { assertThat(locale.toString(), equalTo("en_UK")); } + @Test(expected = NoSuchBeanDefinitionException.class) + public void noMessageCodeResolver() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context.register(AllResources.class, Config.class, + WebMvcAutoConfiguration.class, + HttpMessageConvertersAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + this.context.getBean(MessageCodesResolver.class); + } + + @Test + public void overrideMessageCodesFormat() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.mvc.message-codes-resolver.format:POSTFIX_ERROR_CODE"); + this.context.register(AllResources.class, Config.class, + WebMvcAutoConfiguration.class, + HttpMessageConvertersAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + this.context.getBean(MessageCodesResolver.class); + } + @SuppressWarnings("unchecked") protected Map> getMappingLocations() throws IllegalAccessException { diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 15abb1e0575..a9232a8f958 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -68,6 +68,7 @@ content into your application; rather pick only the properties that you need. http.mappers.json-pretty-print=false # pretty print JSON http.mappers.json-sort-keys=false # sort keys spring.mvc.locale= # set fixed locale, e.g. en_UK + spring.mvc.message-codes-resolver.format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE spring.view.prefix= # MVC view prefix spring.view.suffix= # ... and suffix spring.resources.cache-period= # cache timeouts in headers sent to browser