Set the serialization of Jackson data returned by springboot

Time:2022-5-7

Localdatetime format

Using the following configuration is equivalent to global configuration, so there is no need to add @ jsonformat (pattern = dateut. Yyyy_mm_dd_hh_mm_ss) to the field

If individual fields need different configurations, you can use @ jsonformat (pattern = dateut. Yyyy_mm_dd), @ jsonformat will take precedence, and @ jsonformat annotation can also be used for deserialization

package com. example. mpdemo. config;

Date formatting

#Date type effective

Enumeration formatting

Register the following classes in objectmapper

//Format serialization enumeration

Adjust the structure to be formatted according to the business needs

package com. example. mpdemo. config;

Serializing enumerations with annotations

Add @ jsonvalue annotation to the fields to be serialized. The registration configuration can be omitted. Only the corresponding value can be displayed and cannot be extended. If it needs to be extended, use @ jsonserialize (using = baseenumserializer. Class) annotation

package com. example. mpdemo. enums;

This work adoptsCC agreement, reprint must indicate the author and the link to this article