使用spring boot在json输出中的日期格式
我正在使用spring boot来创建REST应用程序。我有一个DTO,如下所示:
I am working on spring boot for creating a REST application. And I have a DTO as shown below:
public class Subject {
private String uid;
private String number;
private String initials;
private Date dateOfBirth;
我使用Spring-Hateos并且我的控制器的重新类型是 ResponseEntity< ;资源与LT;资源与LT受试者GT;>>
。我需要以yyyy-mm-dd格式显示日期。
And I use Spring-Hateos and the reurn type of my controller is ResponseEntity<Resources<Resource<Subject>>>
. I need the date to be displayed in the "yyyy-mm-dd" format.
如果你让杰克逊与你合作应用程序将您的bean序列化为JSON格式,然后您可以使用Jackson anotation @ JsonFormat 将您的日期格式化为指定格式。
如果您需要将日期转换为 yyyy-MM-dd
格式,您需要在要应用此格式的字段上方指定 @JsonFormat
。
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.
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.
例如:
public class Subject {
private String uid;
private String number;
private String initials;
@JsonFormat(pattern="yyyy-MM-dd")
private Date dateOfBirth;
//Other Code
}
来自Docs:
注释,用于配置如何序列化属性
的值的详细信息。
annotation used for configuring details of how values of properties are to be serialized.
希望这有帮助。