SpringBoot form格式和requestbody json格式

Posted 最小的帆也能远航

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot form格式和requestbody json格式相关的知识,希望对你有一定的参考价值。

一、form格式请求

以表单形式提交,默认 @RequestParam

@ApiOperation(value = "测试接口",httpMethod = "GET",response = Result.class,notes = "测试接口")
    @RequestMapping(value = "/GetUserInfo",method = RequestMethod.GET)
    public Result GetUserInfo(GetUserInfoRequest param){
        User userInfo =  userService.getUserInfoById(param.getUserId());
        return Result.success(userInfo);
    }

也可以这样写

 @ApiOperation(value = "测试接口",httpMethod = "GET",response = Result.class,notes = "测试接口")
    @RequestMapping(value = "/GetUserInfo",method = RequestMethod.GET)
    public Result GetUserInfo(@RequestParam GetUserInfoRequest param){
        User userInfo =  userService.getUserInfoById(param.getUserId());
        return Result.success(userInfo);
    }

请求方式

application/x-www-form-urlencoded

二、requestbody json格式

以json格式提交,用RequestBody声明

  @RequestMapping(value = "/InsertUserInfo", method = RequestMethod.POST)
    @ApiOperation(value = "提交用户信息", notes="返回成功失败")
    public Result InsertUserInfo(@RequestBody PostUserInfoRequest userInfo) {
        System.out.println("进来了 InsertUserInfo");

        try
        {
            //对象转换
            User info1 = new User();
            BeanUtils.copyProperties(userInfo, info1);

            User info = new User();
            info.setUserId(userInfo.getUserId());
            info.setUserAccount(userInfo.getUserAccount());
            info.setUserPass(userInfo.getUserPass());
            info.setUserPhone(userInfo.getUserPhone());
            info.setUserPhoto(userInfo.getUserPhoto());
            info.setUserState(userInfo.getUserState());

            int code = userService.InsertInfo(info);

            if(code == 1) {
                User uInfo = userService.getUserInfoById(info.getUserId());
                return Result.success(uInfo);
            }
        }
        catch (Exception e)
        {
            logger.error("写入数据库失败", e);
        }

        return Result.failure(-1,"插入错误");
    }

请求方式

application/json

以上是关于SpringBoot form格式和requestbody json格式的主要内容,如果未能解决你的问题,请参考以下文章

Springboot 页面请求 request.getParameter(xx)获取空

ajax和form表单,django的中间件

requests库发送multipart/form-data格式请求

使用requests库提交multipart/form-data 格式的请求

SpringBoot - Processing of multipart/form-data request failed. Unexpected EOF read on the socket

SpringBoot - Processing of multipart/form-data request failed. Unexpected EOF read on the socket(代码片