A: Preface
After testing, Jackson has a lot of undesirable places, so it is suggested to use Fastjson to replace;
Two: Jackson's pit
Define the entity class first:
true publicclass Mockmodel { private String fuid; Private String fName; Private String Fgender;}
Pit 1: Here I use Lombok annotations to automatically generate construction methods and getter, setter In the test springboot, the default Jackson is the Mockmodel object that does not support serializing JSON strings to @data annotations (no getter and setter are defined manually);
Test mode @requestbody Mockmodel body; then the POST request body parameter is the corresponding JSON string (exactly), but Jackson cannot convert ; (Custom getter and setter methods Jackson can deserialize to a Mockmodel object, and Fastjson is both.)
Pit 2:mapping method returns the Mockmodel object, and Jackson is able to convert the Mockmodel object to a JSON string, but the property name transforms the very pit dad, originally I need {"fuid": xxx ...} But after Jackson's conversion, key becomes lowercase, that is, fuid instead of fuid;
Three: The way to replace Jackson with Fastjson
Declaring a bean directly can:
@Bean Publichttpmessageconverters fastjsonhttpmessageconverters () {//1. You need to define an object for the convert message;Fastjsonhttpmessageconverter Fastjsonhttpmessageconverter =NewFastjsonhttpmessageconverter (); //2: Add the configuration information of Fastjson;Fastjsonconfig Fastjsonconfig =NewFastjsonconfig (); /*** TODO The first serializerfeature.prettyformat can be omitted, after all this causes additional memory consumption and traffic, and the second is used to specify if the property value is null is output: Pro:null*/fastjsonconfig.setserializerfeatures (Serializerfeature.writemapnullvalue); //3 Handling Chinese garbled problemList<mediatype> fastmediatypes =NewArraylist<>(); Fastmediatypes.add (Mediatype.application_json_utf8); //4. Add the configuration information in convert.fastjsonhttpmessageconverter.setsupportedmediatypes (fastmediatypes); Fastjsonhttpmessageconverter.setfastjsonconfig (Fastjsonconfig); Httpmessageconverter<?> converter =Fastjsonhttpmessageconverter; return Newhttpmessageconverters (Converter); }
The test is not replaced by the format of the JSON string that can be serialized by setting Serializerfeature.prettyformat;
can also be tested by the following method (the following way will be serialized into a JSON string when it becomes {"F_uid": xx}, originally {"Fuid": xx}, but input can still be two kinds of line)
In this form, it is possible to implement the specialization of the JSON parameter format with some interfaces, that is, to define a amodel that is specifically connected to a system and then use @jsonfield (name = "Xx_bb"). Then the JSON string with a system will be specialized (since the corresponding Amodel), while the other system is the default format;
@JSONField (name = "F_uid")private String fuid;
Fastjson does not have pit 1 and pit 2 in Jackson, and supports converting as {"AA_BB": 33} to the property name Aabb assigning it 33 (and also supporting some of the conversion in the form of AA_BB part of the property is Uumm)
Three: Some considerations in JSON format
1.key-value delimiter can only be: number and cannot be = number
2. Numeric strings can be converted to numeric properties, and numeric values can also be converted to string properties (such as fuid:33 can be converted to string fuid in Mockmodel), at least Fastjson supported;
3.key part Best Use "" "package, after testing some JSON package is not supported, such as {fuid:33}, only support {" Fuid ": 33}
4. The mainstream JSON package supports the notation of {"fName": null};
IV: Other
Springboot get method can not set Content-type (because it is not used), but post must set this property, and it seems not to be the type of */*;xx?? , such as the JSON parameters above will be Content-type:application/json;charset=utf8 (CharSet can not, but preferably written on)
Replace the default Jackson with Fastjson in Springboot