If you use spring MVC to communicate with clients and use the JSON data format completely, you need to define a requestmapping as follows
@Controller Public class testcontroller{ @RequestMapping ("\test") @ResponseBody public Retureresult Test (@RequestBody user user) { returnnew returnresult (); } }
This allows the JSON-formatted data to be converted to the specified object user, such as {name: ' Test '},name the property value of the user class, or it can return JSON-formatted data.
But sometimes when we receive JSON-formatted data, we may need to convert it to multiple objects, which is not supported by the direct definition of multiple arguments in a method variable, and the following is an error. The reason is that the body of request is read in the form of a stream, and once it has been read, it cannot be read again.
@Controller public class testcontroller{@RequestMapping ( "\test" ) @ Responsebody public Retureresult Test (@RequestBody User user, @RequestBody address) { return new Returnresult (); } }
You can use a workaround to do this at this point.
Add a wrapper class to the Testcontroller.java file
@Controller Public class testcontroller{ @RequestMapping ("\test") @ResponseBody public Retureresult Test (@RequestBody Param Param) { returnnew returnresult (); }class param{ public user user; Public address address; }
Note: A property in a class must be a property in Public;param only more than the object in JSON, not less.
The transferred JSON data format changes to {user:{name: "Test"},address:{location: "Xinhualu"}}.
Because just adding a wrapper class to the TestController does not affect other classes and the model class that is already defined, it is very convenient to achieve the purpose of receiving multiple object parameters.
Spring receives multiple object parameters in JSON format (workaround)