spring-boot中的json日期格式 [英] json date format in spring-boot
本文介绍了spring-boot中的json日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是spring-boot,我有一个像这样定义的实体类
I am using spring-boot and I have an entity class defined something like this
import org.joda.time.LocalDateTime;
@Entity
public class Project {
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime start_date;
...
...
}
当这个class被转换为JSON,该字段被转换为以下字符串表示
When this class is converted to JSON, the field gets converted to the following string representation
{"start_date":[2014,11,15,0,0,0,0],...., ...}
我想要让json响应为 yyyy-MM-dd
。
I want to have the json response as yyyy-MM-dd
.
我试过 @ DateTimeFormat(iso = ISO.DATE)
注释,但也没有帮助。
I tried the @DateTimeFormat(iso = ISO.DATE)
annotation and that did not help either.
是否有一种简单的方法可以将此转换为正确的json格式?
Is there an easy way to do this conversion to proper json format ?
推荐答案
将日期格式设置为 yyyy-MM需要做三件事-dd
:
- 在
com.fasterxml.jackson.datatype上添加依赖项:杰克逊 - 数据类型 - 约达
。从你目前得到的输出来看,我认为你可能已经有这种依赖。 - 配置Jackson不要通过添加
spring将日期格式化为时间戳。 jackson.serialization.write-dates-as-timestamps:false
到你的application.properties
文件。 - 使用
@JsonFormat(pattern =yyyy-MM-dd)
<注释LocalDataTime
字段或getter方法li>
- Add a dependency on
com.fasterxml.jackson.datatype:jackson-datatype-joda
. Judging by the output you're getting at the moment, I think you may already have this dependency. - Configure Jackson not to format dates as timestamps by adding
spring.jackson.serialization.write-dates-as-timestamps: false
to yourapplication.properties
file. - Annotate the
LocalDataTime
field or getter method with@JsonFormat(pattern="yyyy-MM-dd")
注意:您需要使用Spring Boot 1.2才能使第2步工作。
Note: You'll need to use Spring Boot 1.2 for step 2 to work.
这篇关于spring-boot中的json日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文