我已经创建了如下的Entity列
@CreatedDate
@Column(name = "create_ts", nullable = false, updatable = false)
private LocalDateTime createdDate;
映射到列类型为
的Postgres表timestamp(0) without time zone
插入在表
中以正确的格式运行良好2022-12-01 01:24:35
在api上执行curl操作时,
curl -X GET "http://localhost:8080/api/v1/profile/111111"
列数据如下所示。
{"userId":111111,"createdDate":[2022,12,1,1,24,35],"lastModifiedDate":[2022,12,1,1,24,35]}
我的应用程序。yml低于
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql=true:
控制器
@GetMapping(path = "/{dsid}", produces = {APPLICATION_JSON_VALUE})
public ResponseEntity<UserProfile> getUserProfile(@PathVariable Long dsid){
Optional<UserProfile> userProfile = userProfService.getUserProfile(dsid);
if (!userProfile.isPresent()) {
logger.error("User profile not set for user with id: " + dsid);
throw new ResourceNotFoundException("Profile", "id", dsid);
}
return new ResponseEntity<>(userProfService.getUserProfile(id).get(), HttpStatus.OK);
我在这里做错了什么?
解决方案是用
注释实体字段createdDate@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
添加注释后的响应。
{
"id": 123,
"userPreference": {
"theme": "dark",
"landing": "myprojects",
"projects": "112,333,222"
},
"createdDate": "2022-12-02 20:28:23",
"lastModifiedDate": "2022-12-02 20:28:23"
}