@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";
}
相关推荐
linmengmeng_13149 小时前
【总结】MyBatis-Plus批量插入与清表的正确姿势
java·spring boot·mysql·mybatis
高级程序源9 小时前
django大学生创新创业项目管理系统94923-计算机课程设计、毕业设计
javascript·vue.js·spring boot·后端·python·django·课程设计
暮雨哀尘11 小时前
JAVA基础练习(四):SpringBoot 实现简易登录功能
java·spring boot·eclipse·maven
VXbishe13 小时前
【课程设计】基于SpringBoot的乡村政务服务系统的设计与实现-计算机毕设77011
javascript·vue.js·spring boot·python·php·课程设计·政务
vx_BS8133015 小时前
【项目编号:project51381】Spring Boot 婚纱摄影管理系统:套餐预约、在线咨询、支付与客片展示的一站式业务实现
java·spring boot·eclipse·tomcat·mybatis
雪芽蓝域zzs15 小时前
第11节:分页查询(MyBatis 实现分页,后端最常用功能)
spring boot
计算机毕设定制辅导-无忧学长15 小时前
《基于SpringBoot的中学教师数字胜任力测评网站的设计与实现》
java·vue.js·spring boot·mysql·中学教师数字胜任力测评网站
小蒜学长15 小时前
基于Java的论坛数据可视化分析系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·数据可视化·论坛系统
黑马程序员毕设16 小时前
基于微信小程序的二手交易平台设计与实现报告
前端·人工智能·spring boot·后端·考研