简体   繁体   English

Jackson ObjectMapper 将本地日期序列化为长字符串

[英]Jackson ObjectMapper serializing local date into a long striing

I am trying to serialize my myBeanObject using jackson Objectmapper.我正在尝试使用 jackson Objectmapper 序列化我的 myBeanObject。 This is what I have:这就是我所拥有的:

Objectmapper m= new Objectmapper()
m.setdateFormat(new SimpledateFormat("yyyy-MM-dd")); 
String json = m.writeValueAsString(myBeanObject);

Problem is a LocalDate variable in my bean is getting serialised into a long String, something like {"year":1970,"month":"JANUARY"...}问题是我的 bean 中的 LocalDate 变量被序列化为一个长字符串,例如 {"year":1970,"month":"JANUARY"...}

I want the LocalDate to become simple like "1970-01-01"我希望 LocalDate 变得像“1970-01-01”一样简单

Can someone please help?有人可以帮忙吗?

Try to add this dependency:尝试添加此依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.8.6</version>
</dependency>

Then setup your ObjectMapper as follows:然后按如下方式设置您的ObjectMapper

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM