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