First, Jackson
1. Controller class plus annotations @restcontroller
This annotation is equivalent to @controller this annotation plus @ResponseBody
2. Springboot uses Jackson to convert Java objects to JSON strings by default.
Second, fast JSON
1. pom file joins fast JSON dependency
<dependency> <groupId>com.alibaba</groupId> <artifactid>fastjson</ artifactid> <version>1.2. </version> </dependency>
2. Inject a @bean into the app launcher class
@Bean Publichttpmessageconverters fastjsonhttpmessageconverters () {//1, need to first define a converter converterFastjsonhttpmessageconverter Fastconverter =NewFastjsonhttpmessageconverter (); //2, add Fastjson configuration information, such as: whether to format the returned JSON dataFastjsonconfig Fastjsonconfig =NewFastjsonconfig (); Fastjsonconfig.setserializerfeatures (Serializerfeature.prettyformat); //3. Add configuration information in convertFastconverter.setfastjsonconfig (fastjsonconfig); //4. Add Convert to Convertershttpmessageconverter<?> converter =Fastconverter; return Newhttpmessageconverters (Converter); }
3. Testing
Use Fastjson-specific annotations to test;
Annotate an attribute on a date type in an entity class @JSONField (format= "Yyyy/mm/dd hh:mm")
Returns the data to see if the date has been returned in the format above. Ok!
4. Annotations added on the controller class also use @restcontroller
Springboot return JSON string (Jackson and fast JSON)