SpringBoot TestRestTemplate和LocalDateTime无法正常工作

时间:2018-04-22 17:37:34

标签: spring-boot resttemplate spring-test spring-rest localdate

我在我的模型中使用LocalDateTime,包括LocalDateTimeDeserializer, 将bean字段转换为

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

并包含

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
在SpringBoot的application.properties文件中的

属性,应用程序终于能够反序列化JSON并正确显示,

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

但是,当我正在进行测试时,它会忽略WRITE_DATES_AS_TIMESTAMPS标志,我猜并为上面相同的字符串日期生成一个输出,如

"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"}}

请注意,在test resources文件夹中的application.properties中包含该属性并没有改变任何内容。

我的测试配置如下,

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

你对我做错了什么有任何想法吗?

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,以下解决方案对我有用,

在Applicationtests类中添加以下代码

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);
     }

如果您想支持自己的日期格式,请同时添加格式化程序, //如果您需要支持不同的日期格式,则需要以下自定义

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

自定义类的位置,

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;
    }

}

public class LocalDateTimeCustomDeserializer extends LocalDateTimeDeserializer {

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

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