@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";
}
相关推荐
xcl09256 小时前
从零开发流浪宠物领养平台:技术选型与核心模块设计
java·spring boot·宠物
李昊哲小课6 小时前
Spring Boot 4 旅游主题实战教程 阶段三:数据持久化
spring boot·后端·mybatis·旅游
wno7047 小时前
Spring-Boot-shiro用户认证
spring boot
paopaokaka_luck7 小时前
基于springboot3+vue3+uniapp的河南非遗数字图谱小程序(协同过滤算法、数字图谱展示、ECharts 图形化分析)
java·前端·spring boot·学习·小程序·uni-app·echarts
步行cgn7 小时前
传统 SSM 与 Spring Boot 开发对比:从配置地狱到约定优于配置
java·spring boot·后端
码视野17 小时前
基于 Spring Boot + Vue3 的【大学英语四六级 (CET-4/6) 作文智能评分与句式润色系统】设计与实现(含PRD/三端高保真源码/大屏)
java·前端·人工智能·spring boot·后端·vue3
李昊哲小课19 小时前
SpringBoot4 云端咖啡站 阶段二:数据访问与分层架构
spring boot·架构
宠友信息21 小时前
社区类源码开发实践中的仿小红书系统技术要点分析
java·spring boot·redis·mysql·uni-app·vue·内容运营
摇滚侠21 小时前
《SpringBoot 3:入门与应用实战》第 9 章 使用 WebMvc 开发应用 阅读笔记 1
spring boot·笔记·后端
李昊哲小课1 天前
SpringBoot4 云端咖啡站 阶段四:安全、文件与性能
spring boot·安全·性能优化·文件·性能