Previously, always thought in the SPRINGMVC environment, @RequestBody received a JSON object, has been debugging the code is not successful, and later found that, in fact, @RequestBody receive a JSON object string, rather than a JSON object. In AJAX requests, however, the JSON object is often passed, and it is later discovered that the object can be converted to a string using the Json.stringify (data) method. At the same time Ajax request also specify dataType: "JSON",contentType: "Application/json" so you can easily upload an object or list to the Java side, You can bind an object or list using @requestbody.
JavaScript Code:
<Scripttype= "Text/javascript">$ (document). Ready (function(){ varsavedataary=[]; vardata1={"UserName":"Test","Address":"GZ"}; vardata2={"UserName":"Ququ","Address":"GR"}; Savedataary.push (DATA1); Savedataary.push (DATA2); $.ajax ({type:"POST", URL:"User/saveuser", DataType:"JSON", ContentType:"Application/json", Data:JSON.stringify (SaveData), success:function(data) {}}); }); </Script>
Java code
@RequestMapping (value = "Saveuser", method = {requestmethod.post}}) @ResponseBody public void saveuser (@RequestBody list<user> users) { userservice.batchsave (users); }
To invoke the method using the API tool:
JSON data
{"Versionnumbern": "1.0", "Channel": "ios"}
Java code
@PostMapping ("/check_version") @ApiOperation (value= "Detect app version Information", notes = "Detect app version information") PublicResponseentity<jsonresponsevm<detectversionresponsevm>>detectversion (@Validated @RequestBody detectversionrequestvm requestvm) {log.info ("Detect app Version information: {}", Json.tojsonstring (REQUESTVM)); DETECTVERSIONRESPONSEVM RESPONSEVM=appservice.detectversion (REQUESTVM); if(RESPONSEVM = =NULL) { returnResponseentity.ok (NewJSONRESPONSEVM (NewERRORVM (Errorconstants.err_version_vaild_fail_code, Errorconstants.err_version_vaild_fail, Constants.RETCODE (_fail))); } if(Requestvm.getchannel (). Equals (Constants.channel_ios)) {Responsevm.setdownloadurl (Hxjinitconfig.getiosdow Nloadurl ()); } return NewResponseentity<jsonresponsevm<detectversionresponsevm>> (NewJSONRESPONSEVM (NewERRORVM (constants.retcode_succeed), RESPONSEVM), Httpstatus.ok); }
For receiving objects, you can customize the validation specification
@ApiModel Public classDETECTVERSIONREQUESTVM {@Size (max=, message = "mobile app version number cannot be greater than 20") @ApiModelProperty (DataType= "Java.lang.String", value = "Version number") PrivateString Versionnumbern;
@Pattern (RegExp= "(ios|android)") @Size (Max=, message = "channel length cannot be greater than 20") @NotEmpty (Message= "Channel number cannot be empty") @ApiModelProperty (DataType= "Java.lang.String", value = "type: iOS, Android", example = "ios") PrivateString Channel; Get Set@Override PublicString toString () {return"detectversionrequestvm{" + "versionnumbern=" + Versionnumbern + "\" + ", channel= '" + Cha Nnel + ' + ' + '} '; }}
SPRINGMVC @RequestBody receive JSON object strings