@JsonFormat 处理 LocalDateTime 失效
·
Failed to convert property value of type ‘java.lang.String’ to required type ‘localdatetime’ for property ‘time’ xxxx
Api 请求参数中,通过需要用时间LocalDateTime,希望通过@JsonFormat() 处理时间格式:
@GetMapping("/user")
public UserDTO getUser(UserDTO name) {
xxx
}
@Data
public class UserDTO {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime time;
}
当我们通过get请求,通过表单的方式提交时,就会报上面按个转换异常参数;
解决方案:改成请求体的方式提交,@RequestBody ;
//get请求
@GetMapping("/user")
public UserDTO getUser(@RequestBody UserDTO name) {
}
// post 请求
@PostMapping("/user")
public UserDTO getUser(@RequestBody UserDTO name) {
}
方案二:同时添加@DateTimeFormat()注解这个是Spring提供的注解
@Data
public class UserDTO {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime time;
}
@DateTimeFormat 用于将请求参数序列化
@JsonFormat() 将返回参数序列话
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)