If we want to use a third-party JSON parsing framework in spring boot:
1) We need to introduce third-party package dependencies in the Pom.xml file;
2) Implementation method:
Method 1 requires inheriting the Webmvcconfigureradapter class in the startup class and overriding the Configuremessageconverters method of the class.
Method 2. We use @bean to inject third-party analytic frameworks directly.
1, the introduction of Fastjson-dependent library
<Dependency> <groupId>Com.alibaba</groupId> <Artifactid>Fastjson</Artifactid> <version>1.2.15</version></Dependency>
After 1.2.10, there will be two methods to support Httpmessageconvert, one is Fastjsonhttpmessageconvert, support the following version of 4.2;
One is FASTJSONHTTPMESSAGECONVERT4, which supports more than 4.2 versions.
2, configuration Fastjson (support two methods)
(1) Method one:
<1> Start class inherits Webmvcconfigureradapter class;
<2> coverage Method configuremessageconverters;
The ① startup class code is as follows:
@SpringBootApplication Public classAppextendswebmvcconfigureradapter{ @Override Public voidConfiguremessageconverters (listconverters) { Super. Configuremessageconverters (converters); /** 1, the need to define a convert conversion message object, * 2, add Fastjson configuration information, such as: whether to format the returned JSON data; * 3, add configuration information in convert; * 4. Add convert to converts; */ //1. A convert message object needs to be defined first;Fastjsonhttpmessageconverter Fastconverter =NewFastjsonhttpmessageconverter (); //2, add Fastjson configuration information, such as: whether to format the returned JSON data;Fastjsonconfig Fastjsonconfig =NewFastjsonconfig (); Fastjsonconfig.setserializerfeatures (Serializerfeature.prettyformat); //3, add the configuration information in Convert;Fastconverter.setfastjsonconfig (fastjsonconfig); //4. Add convert to converts;Converters.add (Fastconverter); } Public Static voidMain (string[] args) {/** The application that launches the App class in the Main method*/Springapplication.run (App.class, args); }}
The interface code for ②controller is as follows:
@RestController Public classhellocontroller{/*** returned JSON data *@return */@RequestMapping ("/getdemo") Public demo Getdemo () {Demo demo=NewDemo (); Demo.setid (1); Demo.setname ("Zhangsan"); Demo.setcreatetime ( New Date ()); returndemo; }}
③ Test Code:
In the Createtime property in the Demo entity class, format the date type as "Yyyy-mm-dd HH:mm:ss" using @JSONField annotations, as follows:
Public class demo{ privateint ID; Private String name; // annotations using the package: Com.alibaba.fastjson.annotation.JSONField @JSONField (format = "Yyyy-mm-dd HH:mm:ss") private Date createtime; Create Time //getter and setter method omitted }
Open the http://localhost:8080/getDemo in the browser:
If the format of "Createtime" in the returned JSON data is: YYYY-MM-DD HH:MM:SS, the configured Fastjson processing data is used, otherwise the configured Fastjson of the configuration does not take effect.
(2) Method two:
<1> injecting bean:httpmessageconverters into the startup class
The code for the startup class is as follows:
@SpringBootApplication Public classapp{/*** Inject Fastjsonhttpmessageconverter with @Bean *@return */@Bean Publichttpmessageconverters fastjsonhttpmessageconverters () {//1. A convert message object needs to be defined first;Fastjsonhttpmessageconverter Fastconverter =NewFastjsonhttpmessageconverter (); //2, add Fastjson configuration information, such as: whether to format the returned JSON data;Fastjsonconfig Fastjsonconfig =NewFastjsonconfig (); Fastjsonconfig.setserializerfeatures (Serializerfeature.prettyformat); //3, add the configuration information in Convert;Fastconverter.setfastjsonconfig (fastjsonconfig); //4.httpmessageconverter<?> converter =Fastconverter; return Newhttpmessageconverters (Converter); } Public Static voidMain (string[] args) {/** The application that launches the App class in the Main method*/Springapplication.run (App.class, args); }}
3. Some ways to use Fastjson
(1) Format the format of the date:
@JSONField(format = "Yyyy-mm-dd HH:mm:ss") private Date createtime; creation Time
(2) If you do not want the interface to return an attribute, set the Serialize property of the @JSONField to false(not serialized);
/* * Do not want to return this property, serialize: Whether to serialize */ @JSONFieldfalse) Private String remarks; // Notes Information
Spring Boot (ii): Parsing JSON data using Fastjson