[Differences in deserialization of time and date between fastjson and Jackson]

Once I encountered that the spring controller interface can accept the serialized time format normally, but it does not work in the code.

The difference between @RequestBody annotation serialized JSON and JSON.parseObject(playloadJson, clazz) in the code
is essentially that the spring@RequestBody annotation serialized JSON uses Jackson, while the code uses fastjson JSON.parseObject

fastjson and Jackson are two different JSON libraries that have some different behaviors when dealing with date formats.

By default, fastjson will try to parse the date string into Java's default date format, ie yyyy-MM-dd HH:mm:ss. If the format of the date string does not match the default format, fastjson will not parse the date correctly.

In contrast, Jackson has a more flexible date parsing mechanism. It can automatically parse dates according to the format of the date string without explicitly specifying the format. Jackson uses a mechanism called "Date Deserialization" to handle date deserialization, which can be parsed according to a variety of common date formats. However, to more precisely control the date parsing behavior, Jackson also provides @JsonFormatannotations, which you can use to specify the format of the date field. This ensures that Jackson does date parsing as you require. Therefore, fastjson needs to specify the date format because its default parsing behavior is based on Java's default date format, while Jackson has a more flexible date parsing mechanism that can automatically identify the date format, but can also be more precisely controlled through annotations.

Because fastjson has a default time format, unusual time formats are encountered during serialization, such as nanosecond-level 2023-07-04T10:00:00.0000000, which cannot be parsed normally. If parsing is required, @JsonFormat specifies the format
@JSONField (format = “yyyy-MM-dd'T'HH:mm:ss.SSSSSSS”)
private Date date;

Guess you like

Origin blog.csdn.net/qq_44961149/article/details/131793023