java - 如何通过邮递员发送日期并将其保存到数据库中?
问题描述
我想通过 Postman 发出请求,并通过下面的方法接受它。据我了解,由于我错误地处理了日期,会弹出一个错误。问题:如何通过邮递员发送日期并将其正确保存在数据库中?
登录 IDEA 和 Postman:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String "23/02/2020": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '23/02/2020' could not be parsed at index 0; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "23/02/2020": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '23/02/2020' could not be parsed at index 0
at [Source: (PushbackInputStream); line: 2, column: 20] (through reference chain: task.homerent.model.Contract["start_date"])]
询问:
{
"start_date" : "23/02/2020",
"end_date" : "27/02/2020",
"id_house" : 2,
"id_tenant" : 2
}
代码:
@PostMapping("/rent")
@PreAuthorize("hasAuthority('user:write')")
public void homeRent(@RequestBody House house) {
System.out.println("ID арендатора");
System.out.println(house.getId_tenant());
System.out.println("Начало аренды");
int dates = Integer.valueOf(String.valueOf(house.getStart_date()));
LocalDate date = new LocalDate(dates); - an error is displayed LocalDate(int, int, int) has private access in 'java.time.LocalDate'`
System.out.println(date);
}
合同
@Data
@Entity
@Table(name = "contract", schema = "public")
public class Contract {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "id_house")
@ToString.Exclude
@EqualsAndHashCode.Exclude
@JsonIgnore
private House house;
@ManyToOne
@JoinColumn(name = "id_tenant")
@EqualsAndHashCode.Exclude
@ToString.Exclude
@JsonIgnore
private User user;
@Column(name = "end_date")
private LocalDate end_date;
@Column(name = "start_date")
private LocalDate start_date;
}
解决方案
您需要使用LocalDate.of(int,int,int)
来构造一个 LocalDate 实例
推荐阅读
- selenium - SeleniumLibrary.Input 文本中的错误 - 在 if/else 块中使用
- python - Discord bot上的功能已定义错误
- powerbi - 是否有 Power Bi Dax 公式来显示基于多个条件的结果?
- javascript - 如何更改 Laravel + Vue.js 的动态 baseUrl?
- java - 安全的 SimpleDateFormat 解析
- r - for-loop:向量 A 中的条目将被向量 B 中的条目替换
- javascript - 节点 escpos LIBUSB_ERROR_NOT_SUPPORTED
- azure - 了解 Azure Kubernetes 服务 (AKS)
- c# - 如何设置副本数等于 DataGridView 中的数量
- web-component - 开发具有外部依赖关系的 Web 组件?