码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC @RequestBody接收Json对象字符串

时间:2018-01-13 18:58:27      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:note   func   mod   size   eth   ror   ini   equals   渠道号   

以前,一直以为在SpringMVC环境中,@RequestBody接收的是一个Json对象,一直在调试代码都没有成功,后来发现,其实 @RequestBody接收的是一个Json对象的字符串,而不是一个Json对象。然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象变成字符串。同时ajax请求的时候也要指定dataType: "json",contentType:"application/json" 这样就可以轻易的将一个对象或者List传到Java端,使用@RequestBody即可绑定对象或者List.

JavaScript 代码:

<script type="text/javascript">  
    $(document).ready(function(){  
        var saveDataAry=[];  
        var data1={"userName":"test","address":"gz"};  
        var data2={"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代码

  @RequestMapping(value = "saveUser", method = {RequestMethod.POST }}) 
    @ResponseBody  
    public void saveUser(@RequestBody List<User> users) { 
         userService.batchSave(users); 
    }

使用API工具调用方法:

技术分享图片

json数据

{"versionNumbern":"1.0","channel":"ios"}

java代码

   @PostMapping("/check_version")
    @ApiOperation(value = "检测app版本信息", notes = "检测app版本信息")
    public ResponseEntity<JsonResponseVM<DetectVersionResponseVM>> detectVersion(@Validated @RequestBody DetectVersionRequestVM requestVM) {
        log.info("检测app版本信息: {}", JSON.toJSONString(requestVM));
        DetectVersionResponseVM responseVM = appService.detectVersion(requestVM);
        if (responseVM == null) {
            return ResponseEntity.ok(new JsonResponseVM(new ErrorVM(ErrorConstants.ERR_VERSION_VAILD_FAIL_CODE, ErrorConstants.ERR_VERSION_VAILD_FAIL, Constants.RETCODE_FAIL)));
        }
        if (requestVM.getChannel().equals(Constants.CHANNEL_IOS)) {
            responseVM.setDownloadUrl(hxjInitConfig.getIosDownloadUrl());
        }
        return new ResponseEntity<JsonResponseVM<DetectVersionResponseVM>>(new JsonResponseVM(new ErrorVM(Constants.RETCODE_SUCCEED), responseVM), HttpStatus.OK);
    }

用于接收的对象,可以自定义校验规范

@ApiModel
public class DetectVersionRequestVM {
    @Size(max = 20, message = "手机app版本号长度不能大于20")
    @ApiModelProperty(dataType = "java.lang.String", value = "版本号")
    private String versionNumbern;
@Pattern(regexp
= "(ios|android)") @Size(max = 20, message = "渠道长度不能大于20") @NotEmpty(message = "渠道号不能为空") @ApiModelProperty(dataType = "java.lang.String", value = "类型: ios、android", example = "ios") private String channel; //get set @Override public String toString() { return "DetectVersionRequestVM{" + "versionNumbern=‘" + versionNumbern + ‘\‘‘ + ", channel=‘" + channel + ‘\‘‘ + ‘}‘; } }

 

SpringMVC @RequestBody接收Json对象字符串

标签:note   func   mod   size   eth   ror   ini   equals   渠道号   

原文地址:https://www.cnblogs.com/jiafuwei/p/8279902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!