SpringMVC@RequestBody receives an Json object string

  • 2020-05-30 20:04:09
  • OfStack

There are two types of page submission request parameters, one is submitted in form format and one is submitted in json format

Normally we use data submitted in form format :k=v & k=v, it is ok to receive parameters with springMVC at this time, but sometimes the front end will pass data through json to the back end, and springMVC will fail to get parameter values

Note: jQuery's $.post method can also pass data in json format, but it is actually submitted in form format. jquery will help you convert json to form format and submit the background

So you can actually submit the json format through $.post, $.get, and ask jquery to help you convert, but if the back-end USES restful, you can only use the following methods

JavaScript code:

<script type="text/javascript">  
    var saveDataAry=[];  
    var data1={"userName":"test","address":"gz"};  
    var data2={"userName":"ququ","address":"gr"};  

Backend springMVC reception

@RequestMapping(value = "user", method = RequestMethod.PUT )  
  public void saveUser(@RequestBody List<User> users) {  

Note: the submitted data must be an json format string, content-type must be 'application/json; charset= utf-8 'indicates the type and encoding format of the submission. dataType is the data type that is expected to be returned by the server. There are strict requirements on the format of json string, as follows: '{"type":"type","fileftppath":"fileftppath","map":{"id":1,"name":"suo"}}', string, key double quotes are required, otherwise 400 bad request, of course, if you do not format the data correctly, 400 bad request, for example, you send a "step1", the back end is received by Integer, which is also 400, directly from the json object to json string, This json string format will not be a problem, if 400, only the data format

Related articles: