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

x33g5p2x  于2021-11-19 转载在 Spring  
字(3.7k)|赞(0)|评价(0)|浏览(293)

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接收的是表单提交

相关文章

微信公众号

最新文章

更多