springboot中使用requestBody注解接收json串(参数)

第一种

controller

	/**
	*第一种
	*/
    @PostMapping(value= "/addOrgposNoparametercheck",produces="application/json;charset=UTF-8")
    public int addOrgposNoparametercheck(@RequestBody SysOrgpos sysOrgpos)  {
        return sysOrgposService.addOrgposNoparametercheck(sysOrgpos);
    }
    
    /**
	*第二种
	*/
    @RequestMapping(path = "/addOrgposNoparametercheck", method = RequestMethod.POST, produces ="application/json;charset=UTF-8" )
    public int addOrgposNoparametercheck(@RequestBody SysOrgpos sysOrgpos)  {
        return sysOrgposService.addOrgposNoparametercheck(sysOrgpos);
    }

serviceImpl

    @Override
    public int addOrgposNoparametercheck(SysOrgpos sysOrgpos) {
        return sysOrgposMapper.addOrgpos(sysOrgpos);
    }

mapper


      insert into "sys_orgpos" ("id", "org_id","org_latitude", "org_longitude", "sortcode","updatetime", "isdel")
    values (#{id,jdbcType=VARCHAR}, #{org_id,jdbcType=VARCHAR}, #{org_latitude,jdbcType=VARCHAR},#{org_longitude,jdbcType=VARCHAR},
    #{sortcode,jdbcType=VARCHAR}, #{updatetime,jdbcType=VARCHAR},#{isdel,jdbcType=VARCHAR})
  

@RequestBody SysOrgpos sysOrgpos 这种形式会将JSON字符串中的值赋予SysOrgpos 中对应的属性上,需要注意的是,JSON字符串中的key必须对应user中的属性名,否则是请求不过去的。
参考博客

第二种

controller

    @ApiOperation(value = "添加矿井地理信息,进行参数校验",notes="添加矿井地理信息,进行参数校验", produces = "application/json")
    @PostMapping(value= "/addOrgpos",produces="application/json;charset=UTF-8")
    public int addOrgpos(@RequestBody String sysOrgpos)  {
        return sysOrgposService.addOrgpos(sysOrgpos);
    }

service

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrgpos(String sysOrgpos) {
        JSONObject json = JSON.parseObject(sysOrgpos);
        String org_id = (String) json.get("org_id");
        String org_latitude = (String) json.get("org_latitude");
        String org_longitude = (String) json.get("org_longitude");
        String sortcode = (String) json.get("sortcode");

        SysOrgpos sysOrgpos1 = new SysOrgpos
                (id,org_id,org_latitude,org_longitude,sortcode,FormatDateUtil.dataFormat(new Date()),"0");

        int count = sysOrgposMapper.addOrgpos(sysOrgpos1);
        return count;
    }

sql同上,
这种方式是获取前端传过来的json字符串,再将json转换成object,可以获取object中字段的值

postman截图
springboot中使用requestBody注解接收json串(参数)_第1张图片
这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!!!

欢迎加入技术群聊

springboot中使用requestBody注解接收json串(参数)_第2张图片

你可能感兴趣的:(springboot注解)