SPRINGMVC @RequestBody receive JSON object strings

Source: Internet
Author: User

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

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.