【发布时间】:2021-12-22 08:19:36
【问题描述】:
每当我尝试通过邮递员发送此邮件时:
{
"date": "2021-11-05 12:32:32",
"start": "start",
"destination": "destination",
"provider": "provider",
"driver":1,
"vehicule":1
}
我收到错误 400,错误请求,我同时使用了 @restController 和 @requestBody 注释,同时还将内容类型设置为 json。
我在调试器上收到此错误:
2021-11-09 16:57:52.086 WARN 11748 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2021-11-06 12:32:32.0": not a valid representation (error: Failed to parse Date value '2021-11-06 12:32:32.0': Cannot parse date "2021-11-06 12:32:32.0": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2021-11-06 12:32:32.0": not a valid representation (error: Failed to parse Date value '2021-11-06 12:32:32.0': Cannot parse date "2021-11-06 12:32:32.0": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))
at [Source: (PushbackInputStream); line: 3, column: 17] (through reference chain: com.siam.HRAssistTool.Entity.Schedule["date"])]
我不明白我应该如何解决我认为与日期格式相关的问题
当我从 json 正文中删除时间并只留下日期时,我收到此错误:
2021-11-09 17:34:55.418 WARN 11748 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.siam.HRAssistTool.Entity.Vehicule` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (1); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.siam.HRAssistTool.Entity.Vehicule` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (1)
at [Source: (PushbackInputStream); line: 8, column: 20] (through reference chain: com.siam.HRAssistTool.Entity.Schedule["vehicule"])]
我的日程安排实体:
@Entity
public class Schedule implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id ;
private Date date ;
private String Start;
private String destination;
@OneToOne( fetch = FetchType.LAZY)
private Staff driver;
@OneToOne(fetch = FetchType.LAZY)
private Vehicule vehicule;
private String provider;
//constructors, getters and setters
}
我的控制器:
@RestController
public class ScheduleController {
@Autowired
ScheduleService scheduleService;
@PostMapping(value="/schedule/create")
public @ResponseBody String createSchedule( @RequestBody Schedule schedule) {
System.out.println(schedule.toString());
return scheduleService.addSchedule(schedule);
}
//other crud operation
}
【问题讨论】:
-
请添加
Controller以及您的模型类,否则我们无能为力。谢谢!
标签: json spring-boot date post postman