From 0f738be11477cef24295a2acd5b99119f54fd784 Mon Sep 17 00:00:00 2001 From: Marcel Overdijk Date: Sun, 4 May 2014 10:52:09 +0200 Subject: [PATCH] Add specifying (fixed) date format via application properties Fixes gh-778, Fixes gh-755 --- .../web/WebMvcAutoConfiguration.java | 11 +++++++ .../web/WebMvcAutoConfigurationTests.java | 33 +++++++++++++++++++ .../appendix-application-properties.adoc | 1 + 3 files changed, 45 insertions(+) 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 b71bbcc710..4ddf6b5a2b 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 @@ -20,6 +20,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.List; import javax.servlet.Servlet; @@ -48,6 +49,7 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.format.Formatter; import org.springframework.format.FormatterRegistry; +import org.springframework.format.datetime.DateFormatter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.util.StringUtils; import org.springframework.validation.DefaultMessageCodesResolver; @@ -143,6 +145,9 @@ public class WebMvcAutoConfiguration { @Value("${spring.mvc.locale:}") private String locale = ""; + @Value("${spring.mvc.date-format:}") + private String dateFormat = ""; + @Autowired private ListableBeanFactory beanFactory; @@ -200,6 +205,12 @@ public class WebMvcAutoConfiguration { return new FixedLocaleResolver(StringUtils.parseLocaleString(this.locale)); } + @Bean + @ConditionalOnExpression("'${spring.mvc.date-format:}' != ''") + public Formatter dateFormatter() { + return new DateFormatter(this.dateFormat); + } + @Override public MessageCodesResolver getMessageCodesResolver() { if (this.messageCodesResolverFormat != null) { 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 3e9e5416c0..56d848ef53 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 @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.web; import java.lang.reflect.Field; +import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; @@ -25,6 +26,7 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.joda.time.DateTime; import org.junit.After; import org.junit.Rule; import org.junit.Test; @@ -41,6 +43,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.format.support.FormattingConversionService; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -152,6 +155,7 @@ public class WebMvcAutoConfigurationTests { equalTo((Resource) new ClassPathResource("/foo/"))); } + @Test public void noLocaleResolver() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(AllResources.class, Config.class, @@ -183,6 +187,35 @@ public class WebMvcAutoConfigurationTests { assertThat(locale.toString(), equalTo("en_UK")); } + @Test + public void noDateFormat() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context.register(AllResources.class, Config.class, + WebMvcAutoConfiguration.class, + HttpMessageConvertersAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + FormattingConversionService cs = this.context.getBean(FormattingConversionService.class); + Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); + // formatting cs should use simple toString() + assertThat(cs.convert(date, String.class), equalTo(date.toString())); + } + + @Test + public void overrideDateFormat() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + // set fixed date format + EnvironmentTestUtils.addEnvironment(this.context, "spring.mvc.date-format:dd*MM*yyyy"); + this.context.register(AllResources.class, Config.class, + WebMvcAutoConfiguration.class, + HttpMessageConvertersAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + FormattingConversionService cs = this.context.getBean(FormattingConversionService.class); + Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); + assertThat(cs.convert(date, String.class), equalTo("25*06*1988")); + } + @Test public void noMessageCodesResolver() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); 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 3c6a4ffc53..85c2751fe4 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.date-format= # set fixed date format, e.g. dd/MM/yyyy spring.mvc.message-codes-resolver.format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE spring.view.prefix= # MVC view prefix spring.view.suffix= # ... and suffix