About @JsonFormat and @DateFormat issues

Today in debugging web applications, you get the time serialization and de-serialization problem, check the information online are generally about @JsonFormat and @DateFormat

Many online say is @JsonFormat is to turn the object that is serialized string functioning, @ DateFormat string transfer is an object that is deserialized work, and some say @JsonFormat serialization and de-serialization have a role.

In fact, this problem is not simple, in the end who works is conditional.

1, the current client request content-Type is application / json when

  Forwarding the request to the rear end of the object is @JsonFormat work string.

domain file

controller layer

 

requesting postman.

As can be seen from the above, the rear end deserialization Error

Now I put the notes replaced, as follows

public class Test {

    @JsonFormat(pattern = "yyyy-MM-dd")
//    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date date1;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date date2;

    public Date getDate1() {
        return date1;
    }

    public void setDate1(Date date1) {
        this.date1 = date1;
    }

    public Date getDate2() {
        return date2;
    }

    public void setDate2(Date date2) {
        this.date2 = date2;
    }

}

此时后端能调通,且返回的结果时间格式是正确的,我们可以发现这种情况下@JsonFormat在反序列化和序列化过程都起作用了。

2、当请求类型:content-Typeform-data

此时domain还和上面一样,请求url换func1,因为

 

Guess you like

Origin www.cnblogs.com/lqwh/p/11229031.html