SpringBoot TestRestTemplate 和 LocalDateTime 不工作

SpringBoot TestRestTemplate and LocalDateTime not working

在包含 LocalDateTimeDeserializer 之后,我在我的模型中使用了 LocalDateTime, 将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"}}

请注意,在测试资源文件夹中 application.properties 中包含 属性 并没有改变任何内容。

我的测试配置看起来像,

@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@Category(IntegrationTest.class)
public class 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);
     }

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

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

自定义 classes 的样子,

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