@RequestMapping 和 @GetMapping等子注解的区别及其用法

常用的请求映射注解介绍

  1. @GetMapping
    • 用于处理 HTTP GET 请求。
    • 等价于 @RequestMapping(method = RequestMethod.GET)
    1@GetMapping("/path")
    2public String handleGetRequest() {
    3    return "GET request handled";
    4}
  2. @PostMapping
    • 用于处理 HTTP POST 请求。
    • 等价于 @RequestMapping(method = RequestMethod.POST)
    1@PostMapping("/path")
    2public String handlePostRequest(@RequestBody String data) {
    3    return "POST request handled";
    4}
  3. @PutMapping
    • 用于处理 HTTP PUT 请求。
    • 等价于 @RequestMapping(method = RequestMethod.PUT)
    1@PutMapping("/path")
    2public String handlePutRequest(@RequestBody String data) {
    3    return "PUT request handled";
    4}
  4. @DeleteMapping
    • 用于处理 HTTP DELETE 请求。
    • 等价于 @RequestMapping(method = RequestMethod.DELETE)
    1@DeleteMapping("/path")
    2public String handleDeleteRequest() {
    3    return "DELETE request handled";
    4}
  5. @PatchMapping
    • 用于处理 HTTP PATCH 请求。
    • 等价于 @RequestMapping(method = RequestMethod.PATCH)
    1@PatchMapping("/path")
    2public String handlePatchRequest(@RequestBody String data) {
    3    return "PATCH request handled";
    4}
  6. @OptionsMapping
    • 用于处理 HTTP OPTIONS 请求。
    • 等价于 @RequestMapping(method = RequestMethod.OPTIONS)
    1@OptionsMapping("/path")
    2public String handleOptionsRequest() {
    3    return "OPTIONS request handled";
    4}
  7. @HeadMapping
    • 用于处理 HTTP HEAD 请求。
    • 等价于 @RequestMapping(method = RequestMethod.HEAD)
    1@HeadMapping("/path")
    2public String handleHeadRequest() {
    3    return "HEAD request handled";
    4}
  8. @RequestMapping
    • 用于映射 HTTP 请求。
    • 可以指定多个 HTTP 方法。
1@RequestMapping(value = "/path", method = {RequestMethod.GET, RequestMethod.POST})
2public String handleMultipleRequests(@RequestParam String param) {
3    return "Multiple requests handled";
4}


区别及其用法

详细解释一下 @RequestMapping@GetMapping 等子注解的区别及其用法。

注:一下以@GetMapping 为例,其他子注解同理

 @RequestMapping 注解

@RequestMapping 是一个通用的请求映射注解,它可以用来映射 HTTP 请求到特定的方法。它可以单独使用或者与其他注解(如 @GetMapping@PostMapping 等)结合使用。

@RequestMapping 的用法:
  • 路径映射:指定请求的 URL 路径。
  • HTTP 方法:可以指定一个或多个 HTTP 方法(如 GET、POST、PUT、DELETE 等)。
示例代码:
1@RequestMapping("/test")
2public class TestController {
3
4    @RequestMapping(method = RequestMethod.GET)
5    public String handleGetRequest() {
6        return "GET request handled";
7    }
8
9    @RequestMapping(method = RequestMethod.POST)
10    public String handlePostRequest() {
11        return "POST request handled";
12    }
13}


 @GetMapping 注解

@GetMapping 是一个方便的注解,专门用来映射 HTTP GET 请求。它是 @RequestMapping 的子注解,内部使用 @RequestMapping(method = RequestMethod.GET) 实现。

@GetMapping 的用法:
  • 仅用于 GET 请求:专门用来处理 HTTP GET 请求。
示例代码:
1@GetMapping("/test")
2public String handleGetRequest() {
3    return "GET request handled";
4}

组合使用

@RequestMapping 可以与 @GetMapping@PostMapping 等注解组合使用,以实现更细粒度的请求映射。

示例代码:
1@Controller
2@RequestMapping("/test")
3public class TestController {
4
5    @GetMapping
6    public String handleGetRequest() {
7        return "GET request handled";
8    }
9
10    @PostMapping
11    public String handlePostRequest() {
12        return "POST request handled";
13    }
14}

总结

  • @RequestMapping:是一个通用的请求映射注解,可以映射各种 HTTP 方法(GET、POST、PUT、DELETE 等)。
  • @GetMapping:专门用于处理 GET 请求,简化了代码。

使用建议

  • 如果一个方法只处理 GET 请求,使用 @GetMapping 更加简洁明了。
  • 如果一个方法需要处理多种 HTTP 方法,使用 @RequestMapping 并指定方法类型。


示例代码汇总

以下是完整的示例代码:

后端代码
1package com.example.demo;
2
3import org.springframework.web.bind.annotation.*;
4
5@RestController
6@RequestMapping("/test")
7public class TestController {
8
9    @GetMapping
10    public String handleGetRequest(@RequestParam String username) {
11        System.out.println("已接收到请求" + username);
12        return "传输成功";
13    }
14
15    @PostMapping
16    public String handlePostRequest(@RequestBody String data) {
17        System.out.println("已接收到 POST 数据:" + data);
18        return "POST 数据处理成功";
19    }
20}
前端代码
1<!DOCTYPE html>
2<html lang="en">
3<head>
4    <meta charset="UTF-8">
5    <meta name="viewport" content="width=device-width, initial-scale=1.0">
6    <title>表单提交数据</title>
7</head>
8<body>
9
10    <!-- GET 请求 -->
11    <form action="http://localhost:8080/test" method="get">
12        <input type="text" name="username">
13        <input type="submit" value="提交 GET 请求">
14    </form>
15
16    <!-- POST 请求 -->
17    <button onclick="sendPostRequest()">提交 POST 请求</button>
18
19    <script>
20        function sendPostRequest() {
21            fetch('http://localhost:8080/test', {
22                method: 'POST',
23                headers: {
24                    'Content-Type': 'application/json'
25                },
26                body: JSON.stringify({ data: 'Hello, POST!' })
27            })
28            .then(response => response.text())
29            .then(data => console.log(data))
30            .catch(error => console.error('Error:', error));
31        }
32    </script>
33    
34</body>
35</html>

希望通过上述代码,你可以清楚地区分 GET 请求和 POST 请求,并且了解如何使用 @RequestMapping@GetMapping 注解来处理不同类型的请求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值