Springboot return JSON string (Jackson and fast JSON)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.