SpringMVC前后端交互使用场景总结

目录

Get 请求

1、使用 HttpServletRequest 获取参数

 2、参数直接在路径中传递

 3、参数跟在 ? 后面

4、使用 Map 接收参数值(可传递多个,用 & 连接)

5、使用对象传参

Post 请求

1、使用 HttpServletRequest 获取参数(通用型)

2、Form 表单数据(写在 body 里)

3、使用 Map 传参

4、使用对象来接收参数

6、使用 Map 接收 JSON 数据(@RequestBody)


Get 请求


说明:@GetMapping("/getInfo")  与 @RequestMapping(value = "/getInfo",method = RequestMethod.GET)  有相同的效果。

1、使用 HttpServletRequest 获取参数


请求URL地址:http://127.0.0.1/getInfo5?userId=123&userName=流放深圳

controller层接收方法:

   @GetMapping("/getInfo5")
    public String getInfo5(HttpServletRequest request){
        String userName = request.getParameter("userName");
        Integer userId = Integer.parseInt(request.getParameter("userId"));
        System.out.println("userName="+userName+",userId="+userId);
        return "userName="+userName+",userId="+userId;
    }


 
2、参数直接在路径中传递


请求URL地址(要传递的参数是 biandan):http://127.0.0.1/getInfo/biandan

controller:

@RestController
public class UserController {
    @GetMapping("/getInfo/{userName}")
    public UserEntity getInfo(@PathVariable(value = "userName") String userName){
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(123);
        userEntity.setUserName(userName);
        return userEntity;
    }
}


注意事项:

这种方式的传参,只能传递数字、英文字母。如果传递中文,会报错 400 Bad Request。除非使用汉字转码。

汉字转码地址:https://www.sojson.com/encodeurl.html  比如:

 3、参数跟在 ? 后面


请求URL地址:http://127.0.0.1/getInfo2?userName=流放深圳

后台写法:required = false 是非必传的意思,不传递则为 null。defaultValue 是默认值,当前端不传递时,默认的数据。

    @GetMapping("/getInfo2")
    public UserEntity getInfo2(@RequestParam(value = "userName",required = false,defaultValue = "无名") String userName){
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(123);
        userEntity.setUserName(userName);
        return userEntity;
    }

测试结果

4、使用 Map 接收参数值(可传递多个,用 & 连接)


请求URL地址:http://127.0.0.1/getInfo3?userName=流放深圳&age=18

后台写法:

    @GetMapping("/getInfo3")
    public String getInfo3(@RequestParam Map<String,Object> reqMap){
        String userName = (String)reqMap.get("userName");
        Integer age = Integer.parseInt(reqMap.get("age").toString());
        return "userName="+userName+",age="+age;
    }


结果:

5、使用对象传参


请求URL地址(与 Map 传参写法相同):http://127.0.0.1/getInfo4?userId=123&userName=流放深圳

后台写法:

    @GetMapping("/getInfo4")
    public String getInfo4(UserEntity userEntity){
        String userName = userEntity.getUserName();
        Integer userId = userEntity.getUserId();
        System.out.println("userName="+userName+",userId="+userId);
        return "userName="+userName+",userId="+userId;
    }

Post 请求


说明:@PostMapping("/getInfo")  与 @RequestMapping(value = "/getInfo",method = RequestMethod.POST)  有相同的效果。

1、使用 HttpServletRequest 获取参数(通用型)


请求URL地址(可以放在 Params、Body【form-data、x-www-form-urlencoded】都可以):http://127.0.0.1/getInfo6?userId=123&userName=流放深圳

后台写法:

  @GetMapping("/getInfo5")
    public String getInfo5(HttpServletRequest request){
        String userName = request.getParameter("userName");
        Integer userId = Integer.parseInt(request.getParameter("userId"));
        System.out.println("userName="+userName+",userId="+userId);
        return "userName="+userName+",userId="+userId;
    }


 
 
2、Form 表单数据(写在 body 里)


请求URL地址:(可以放在 Params、Body【form-data、x-www-form-urlencoded】都可以):http://127.0.0.1/getInfo7

后台写法:

    @PostMapping("/getInfo7")
    public String getInfo7(@RequestParam(value = "userName",required = false)String userName,
                           @RequestParam(value = "userId",required = false)Integer userId){
        System.out.println("userName="+userName+",userId="+userId);
        return "userName="+userName+",userId="+userId;
    }


 

3、使用 Map 传参


请求URL地址(可以放在 Params、Body【form-data、x-www-form-urlencoded】都可以):http://127.0.0.1/getInfo8?userId=101&userName=流放深圳55

后台写法:

    @PostMapping("/getInfo8")
    public String getInfo8(@RequestParam Map<String,Object> reqMap){
        String userName = (String)reqMap.get("userName");
        Integer userId = Integer.parseInt(reqMap.get("userId").toString());
        return "userName="+userName+",userId="+userId;
    }


 

4、使用对象来接收参数


请求URL地址:http://127.0.0.1/getInfo9?userId=123&userName=今天高考

后台写法:

    @PostMapping("/getInfo9")
    public String getInfo9(UserEntity userEntity){
        String userName = userEntity.getUserName();
        Integer userId = userEntity.getUserId();
        System.out.println("userName="+userName+",userId="+userId);
        return "userName="+userName+",userId="+userId;
    }


 

5、使用 Bean 对象来接收 JSON 数据(@RequestBody)
请求URL地址:必须把参数放在 Body 下,并且选择 raw,使用 JSON 格式。否则报错 415:

{
    "timestamp": "2021-06-07T07:06:21.559+0000",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
    "path": "/getInfo10"
}


后台写法:

    @PostMapping("/getInfo10")
    public String getInfo10(@RequestBody UserEntity userEntity){
        String userName = userEntity.getUserName();
        Integer userId = userEntity.getUserId();
        System.out.println("userName="+userName+",userId="+userId);
        return "userName="+userName+",userId="+userId;
    }


 

6、使用 Map 接收 JSON 数据(@RequestBody)


请求URL地址:

后台写法:

    @PostMapping("/getInfo11")
    public String getInfo11(@RequestBody Map<String,Object> reqMap){
        String userName = (String)reqMap.get("userName");
        Integer userId = Integer.parseInt(reqMap.get("userId").toString());
        return "userName="+userName+",userId="+userId;
    }


 
————————————————
版权声明:本文为CSDN博主「流放深圳」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/BiandanLoveyou/article/details/117658392

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值