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