Commit 1cbef022 authored by Marcel Overdijk's avatar Marcel Overdijk Committed by Dave Syer

Add messagecode resolver format based on application property

parent e26e06d5
...@@ -31,7 +31,6 @@ import org.springframework.beans.factory.ListableBeanFactory; ...@@ -31,7 +31,6 @@ import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; 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.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
...@@ -50,6 +49,8 @@ import org.springframework.format.Formatter; ...@@ -50,6 +49,8 @@ import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry; import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils; 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.accept.ContentNegotiationManager;
import org.springframework.web.context.request.RequestContextListener; import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.filter.HiddenHttpMethodFilter; import org.springframework.web.filter.HiddenHttpMethodFilter;
...@@ -135,6 +136,9 @@ public class WebMvcAutoConfiguration { ...@@ -135,6 +136,9 @@ public class WebMvcAutoConfiguration {
@Value("${spring.resources.cachePeriod:}") @Value("${spring.resources.cachePeriod:}")
private Integer cachePeriod; private Integer cachePeriod;
@Value("${spring.mvc.message-codes-resolver.format:}")
private String messageCodesResolverFormat = "";
@Value("${spring.mvc.locale:}") @Value("${spring.mvc.locale:}")
private String locale = ""; private String locale = "";
...@@ -195,6 +199,15 @@ public class WebMvcAutoConfiguration { ...@@ -195,6 +199,15 @@ public class WebMvcAutoConfiguration {
return new FixedLocaleResolver(StringUtils.parseLocaleString(this.locale)); 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 @Override
public void addFormatters(FormatterRegistry registry) { public void addFormatters(FormatterRegistry registry) {
for (Converter<?, ?> converter : getBeansOfType(Converter.class)) { for (Converter<?, ?> converter : getBeansOfType(Converter.class)) {
......
...@@ -16,6 +16,12 @@ ...@@ -16,6 +16,12 @@
package org.springframework.boot.autoconfigure.web; 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.lang.reflect.Field;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
...@@ -43,6 +49,7 @@ import org.springframework.core.io.Resource; ...@@ -43,6 +49,7 @@ import org.springframework.core.io.Resource;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.LocaleResolver;
...@@ -55,12 +62,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl ...@@ -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.resource.ResourceHttpRequestHandler;
import org.springframework.web.servlet.view.AbstractView; 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}. * Tests for {@link WebMvcAutoConfiguration}.
* *
...@@ -180,6 +181,30 @@ public class WebMvcAutoConfigurationTests { ...@@ -180,6 +181,30 @@ public class WebMvcAutoConfigurationTests {
assertThat(locale.toString(), equalTo("en_UK")); 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") @SuppressWarnings("unchecked")
protected Map<String, List<Resource>> getMappingLocations() protected Map<String, List<Resource>> getMappingLocations()
throws IllegalAccessException { throws IllegalAccessException {
......
...@@ -68,6 +68,7 @@ content into your application; rather pick only the properties that you need. ...@@ -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-pretty-print=false # pretty print JSON
http.mappers.json-sort-keys=false # sort keys http.mappers.json-sort-keys=false # sort keys
spring.mvc.locale= # set fixed locale, e.g. en_UK 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.prefix= # MVC view prefix
spring.view.suffix= # ... and suffix spring.view.suffix= # ... and suffix
spring.resources.cache-period= # cache timeouts in headers sent to browser spring.resources.cache-period= # cache timeouts in headers sent to browser
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment