JSON interface return value

Source: Internet
Author: User

Springboot responding to client-side rendering data

1. Springboot constructs and returns a JSON object

First, we create a new Pojo package under the parent package and create a new user class

idea automatically generates the corresponding Get/set, and then creates a new Usercontroller.

This time we use @controller, @RequestMapping to define the Controller, note that if you write only one @controller the runtime will error (mapping failed). Method is the same as before, but we have added a new @responsebody that will let the method return a JSON object.

Then run application again.

Request URL, you can see that the returned data is transmitted to the foreground in JSON format.

Of course, a friend with project development experience must know that this JSON format is not in line with the development requirements

Normal JSON returns, you should customize some status codes, and then put the data

We can redefine a new approach.

The returned form is the JSON format we defined

Once the URL is re-requested, you can see the normal JSON return form.

Of course we just follow the traditional springmvc annotation form, then in Springboot, we can directly use the @restcontroller, and the @controller, @ResponseBody and other comments out

After you rerun the program, the result is the same.

Previously mentioned in the user class has some of the property values are some problems, such as getting a user object does not return a password, and the format of the birthday problem, and desc is empty, then in fact, the front end is not necessary to obtain.

At this point, we can use Jackson as a convenient treatment.

In the form of annotations, @JsonIgnore ignores this attribute, @JsonFormat is formatted output, pattern is the form, locale is region, timezone instant Zone (East eight), @JsonInclude is the judgment attribute, When NULL is not output to the foreground.

After you rerun the program, you can see that the JSON returned is more perfect.

JSON interface return value

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.