SpringBoot TestRestTemplate 和 LocalDateTime 不起作用 [英] SpringBoot TestRestTemplate and LocalDateTime not working

查看:341
本文介绍了SpringBoot TestRestTemplate 和 LocalDateTime 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型中使用 LocalDateTime,在包含 LocalDateTimeDeserializer 之后,将 bean 字段转换为

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;

并包括

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

SpringBoot 的 application.properties 文件中的属性,应用程序终于能够反序列化 JSON 并正常显示,

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

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

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

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

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?

推荐答案

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

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

在您的 Applicationtests 类中添加以下代码

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

自定义类的样子,

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

这篇关于SpringBoot TestRestTemplate 和 LocalDateTime 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆