Springboot is automatically configured for Jackson, and if you need to modify it, there are two ways:
Way one: Through APPLICATION.YML
Configuration Property Description: # #
SPRING.JACKSON.DATE-FORMAT Specifies the date format, such as Yyyy-mm-dd HH:mm:ss, or the fully qualified name of the specific formatting class
Spring.jackson.deserialization whether to turn on Jackson's deserialization
Spring.jackson.generator whether to turn on JSON generators.
SPRING.JACKSON.JODA-DATE-TIME-FORMAT Specifies the format of the Joda date/time, such as YYYY-MM-DDHH:MM:SS). If not configured, DateFormat will act as backup
Spring.jackson.locale Specifies the locale used by JSON.
Spring.jackson.mapper whether the Jackson Universal feature is turned on.
Spring.jackson.parser whether to open Jackson's parser feature.
Spring.jackson.property-naming-strategy designation Propertynamingstrategy (camel_case_to_lower_case_with_underscores) Or specify the fully qualified class name of the Propertynamingstrategy subclass.
Spring.jackson.serialization whether to turn on Jackson's serialization.
spring.jackson.serialization-inclusion specifies how the property is inclusion at serialization time, and the Jsoninclude.include enumeration is specifically viewed.
Spring.jackson.time-zone Specifies the date to format the time zone, such as America/los_angeles or gmt+10.
Common configuration:
Spring: Jackson: #日期格式化 date-format:yyyy-mm-dd HH:mm:ss serialization: #格式化输出 true #忽略无法转换的对象 false #设置空如何序列化 defaultpropertyinclusion:non_empty deserialization: #允许对象忽略json中不存在的属性 false parser: # Allow special characters and escape characters to be true #允许出现单引号 true
Mode two: Using re-injection Objectmapper
* * Use the following configuration in the configuration bean * *
@Bean @primary@conditionalonmissingbean (objectmapper.class) Publicobjectmapper jacksonobjectmapper (Jackson2objectmapperbuilder builder) {Objectmapper Objectmapper= Builder.createxmlmapper (false). build (); //the Mapper object is set by this method, and all serialized objects are serialized according to the rules.//Include.Include.ALWAYS Default//the Include.non_default property is not serialized as the default value//if the Include.non_empty property is empty ("") or null is not serialized, the returned JSON does not have this field. This will save traffic to the mobile side//Include.non_null property is NULL not serializedobjectmapper.setserializationinclusion (JsonInclude.Include.NON_EMPTY); Objectmapper.configure (Deserializationfeature.fail_on_unknown_properties,false); //allow special characters and escape symbols to appearObjectmapper.configure (JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true); //allow single quotes to appearObjectmapper.configure (JsonParser.Feature.ALLOW_SINGLE_QUOTES,true); //field is reserved, the null value is converted to ""Objectmapper.getserializerprovider (). Setnullvalueserializer (NewJsonserializer<object>() {@Override Public voidSerialize (Object O, jsongenerator jsongenerator, Serializerprovider serializerprovid ER)throwsIOException {jsongenerator.writestring (""); } }); returnObjectmapper; }
Original
Two ways to configure Springboot's Jackson