如何通过Postman发送日期并将其保存到数据库中?

huangapple go评论88阅读模式
英文:

How to send the date via postman and save it to the database?

问题

我想通过Postman发起请求,并在下面的方法中进行接收。出现了一个错误,我理解是由于我错误地处理了日期。问题是:如何通过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;
}
英文:

I want to make a request through Postman and accept it in the method below. An error pops up, as I understand it, due to the fact that I incorrectly process the date. Question: how can I send the date via postman and save it correctly in the database?

Log in IDEA and 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"])]

Inquiry:

{
    "start_date" : "23/02/2020",
    "end_date" : "27/02/2020",
    "id_house" : 2,
    "id_tenant" : 2
}

Code:

@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);
    }

Contract

@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;
}

答案1

得分: 1

在 pom.xml 文件中添加依赖:

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.6.0</version>
    </dependency>

预定义的格式化器:https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

日期格式为 ISO,`yyyy-MM-dd`

以我的请求为例:

    {
        "start_date": "2020-03-12",
        "end_date": "2020-03-19",
        "id_house": 2,
        "id_tenant": 3
    }
英文:

Enabling the dependency in pom.xml file:

&lt;dependency&gt;
    &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
    &lt;artifactId&gt;jackson-datatype-jsr310&lt;/artifactId&gt;
    &lt;version&gt;2.6.0&lt;/version&gt;
&lt;/dependency&gt;

Predefined formatters https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

The date format is ISO, yyyy-MM-dd

Using my request as an example:

{
    &quot;start_date&quot; : &quot;2020-03-12&quot;,
    &quot;end_date&quot; : &quot;2020-03-19&quot;,
    &quot;id_house&quot; : 2,
    &quot;id_tenant&quot; : 3
}

答案2

得分: 0

你需要使用LocalDate.of(int,int,int)来构造一个LocalDate实例。

英文:

You'll need to use LocalDate.of(int,int,int) to construct a LocalDate instance

huangapple
  • 本文由 发表于 2020年10月1日 00:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64141563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定