Pra*_*mod 37 java rest spring-hateoas spring-boot
我正在使用spring boot来创建REST应用程序.我有一个DTO,如下所示:
public class Subject {
private String uid;
private String number;
private String initials;
private Date dateOfBirth;
Run Code Online (Sandbox Code Playgroud)
我使用Spring-Hateos并且我的控制器的重新类型是ResponseEntity<Resources<Resource<Subject>>>.我需要以"yyyy-mm-dd"格式显示日期.
Yag*_*ola 66
如果杰克逊与您的应用程序一起将bean序列化为JSON格式,那么您可以使用Jackson anotation @JsonFormat将日期格式化为指定格式.
在您的情况下,如果您需要将日期转换为yyyy-MM-dd格式,则需要@JsonFormat在要应用此格式的字段上方指定.
例如 :
public class Subject {
private String uid;
private String number;
private String initials;
@JsonFormat(pattern="yyyy-MM-dd")
private Date dateOfBirth;
//Other Code
}
Run Code Online (Sandbox Code Playgroud)
来自Docs:
用于配置如何序列化属性值的详细信息的注释.
希望这可以帮助.
Mas*_*ave 32
你很可能意味着"yyyy-MM-dd"小后者'm'意味着分钟节.
你应该做两件事
添加spring.jackson.serialization.write-dates-as-timestamps:false您的application.properties此功能将禁用将日期转换为时间戳,而是使用符合ISO-8601标准的格式
您可以通过使用注释dateOfBirth属性的getter方法来自定义格式@JsonFormat(pattern="yyyy-MM-dd")
从Spring Boot版本1.2.0.RELEASE开始,您可以添加一个属性,application.properties以便为所有类设置默认日期格式spring.jackson.date-format.
对于您的日期格式示例,您可以将此行添加到属性文件中:
spring.jackson.date-format=yyyy-MM-dd
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89535 次 |
| 最近记录: |