Springmvc this @RequestBody used relatively little, today to see a bit, or is very convenient.
@RequestBody receive a JSON string similar to [{name: ' Test '}, {name: ' Zhang San '}].
Look at the page first:
<! DOCTYPE html>functionTest () {varsavedataary=[]; vardata1={"name": "Test"}; vardata2={"name": "Zhang San"}; Savedataary.push (DATA1); Savedataary.push (DATA2); $.ajax ({type:"POST", URL:"Http://localhost/test/student", DataType:"JSON", ContentType:"Application/json", Data:JSON.stringify (savedataary), success:function(data) {alert (data)}}); }</script>Last look at the background code:
Importjava.util.List;ImportOrg.springframework.web.bind.annotation.RequestBody;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportOrg.springframework.web.bind.annotation.RestController, @RestController @requestmapping ("/test") Public classTestController {@ResponseBody @RequestMapping (value= "/student", method=requestmethod.post) PublicString Student (@RequestBody list<student>students) { for(Student s:students) {System.out.println ("Student Name:" +s.getname ()); } return"OK"; }}classstudent{PrivateString name; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } }
To summarize, this argument is JSON string.
SPRINGMVC @RequestBody receive JSON object string demo