在 Spring Boot或 Spring MVC中GetMapping、PostMapping、PutMapping 和 DeleteMapping 是用于定义 RESTful 接口的 HTTP 方法专用注解它们是对 RequestMapping 的语义化封装使代码更清晰、简洁。注解HTTP 方法用途等价写法参数GetMappingGET获取资源查询RequestMapping(method RequestMethod.GET)PathVariable, RequestParam 单独使用PostMappingPOST创建资源 / 提交数据RequestMapping(method RequestMethod.POST)RequestBodyPutMappingPUT全量更新 资源RequestMapping(method RequestMethod.PUT)PathVariable, RequestBody 合用DeleteMappingDELETE删除资源RequestMapping(method RequestMethod.DELETE)PathVariableRestController RequestMapping(/api/users)publicclassUserController{// GET /api/users/1GetMapping(/{id})publicUsergetUser(PathVariable Long id){returnuserService.findById(id);}// GET /api/users?roleadminGetMappingpublicListUsergetUsers(RequestParam(requiredfalse)String role){returnuserService.findByRole(role);}// POST /api/users 请求体为 JSONPostMappingpublicUsercreateUser(RequestBody CreateUserRequest request){returnuserService.create(request);}// PUT /api/users/1 全量更新PutMapping(/{id})publicUserupdateUser(PathVariable Long id,RequestBody UpdateUserRequest request){returnuserService.updateFull(id,request);}// DELETE /api/users/1DeleteMapping(/{id})publicResponseEntityVoiddeleteUser(PathVariable Long id){userService.delete(id);returnResponseEntity.noContent().build();// 返回 204 No Content}}GetMappingGET /api/example?a1b2c3方法1GetMapping(/example)publicStringhandleRequest(RequestParam String a,RequestParam Integer b,RequestParam(requiredfalse)String c// 可选参数){System.out.println(aa, bb, cc);returnOK;}方法二封装到一个 POJO 对象中适合参数多或复用publicclassQueryParams{privateString a;privateInteger b;privateString c;// 必须有 getter/setterLombok 可简化publicStringgetA(){returna;}publicvoidsetA(String a){this.aa;}publicIntegergetB(){returnb;}publicvoidsetB(Integer b){this.bb;}publicStringgetC(){returnc;}publicvoidsetC(String c){this.cc;}}GetMapping(/example)publicStringhandleRequest(QueryParams params){System.out.println(params.getA(), params.getB(), params.getC());returnOK;}