springboot中参数传递的三个注解的使用

springboot中参数传递的三个注解的使用

@PathVariable

@PathVariable 注解主要是用来获取 url 参数,Spring Boot 支持 restfull 风格的 url,比如一个 GET请求携带一个参数 id 过来,我们将 id 作为参数接收,可以使用 @PathVariable 注解

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/testPathVariable01/{id}/{name}")
    public Map<String, Object> testPathVariable01(@PathVariable Integer id, @PathVariable(value = "name") String username){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        jsonObject.put("username",username);
        return ResultInfo.getDataMap(jsonObject);
    }
}

如果想要url中占位符中的id值直接赋值到参数id中,需要保证url中的参数和方法接收参数一致,否则就无法接收。如果不一致的话,其实也可以解决,需要用@PathVariable 中的value属性来指定对应关系。比如上面的参数name

测试地址:127.0.0.1:8080/test/testPathVariable01/2/city

还有就是对于访问的url,占位符的位置可以在任何位置,不一定非要在最后,比如这样也行: /xxx/{id}/user 。另外,url 也支持多个占位符,方法参数使用同样数量的参数来接收,原理和一个参数是一样的

@GetMapping("/testPathVariable01/{id}/and/{name}")
    public Map<String, Object> testPathVariable02(@PathVariable Integer id, @PathVariable(value = "name") String username){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        jsonObject.put("username",username);
        return ResultInfo.getDataMap(jsonObject);
    }

测试地址:127.0.0.1:8080/test/testPathVariable01/2/and/city

@RequestParam

GET用法

@RequestParam和@PathVariable的区别:

@PathValiable 是从 url 模板中获取参数值, 即这种风格的 url:http://localhost:8088/test/testPathVariable/{id} ;

@RequestParam 是从 request 里面获取参数值,即这种风格的 url: http://localhost:8088/test/testRequestparam?id=1

	@GetMapping("/testRequestParam01")
    public Map<String, Object> testRequestParam01(Integer id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        return ResultInfo.getDataMap(jsonObject);
    }

测试地址:127.0.0.1:8080/test/testRequestParam01?id=2

@GetMapping("/testRequestParam02")
    public Map<String, Object> testRequestParam02(@RequestParam Integer id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        return ResultInfo.getDataMap(jsonObject);
    }

测试地址:127.0.0.1:8080/test/testRequestParam02?id=2

@GetMapping("/testRequestParam03")
    public Map<String, Object> testRequestParam03(@RequestParam(value = "name") String username){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", username);
        return ResultInfo.getDataMap(jsonObject);
    }

测试地址:127.0.0.1:8080/test/testRequestParam03?name=city

POST用法

@RequestParam注解还可以用于POST请求,接收前端表单提交的参数,假如前端通过表单提交username和
password两个参数,那我们可以使用@RequestParam来接收,用法和上面一样

@PostMapping("/testRequestParam04")
    public Map<String, Object> testRequestParam04(@RequestParam String username, @RequestParam String password){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", username);
        jsonObject.put("password", password);
        return ResultInfo.getDataMap(jsonObject);
    }

在这里插入图片描述

但是通常表单提交的参数比较多,向上面那样一个一个的参数进行接收是不合理的,通常情况下我们都是封装成一个实体类传参,实体中的属性名和表单中的参数名一致即可;后台只需要接收这个实体类就行了,这种情况下就不需要加@RequestParam注解了

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String username;
    private String password;
}

@PostMapping("/testRequestParam05")
    public Map<String, Object> testRequestParam05(User user){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", user.getUsername());
        jsonObject.put("password", user.getPassword());
        return ResultInfo.getDataMap(jsonObject);
    }

在这里插入图片描述

@RequestBody

@RequestBody注解用于接收前端传来的实体,接收参数也是对应的实体,比如前端通过 json 提交传来两个参数 username 和password

@PostMapping("/testRequestBody01")
    public Map<String, Object> testRequestBody01(@RequestBody User user) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", user.getUsername());
        jsonObject.put("password", user.getPassword());
        return ResultInfo.getDataMap(jsonObject);
    }

在这里插入图片描述

@RequestBody注解用于POST请求上,接收json实体参数。

@RequestBody接收的是json实体,@RequestParam接收的是表单提交

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值