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
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot 是 Spring Framework 基础之上构建的快速开发框架,它可以帮助开发者更快速、更简单地构建基于 Spring 的应用程序。Spring Boot 基于注解配置,注解使用可以使 Spring Boot 更加简洁、高效。 以下是 Spring Boot 常用的注解及其详解: 1. @SpringBootApplication:该注解表示这是一个 Spring Boot 应用,它是 Spring Boot 最重要的注解,相当于同时使用了 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 这三个注解。 2. @RestController:该注解是 Spring MVC 注解,表示这是一个 RESTful 风格的控制器,相当于同时使用了 @Controller 和 @ResponseBody 两个注解。 3. @RequestMapping:该注解用于将 HTTP 请求映射到控制器的处理方法上,可以定义请求的 URL、请求的方法(GET、POST 等)、请求的参数等。 4. @PathVariable:该注解用于从请求 URL 获取路径参数,可以将路径参数传递给控制器的处理方法。 5. @RequestParam:该注解用于从 HTTP 请求获取请求参数,可以将请求参数传递给控制器的处理方法。 6. @ResponseBody:该注解用于将返回值直接作为 HTTP 响应的内容返回,常用于返回 JSON、XML 等格式的数据。 7. @Autowired:该注解用于自动装配 Spring 容器的 Bean,可以将 Bean 注入到控制器、服务等组件。 8. @Service:该注解用于将一个类标记为服务,可以将服务注入到控制器、其他服务等组件。 9. @Repository:该注解用于将一个类标记为数据访问对象(DAO),可以将 DAO 注入到服务。 10. @Configuration:该注解用于定义 Spring 配置类,可以使用 @Bean 注解定义 Bean。 11. @Bean:该注解用于定义 Spring Bean,可以指定 Bean 的名称、作用域、初始化方法、销毁方法等属性。 12. @EnableAutoConfiguration:该注解用于自动配置 Spring Boot 应用程序,可以根据类路径和其他条件来决定应该配置哪些 Spring Bean。 以上是 Spring Boot 常用的注解及其详解,还有其他的注解可以根据实际需求选择使用

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值