@GetMapping注解

当我们在Spring MVC或Spring Boot应用程序中使用@GetMapping注解时,它用于处理HTTP GET请求。

它是@RequestMapping(method = RequestMethod.GET)的一种快捷方式。

RequestMapping注解:

  • 在Spring 4.3之前,将传入的HTTP请求URL映射到控制器方法的主要注解是@RequestMapping。
  • 但是,Spring 4.3引入了更具体的每个HTTP请求类型的注解:

@GetMapping:处理HTTP GET请求。

@PostMapping:处理HTTP POST请求。

@PutMapping:处理HTTP PUT请求。

@DeleteMapping:处理HTTP DELETE请求。

@PatchMapping:处理HTTP PATCH请求。

@GetMapping的示例:
假设我们有一个REST控制器来管理用户数据。我们可以使用@GetMapping将特定的URI映射到控制器方法。

@RestController
public class UserController {
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/users")
    public ResponseEntity<List<User>> getAll() {
        List<User> users = userService.getAll();
        return ResponseEntity.ok(users);

在上面的示例中,@GetMapping(“/users”)将处理对/users路径的GET请求,并调用getAll()方法来获取所有用户的列表。如果请求成功,将返回一个包含用户列表的HTTP响应。

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值