SPRINGMVC return JSON format

Source: Internet
Author: User

When developing an API in a restful way, you need to return data in JSON format.

But what's the most convenient?

1. SPRINGMVC Collection Com.fastxml.jackson.core The following three package Jackson-core,jackson-databind, jackson-annotions configured in the Pom.xml file,

2. Use the @responsebody annotation function inside the @controll class

3. Adding <mvc:annotion-driven/> to the spring configuration file, this configuration is important, the key to SPRINGMVC's ability to work, and the key to being able to match Jackson's return JSON format.

Then start the service and you'll see the results in JSON format.

Why use Jackson to look at the source code:

When the @response annotation is parsed and executed, it defaults to the Mappingjackson2httpmessageconverter class, which inherits Abstracthttpmessageconverter. This class refers to the various classes within Jackson. So to introduce the Jackson bag to use.

Import com.fasterxml.jackson.core.JsonEncoding;
Import Com.fasterxml.jackson.core.JsonGenerator;
Import com.fasterxml.jackson.core.JsonProcessingException;
Import Com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
Import Com.fasterxml.jackson.databind.JavaType;
Import Com.fasterxml.jackson.databind.ObjectMapper;
Import Com.fasterxml.jackson.databind.SerializationFeature;


SPRINGMVC return JSON format

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.