【发布时间】:2018-02-01 09:42:46
【问题描述】:
例如我有以下课程
public class Profile {
@JsonFormat(pattern = "dd-MM-yyyy")
private LocalDate dateOfBirth;
//seter, getter
}
在控制器中我使用这个类作为@ResponseBody 参数
@RestController
@RequestMapping("/profiles")
public class ProfileController {
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> createProfile(@RequestBody Profile newProfile) {
//...
}
}
如果我使用以下 JSON 向 /profile 发送 POST 请求,它可以正常工作
{
"dateOfBirth": "21-01-1985"
}
如何在不使用@JsonFormat 或任何其他注释注释Profile#dateOfBirth 类的情况下实现此行为?我想将此实体用于不同的日期格式,因此我需要在 Profile 类之外定义日期格式。
【问题讨论】:
-
您能否将 Profile 类中的所有属性设为字符串,然后转换为您计划使用该字段的特定日期格式。
标签: spring-mvc spring-boot spring-restcontroller spring-rest