Deprecate auto-configuration for Joda-Time

Closes gh-17419
pull/17462/head
Andy Wilkinson 5 years ago
parent 9f253603db
commit 3e6c15c451

@ -108,6 +108,7 @@ public class JacksonAutoConfiguration {
} }
@Deprecated
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Jackson2ObjectMapperBuilder.class, DateTime.class, DateTimeSerializer.class, @ConditionalOnClass({ Jackson2ObjectMapperBuilder.class, DateTime.class, DateTimeSerializer.class,
JacksonJodaDateFormat.class }) JacksonJodaDateFormat.class })
@ -117,6 +118,8 @@ public class JacksonAutoConfiguration {
@Bean @Bean
SimpleModule jodaDateTimeSerializationModule(JacksonProperties jacksonProperties) { SimpleModule jodaDateTimeSerializationModule(JacksonProperties jacksonProperties) {
logger.warn("Auto-configuration of Jackson's Joda-Time integration is deprecated in favor of using "
+ "java.time (JSR-310).");
SimpleModule module = new SimpleModule(); SimpleModule module = new SimpleModule();
JacksonJodaDateFormat jacksonJodaFormat = getJacksonJodaDateFormat(jacksonProperties); JacksonJodaDateFormat jacksonJodaFormat = getJacksonJodaDateFormat(jacksonProperties);
if (jacksonJodaFormat != null) { if (jacksonJodaFormat != null) {

@ -31,6 +31,7 @@ import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
/** /**
* Configuration properties to configure Jackson. * Configuration properties to configure Jackson.
@ -117,6 +118,10 @@ public class JacksonProperties {
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
} }
@Deprecated
@DeprecatedConfigurationProperty(replacement = "dateFormat",
reason = "Auto-configuration for Jackson's Joda-Time integration is "
+ "deprecated in favor of its Java 8 Time integration")
public String getJodaDateTimeFormat() { public String getJodaDateTimeFormat() {
return this.jodaDateTimeFormat; return this.jodaDateTimeFormat;
} }

@ -19,6 +19,8 @@ package org.springframework.boot.autoconfigure.web.format;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle; import java.time.format.ResolverStyle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.format.DateTimeFormatterBuilder; import org.joda.time.format.DateTimeFormatterBuilder;
import org.springframework.format.datetime.DateFormatter; import org.springframework.format.datetime.DateFormatter;
@ -49,9 +51,12 @@ public class WebConversionService extends DefaultFormattingConversionService {
private static final boolean JSR_354_PRESENT = ClassUtils.isPresent("javax.money.MonetaryAmount", private static final boolean JSR_354_PRESENT = ClassUtils.isPresent("javax.money.MonetaryAmount",
WebConversionService.class.getClassLoader()); WebConversionService.class.getClassLoader());
@Deprecated
private static final boolean JODA_TIME_PRESENT = ClassUtils.isPresent("org.joda.time.LocalDate", private static final boolean JODA_TIME_PRESENT = ClassUtils.isPresent("org.joda.time.LocalDate",
WebConversionService.class.getClassLoader()); WebConversionService.class.getClassLoader());
private static final Log logger = LogFactory.getLog(WebConversionService.class);
private final String dateFormat; private final String dateFormat;
/** /**
@ -93,7 +98,9 @@ public class WebConversionService extends DefaultFormattingConversionService {
dateTime.registerFormatters(this); dateTime.registerFormatters(this);
} }
@Deprecated
private void registerJodaTime() { private void registerJodaTime() {
logger.warn("Auto-configuration of Joda-Time formatters is deprecated in favor of using java.time (JSR-310).");
JodaTimeFormatterRegistrar jodaTime = new JodaTimeFormatterRegistrar(); JodaTimeFormatterRegistrar jodaTime = new JodaTimeFormatterRegistrar();
if (this.dateFormat != null) { if (this.dateFormat != null) {
jodaTime.setDateFormatter(new DateTimeFormatterBuilder().appendPattern(this.dateFormat).toFormatter()); jodaTime.setDateFormatter(new DateTimeFormatterBuilder().appendPattern(this.dateFormat).toFormatter());

@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.gson; package org.springframework.boot.autoconfigure.gson;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -26,7 +28,6 @@ import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy; import com.google.gson.LongSerializationPolicy;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -198,9 +199,8 @@ class GsonAutoConfigurationTests {
void customDateFormat() { void customDateFormat() {
this.contextRunner.withPropertyValues("spring.gson.date-format:H").run((context) -> { this.contextRunner.withPropertyValues("spring.gson.date-format:H").run((context) -> {
Gson gson = context.getBean(Gson.class); Gson gson = context.getBean(Gson.class);
DateTime dateTime = new DateTime(1988, 6, 25, 20, 30); ZonedDateTime dateTime = ZonedDateTime.of(1988, 6, 25, 20, 30, 0, 0, ZoneId.systemDefault());
Date date = dateTime.toDate(); assertThat(gson.toJson(Date.from(dateTime.toInstant()))).isEqualTo("\"20\"");
assertThat(gson.toJson(date)).isEqualTo("\"20\"");
}); });
} }

@ -80,6 +80,7 @@ class JacksonAutoConfigurationTests {
.withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class)); .withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class));
@Test @Test
@Deprecated
void registersJodaModuleAutomatically() { void registersJodaModuleAutomatically() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
ObjectMapper objectMapper = context.getBean(ObjectMapper.class); ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
@ -116,6 +117,7 @@ class JacksonAutoConfigurationTests {
} }
@Test @Test
@Deprecated
void customJodaDateTimeFormat() throws Exception { void customJodaDateTimeFormat() throws Exception {
this.contextRunner.withPropertyValues("spring.jackson.date-format:yyyyMMddHHmmss", this.contextRunner.withPropertyValues("spring.jackson.date-format:yyyyMMddHHmmss",
"spring.jackson.joda-date-time-format:yyyy-MM-dd HH:mm:ss").run((context) -> { "spring.jackson.joda-date-time-format:yyyy-MM-dd HH:mm:ss").run((context) -> {
@ -336,6 +338,7 @@ class JacksonAutoConfigurationTests {
} }
@Test @Test
@Deprecated
void customLocaleWithJodaTime() throws JsonProcessingException { void customLocaleWithJodaTime() throws JsonProcessingException {
this.contextRunner.withPropertyValues("spring.jackson.locale:de_DE", "spring.jackson.date-format:zzzz", this.contextRunner.withPropertyValues("spring.jackson.locale:de_DE", "spring.jackson.date-format:zzzz",
"spring.jackson.serialization.write-dates-with-zone-id:true").run((context) -> { "spring.jackson.serialization.write-dates-with-zone-id:true").run((context) -> {

@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.web.format; package org.springframework.boot.autoconfigure.web.format;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date; import java.util.Date;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -33,14 +35,24 @@ import static org.assertj.core.api.Assertions.assertThat;
class WebConversionServiceTests { class WebConversionServiceTests {
@Test @Test
void customDateFormat() { void customDateFormatWithJavaUtilDate() {
customDateFormat(Date.from(ZonedDateTime.of(2018, 1, 1, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant()));
}
@Test
@Deprecated
void customDateFormatWithJodaTime() {
customDateFormat(LocalDate.fromDateFields(new DateTime(2018, 1, 1, 20, 30).toDate()));
}
@Test
void customDateFormatWithJavaTime() {
customDateFormat(java.time.LocalDate.of(2018, 1, 1));
}
private void customDateFormat(Object input) {
WebConversionService conversionService = new WebConversionService("dd*MM*yyyy"); WebConversionService conversionService = new WebConversionService("dd*MM*yyyy");
Date date = new DateTime(2018, 1, 1, 20, 30).toDate(); assertThat(conversionService.convert(input, String.class)).isEqualTo("01*01*2018");
assertThat(conversionService.convert(date, String.class)).isEqualTo("01*01*2018");
LocalDate jodaDate = LocalDate.fromDateFields(date);
assertThat(conversionService.convert(jodaDate, String.class)).isEqualTo("01*01*2018");
java.time.LocalDate localDate = java.time.LocalDate.of(2018, 1, 1);
assertThat(conversionService.convert(localDate, String.class)).isEqualTo("01*01*2018");
} }
@Test @Test

@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.web.reactive; package org.springframework.boot.autoconfigure.web.reactive;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -26,7 +28,6 @@ import java.util.concurrent.TimeUnit;
import javax.validation.ValidatorFactory; import javax.validation.ValidatorFactory;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -187,7 +188,7 @@ class WebFluxAutoConfigurationTests {
void noDateFormat() { void noDateFormat() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
FormattingConversionService conversionService = context.getBean(FormattingConversionService.class); FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); Date date = Date.from(ZonedDateTime.of(1988, 6, 25, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant());
// formatting conversion service should use simple toString() // formatting conversion service should use simple toString()
assertThat(conversionService.convert(date, String.class)).isEqualTo(date.toString()); assertThat(conversionService.convert(date, String.class)).isEqualTo(date.toString());
}); });
@ -197,7 +198,7 @@ class WebFluxAutoConfigurationTests {
void overrideDateFormat() { void overrideDateFormat() {
this.contextRunner.withPropertyValues("spring.webflux.date-format:dd*MM*yyyy").run((context) -> { this.contextRunner.withPropertyValues("spring.webflux.date-format:dd*MM*yyyy").run((context) -> {
FormattingConversionService conversionService = context.getBean(FormattingConversionService.class); FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); Date date = Date.from(ZonedDateTime.of(1988, 6, 25, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant());
assertThat(conversionService.convert(date, String.class)).isEqualTo("25*06*1988"); assertThat(conversionService.convert(date, String.class)).isEqualTo("25*06*1988");
}); });
} }

@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.web.servlet; package org.springframework.boot.autoconfigure.web.servlet;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -30,7 +32,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.ValidatorFactory; import javax.validation.ValidatorFactory;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -321,7 +322,7 @@ class WebMvcAutoConfigurationTests {
void noDateFormat() { void noDateFormat() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
FormattingConversionService conversionService = context.getBean(FormattingConversionService.class); FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); Date date = Date.from(ZonedDateTime.of(1988, 6, 25, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant());
// formatting conversion service should use simple toString() // formatting conversion service should use simple toString()
assertThat(conversionService.convert(date, String.class)).isEqualTo(date.toString()); assertThat(conversionService.convert(date, String.class)).isEqualTo(date.toString());
}); });
@ -331,7 +332,7 @@ class WebMvcAutoConfigurationTests {
void overrideDateFormat() { void overrideDateFormat() {
this.contextRunner.withPropertyValues("spring.mvc.date-format:dd*MM*yyyy").run((context) -> { this.contextRunner.withPropertyValues("spring.mvc.date-format:dd*MM*yyyy").run((context) -> {
FormattingConversionService conversionService = context.getBean(FormattingConversionService.class); FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); Date date = Date.from(ZonedDateTime.of(1988, 6, 25, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant());
assertThat(conversionService.convert(date, String.class)).isEqualTo("25*06*1988"); assertThat(conversionService.convert(date, String.class)).isEqualTo("25*06*1988");
}); });
} }

Loading…
Cancel
Save