[英]Date format in the json output using spring boot
I am working on spring boot for creating a REST application. 我正在使用spring boot来创建REST应用程序。 And I have a DTO as shown below:
我有一个DTO,如下所示:
public class Subject {
private String uid;
private String number;
private String initials;
private Date dateOfBirth;
And I use Spring-Hateos and the reurn type of my controller is ResponseEntity<Resources<Resource<Subject>>>
. 我使用Spring-Hateos,我的控制器的reurn类型是
ResponseEntity<Resources<Resource<Subject>>>
。 I need the date to be displayed in the "yyyy-mm-dd" format. 我需要以“yyyy-mm-dd”格式显示日期。
If you have Jackson integeration with your application to serialize your bean to JSON format, then you can use Jackson anotation @JsonFormat to format your date to specified format. 如果杰克逊与您的应用程序一起将bean序列化为JSON格式,那么您可以使用Jackson anotation @JsonFormat将日期格式化为指定格式。
In your case if you need your date into yyyy-MM-dd
format you need to specify @JsonFormat
above your field on which you want to apply this format. 在您的情况下,如果您需要使用
yyyy-MM-dd
格式的日期,则需要在要应用此格式的字段上方指定@JsonFormat
。
For Example : 例如 :
public class Subject {
private String uid;
private String number;
private String initials;
@JsonFormat(pattern="yyyy-MM-dd")
private Date dateOfBirth;
//Other Code
}
From Docs : 来自Docs:
annotation used for configuring details of how values of properties are to be serialized.
用于配置如何序列化属性值的详细信息的注释。
Hope this helps. 希望这可以帮助。
You most likely mean "yyyy-MM-dd" small latter 'm' would imply minutes section. 你很可能意味着“yyyy-MM-dd”小后者'm'意味着分钟节。
You should do two things 你应该做两件事
add spring.jackson.serialization.write-dates-as-timestamps:false
in your application.properties
this will disable converting dates to timestamps and instead use a ISO-8601 compliant format 在
application.properties
添加spring.jackson.serialization.write-dates-as-timestamps:false
这将禁用将日期转换为时间戳,而是使用符合ISO-8601的格式
You can than customize the format by annotating the getter method of you dateOfBirth
property with @JsonFormat(pattern="yyyy-MM-dd")
您可以通过使用
@JsonFormat(pattern="yyyy-MM-dd")
注释dateOfBirth
属性的getter方法来自定义格式
Starting from Spring Boot version 1.2.0.RELEASE , there is a property you can add to your application.properties
to set a default date format to all of your classes spring.jackson.date-format
. 从Spring Boot版本1.2.0.RELEASE开始,有一个属性可以添加到
application.properties
以便为所有类spring.jackson.date-format
设置默认日期spring.jackson.date-format
。
For your date format example, you would add this line to your properties file: 对于您的日期格式示例,您可以将此行添加到属性文件中:
spring.jackson.date-format=yyyy-MM-dd
Reference https://docs.spring.io/spring-boot/docs/1.2.0.RELEASE/reference/html/common-application-properties.html 参考https://docs.spring.io/spring-boot/docs/1.2.0.RELEASE/reference/html/common-application-properties.html
If you want to change the format for all dates you can add a builder customizer. 如果要更改所有日期的格式,可以添加构建器自定义程序。 Here is an example of a bean that converts dates to ISO 8601:
以下是将日期转换为ISO 8601的bean示例:
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.dateFormat(new ISO8601DateFormat());
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.