SpringBoot TestRestTemplate and LocalDateTime not

2019-08-22 16:23发布

问题:

I am using LocalDateTime in my model, after including LocalDateTimeDeserializer, converted the bean field to

@NotNull
@Column(name = "created")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime created;

and included the

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

property in the SpringBoot's application.properties file, the application is finally able to deserialize the JSON and show properly like,

    "created": "2018-04-22T21:21:53.025",

But, when I am doing testing, it ignores the WRITE_DATES_AS_TIMESTAMPS flag, I guess and generates an output for the same string date above like,

"created":{"year":2018,"monthValue":4,"month":"APRIL","dayOfMonth":22,"dayOfYear":112,"dayOfWeek":"SUNDAY","hour":21,"minute":23,"second":16,"nano":986000000,"chronology":{"id":"ISO","calendarType":"iso8601"}}

Please, note that including the property in application.properties in test resources folder did not change anything.

My test configuration looks like,

@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@Category(IntegrationTest.class)
public class ApplicationTests {
....

Do you have any idea on what I am doing wrong?

回答1:

I had the same issue and the below solution worked for me,

add the below code in your Applicationtests class

protected MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;

@Autowired
     public void setConverters(HttpMessageConverter<?>[] converters) {
         this.mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter)Arrays.asList(converters).stream()
                 .filter(hmc -> hmc instanceof MappingJackson2HttpMessageConverter)
                 .findAny()              
                 .orElse(null);

         assertNotNull("the JSON message converter must not be null",
                 this.mappingJackson2HttpMessageConverter);

         final ObjectMapper objectMapper = new ObjectMapper();
         final JavaTimeModule javaTimeModule = new JavaTimeModule();
         objectMapper.registerModule(new Jdk8Module());
         objectMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
         mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
     }

If you want to support your own date formats then add the formatters as well, //the below customisation is required if you need to support different date formats

javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeCustomSerializer());
            javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeCustomDeserializer());
             objectMapper.registerModule(javaTimeModule);

Where the custom classes will looks like,

public class LocalDateTimeCustomSerializer extends LocalDateTimeSerializer {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
LocalDateTimeCustomSerializer() {
        this(FORMATTER);
    }

    public LocalDateTimeCustomSerializer(DateTimeFormatter f) {
        super(f);
    }

    @Override
    protected DateTimeFormatter _defaultFormatter() {
        return FORMATTER;
    }

}

and

public class LocalDateTimeCustomDeserializer extends LocalDateTimeDeserializer {

    public LocalDateTimeCustomDeserializer() {
        this(DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss"));
    }

    public LocalDateTimeCustomDeserializer(DateTimeFormatter formatter) {
        super(formatter);
    }
}