Java Web base JSON and JavaBean conversions

Source: Internet
Author: User

GitHub Address: Https://github.com/liufeiSAP/JavaWebStudy

Entity class:

 PackageCom.study.demo.domain;ImportCom.fasterxml.jackson.annotation.JsonProperty;Importjava.util.List; Public classStudent { @JsonProperty (value = "Anothername")PrivateString name; Private intAge ; PrivateList<course>courses;  PublicString GetName () {returnname; }     Public intGetage () {returnAge ; }     PublicList<course>getcourses () {returncourses; }     Public voidsetName (String name) { This. Name =name; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public voidSetcourses (list<course>courses) {         This. courses =courses; }}

Controller:

The first approach: using @requestbody (recommended), Springboot integrates with Jackson, which automatically turns JSON into objects;

(Note: The name of the JSON key must be the same as the name of the entity attribute (if not the same as the @jsonproperty annotation)

Jackson's function is still very powerful, in this case the entity also nested list, you can correctly parse correctly.

  The second method: Use HttpServletRequest, and then reads the stream, and this method can be parsed in its own way.

)

@RequestMapping (value = "/student", method = requestmethod.post)    public  String addstudent (@ Requestbody Student record) {        return "OK";    }     = "/student1", method = requestmethod.post)    publicthrows  IOException {         = rquests.getinputstream ();         return "OK";    }

Java Web base JSON and JavaBean conversions

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.