美文网首页
前端→后台传参数,control类方法中如何接收

前端→后台传参数,control类方法中如何接收

作者: 墨色尘埃 | 来源:发表于2017-12-12 12:40 被阅读0次

前端/postman通过url模拟→后台传参数,control类方法中如何接收
SpringBoot开发详解(五)--Controller接收参数以及参数校验
1、如果方法里定义的是Object作为参数的话,那么需要RequestBody配合使用,这样接收到的Object才不为空。// 2017-12-17改:这种情况下只有使用POST请求才有用

    /**
     * 新增
     * 如果方法里定义的是对象作为参数的话,那么需要RequestBody配合使用,这样接收到的对象才不为空
     *
     * @RequestBody EnterpriseBase enterpriseBase
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseObj<Boolean> insert(@RequestBody EnterpriseBase enterpriseBase) {

        //先查询是否有这条数据,没有进行插入操作
        EnterpriseBase enterpriseBase1 = enterpriseBaseMapper.selectByPrimaryKey(enterpriseBase.getEnterpriseId());
        if (enterpriseBase1 == null) {
            String id = UUID.randomUUID().toString();
            enterpriseBase.setEnterpriseId(id);

            int i = enterpriseBaseMapper.insertSelective(enterpriseBase);
            if (i == 1)
                return new ResponseObj<Boolean>(true, RetCode.SUCCESS);
            return new ResponseObj<>(false, RetCode.FAIL);
        } else {
            return new ResponseObj<>(false, RetCode.FAIL);
        }

    }

2、如果方法里定义的是Map作为参数的话,也需要@RequestBody配合使用。// 2017-12-17改:这种情况下只有使用POST请求才有用

    /**
     * 后台map接收前端传过来的json对象
     * 如果方法里定义的是Map作为参数的话,也需要@RequestBody配合使用
     *
     * @RequestBody Map<String,Object> reqMap
     */
    @RequestMapping(value = "/ddd", method = RequestMethod.POST)
    public ResponseObj<Boolean> ddd(@RequestBody Map<String, Object> reqMap) {

        String enterpriseId = (String) reqMap.get("enterpriseId");
        //先查询是否有这条数据,没有进行插入操作
        EnterpriseBase enterpriseBase1 = enterpriseBaseMapper.selectByPrimaryKey(enterpriseId);
        if (enterpriseBase1 == null) {
            String id = UUID.randomUUID().toString();

//            int i = enterpriseBaseMapper.insertSelective(enterpriseBase);
            int i = 1;
            if (i == 1)
                return new ResponseObj<Boolean>(true, RetCode.SUCCESS);
            return new ResponseObj<>(false, RetCode.FAIL);
        } else {
            return new ResponseObj<>(false, RetCode.FAIL);
        }

    }
3、当使用POST请求的时候,前端传过来的是json对象(其实也就是对象,后台使用Map或者Object接收)。application/json 这个 Content-Type 作为响应头大家肯定不陌生。实际上,现在越来越多的人把它作为请求头,用来告诉服务端消息主体是序列化后的 JSON 字符串。 postman测试post请求.png
@RestController
@RequestMapping("/v1/enterpriseInfo")
public class EnterpriseBaseControl {
    @Autowired
    private EnterpriseBaseMapper enterpriseBaseMapper;

    /**
     * 新增
     * 如果方法里定义的是对象作为参数的话,那么需要RequestBody配合使用,这样接收到的对象才不为空
     *
     * @RequestBody EnterpriseBase enterpriseBase
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseObj<Boolean> insert(@RequestBody EnterpriseBase enterpriseBase) {

        //先查询是否有这条数据,没有进行插入操作
        EnterpriseBase enterpriseBase1 = enterpriseBaseMapper.selectByPrimaryKey(enterpriseBase.getEnterpriseId());
        if (enterpriseBase1 == null) {
            String id = UUID.randomUUID().toString();
            enterpriseBase.setEnterpriseId(id);

            int i = enterpriseBaseMapper.insertSelective(enterpriseBase);
            if (i == 1)
                return new ResponseObj<Boolean>(true, RetCode.SUCCESS);
            return new ResponseObj<>(false, RetCode.FAIL);
        } else {
            return new ResponseObj<>(false, RetCode.FAIL);
        }

    }

    /**
     * 后台map接收前端传过来的json对象
     * 如果方法里定义的是Map作为参数的话,也需要@RequestBody配合使用
     *
     * @RequestBody Map<String,Object> reqMap
     */
    @RequestMapping(value = "/ddd", method = RequestMethod.POST)
    public ResponseObj<Boolean> ddd(@RequestBody Map<String, Object> reqMap) {

        String enterpriseId = (String) reqMap.get("enterpriseId");
        //先查询是否有这条数据,没有进行插入操作
        EnterpriseBase enterpriseBase1 = enterpriseBaseMapper.selectByPrimaryKey(enterpriseId);
        if (enterpriseBase1 == null) {
            String id = UUID.randomUUID().toString();

//            int i = enterpriseBaseMapper.insertSelective(enterpriseBase);
            int i = 1;
            if (i == 1)
                return new ResponseObj<Boolean>(true, RetCode.SUCCESS);
            return new ResponseObj<>(false, RetCode.FAIL);
        } else {
            return new ResponseObj<>(false, RetCode.FAIL);
        }

    }

}

相关文章

网友评论

      本文标题:前端→后台传参数,control类方法中如何接收

      本文链接:https://www.haomeiwen.com/subject/ukngixtx.html