@GetMapping @PostMapping @DeleteMapping @PutMapping

在 Spring Boot(或 Spring MVC)中,@GetMapping、@PostMapping、@PutMapping 和 @DeleteMapping 是用于定义 RESTful 接口的 HTTP 方法专用注解,它们是对 @RequestMapping 的语义化封装,使代码更清晰、简洁。

注解 HTTP 方法 用途 等价写法 参数
@GetMapping GET 获取资源(查询) @RequestMapping(method = RequestMethod.GET) @PathVariable, @RequestParam 单独使用
@PostMapping POST 创建资源 / 提交数据 @RequestMapping(method = RequestMethod.POST) @RequestBody
@PutMapping PUT 全量更新 资源 @RequestMapping(method = RequestMethod.PUT) @PathVariable, @RequestBody 合用
@DeleteMapping DELETE 删除资源 @RequestMapping(method = RequestMethod.DELETE) @PathVariable
javascript 复制代码
@RestController
@RequestMapping("/api/users")
public class UserController {

    // GET /api/users/1
    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.findById(id);
    }

    // GET /api/users?role=admin
    @GetMapping
    public List<User> getUsers(@RequestParam(required = false) String role) {
        return userService.findByRole(role);
    }

    // POST /api/users (请求体为 JSON)
    @PostMapping
    public User createUser(@RequestBody CreateUserRequest request) {
        return userService.create(request);
    }

    // PUT /api/users/1 (全量更新)
    @PutMapping("/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody UpdateUserRequest request) {
        return userService.updateFull(id, request);
    }

    // DELETE /api/users/1
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
        userService.delete(id);
        return ResponseEntity.noContent().build(); // 返回 204 No Content
    }
}

@GetMapping

GET /api/example?a=1&b=2&c=3
方法1

javascript 复制代码
@GetMapping("/example")
public String handleRequest(
    @RequestParam String a,
    @RequestParam Integer b,
    @RequestParam(required = false) String c  // 可选参数
) {
    System.out.println("a=" + a + ", b=" + b + ", c=" + c);
    return "OK";
}

方法二:封装到一个 POJO 对象中(适合参数多或复用)

javascript 复制代码
public class QueryParams {
    private String a;
    private Integer b;
    private String c;

    // 必须有 getter/setter(Lombok 可简化)
    public String getA() { return a; }
    public void setA(String a) { this.a = a; }
    public Integer getB() { return b; }
    public void setB(Integer b) { this.b = b; }
    public String getC() { return c; }
    public void setC(String c) { this.c = c; }
}
javascript 复制代码
@GetMapping("/example")
public String handleRequest(QueryParams params) {
    System.out.println(params.getA() + ", " + params.getB() + ", " + params.getC());
    return "OK";
}
相关推荐
Java内核笔记10 小时前
告别十亿美元的错误 : Spring Boot 4 空安全 (JSpecify) 实战
java·spring boot·后端
海棠Flower未眠12 小时前
SpringBoot 消息死信队列(荣耀典藏版)
java·数据库·spring boot
腾渊信息科技公司12 小时前
Spring Boot + TDengine:工业视觉检测数据实时同步方案实战
spring boot·后端·tdengine
weixin_BYSJ198714 小时前
「课设设计」springboot校园超市助购系统26449 (附源码)
java·javascript·spring boot·python·django·flask·php
奈何不吃鱼17 小时前
【SpringBoot】业务多线程四大实战场景
java·spring boot·多线程
奶糖 肥晨18 小时前
一次Spring Boot编译报错排查:三元运算符与包装类型的“隐形陷阱”
java·spring boot·后端
一只小小Java19 小时前
Naocs本地部署&安装3.2.3+Spring boot 3.2.0
java·spring boot·后端·nacos
JL1519 小时前
Spring Boot vs Gin:Java 人搭第一个 Go REST API
java·spring boot·gin
码兄科技1 天前
实战:基于Spring Boot + UniApp的地理信息小程序开发
spring boot·后端·uni-app
豆瓣鸡1 天前
Guava RateLimiter 限流实战:从令牌桶原理到 Nacos 动态配置
spring boot·微服务·nacos·guava