spring boot请求接收参数post,put,delect,get参考案例代码

[复制链接]
admin 发表于 2025-9-14 19:48:06 | 显示全部楼层 |阅读模式
spring boot请求接收参数post,put,delect,get参考案例代码

参考案例一:
  1. package com.jinhei.controller;

  2. import com.jinhei.pojo.Dept;
  3. import com.jinhei.pojo.Result;
  4. import com.jinhei.service.DeptService;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.*;

  8. import java.util.List;

  9. @RestController
  10. @Slf4j
  11. @RequestMapping("/depts")
  12. public class DeptController {
  13.     //private static final Logger log = LoggerFactory.getLogger(DeptController.class); //固定的

  14.     @Autowired
  15.     private DeptService deptService;

  16.     /**
  17.      * 查询部门列表
  18.      */
  19.     //@RequestMapping(value = "/depts", method = RequestMethod.GET) //method: 指定请求方式
  20.     @GetMapping
  21.     public Result list(){
  22.         //System.out.println("查询全部部门数据");
  23.         log.info("查询全部部门数据");
  24.         List<Dept> deptList = deptService.findAll();
  25.         return Result.success(deptList);
  26.     }

  27.     /**
  28.      * 删除部门 - 方式一: HttpServletRequest 获取请求参数
  29.      */
  30.     /*@DeleteMapping("/depts")
  31.     public Result delete(HttpServletRequest request){
  32.         String idStr = request.getParameter("id");
  33.         int id = Integer.parseInt(idStr);
  34.         System.out.println("根据ID删除部门: " + id);
  35.         return Result.success();
  36.     }*/


  37.     /**
  38.      * 删除部门 - 方式二: @RequestParam
  39.      * 注意事项: 一旦声明了@RequestParam, 该参数在请求时必须传递, 如果不传递将会报错. (默认 required 为 true)
  40.      */
  41.     /*@DeleteMapping("/depts")
  42.     public Result delete(@RequestParam(value = "id", required = false) Integer deptId){
  43.         System.out.println("根据ID删除部门: " + deptId);
  44.         return Result.success();
  45.     }*/

  46.     /**
  47.      * 删除部门 - 方式三: 省略@RequestParam (前端传递的请求参数名与服务端方法形参名一致) [推荐]
  48.      *  delete htt p://localhost:8080/depts?id=1
  49.      */
  50.     @DeleteMapping
  51.     public Result delete(Integer id){
  52.         //System.out.println("根据ID删除部门: " + id);
  53.         log.info("根据ID删除部门: {}", id);
  54.         deptService.deleteById(id);
  55.         return Result.success();
  56.     }

  57.     /**
  58.      * 新增部门
  59.      * @RequestBody 的作用是:读取请求体:从客户端的 POST 请求的 Body 中读取 JSON 数据反序列化:将 JSON
  60.      * 数据转换为 Dept 类型的 Java 对象,自动映射字段(例如 JSON 的 name 映射到 Dept 的 name 属性)。
  61.      * 新增部门 - POST http://localhost:8080/depts   请求参数:{"name":"研发部"}
  62.      */
  63.     @PostMapping
  64.     public Result add(@RequestBody Dept dept){
  65.         //System.out.println("新增部门: " + dept);
  66.         log.info("新增部门:{}", dept);
  67.         deptService.add(dept);
  68.         return Result.success();
  69.     }

  70.     /**
  71.      * 根据ID查询部门
  72.      */
  73.     /*@GetMapping("/depts/{id}")
  74.     public Result getInfo(@PathVariable("id") Integer deptId){
  75.         System.out.println("根据ID查询部门 : " + deptId);
  76.         return Result.success();
  77.         路径参数接收:/depts/1,/depts/2 这种在url中传递的参数,我们称之为路径参数
  78.         需要使用 @PathVariable获取路径参数
  79.     }*/

  80.     /**
  81.      * 根据ID查询部门
  82.      */
  83.     @GetMapping("/{id}")
  84.     public Result getInfo(@PathVariable Integer id){
  85.         //System.out.println("根据ID查询部门 : " + id);
  86.         log.info("根据ID查询部门: {}", id);
  87.         Dept dept = deptService.getById(id);
  88.         return Result.success(dept);
  89.     }


  90.     /**
  91.      * 修改部门
  92.      */
  93.     @PutMapping
  94.     public Result update(@RequestBody Dept dept){
  95.         //System.out.println("修改部门: " + dept);
  96.         log.info("修改部门:{}", dept);
  97.         deptService.update(dept);
  98.         return Result.success();
  99.     }
  100. }
复制代码
前端字符串,后端集合接收案例@RequestParam
  1. /**
  2.      * 菜品批量删除
  3.      *
  4.      * @param ids
  5.      * @return
  6.      */
  7.     @DeleteMapping
  8.     @ApiOperation("菜品批量删除")
  9.     public Result delete(@RequestParam List<Long> ids) {
  10.         log.info("菜品批量删除:{}", ids);
  11.         dishService.deleteBatch(ids);//后绪步骤实现
  12.         return Result.success();
  13.     }
复制代码

网站建设,公众号小程序开发,多商户单商户小程序制作,高端系统定制开发,App软件开发联系我们【手机/微信:17817817816
微信扫码

网站建设,公众号小程序开发,商城小程序,系统定制开发,App软件开发等

粤ICP备2024252464号

在本版发帖
微信扫码
QQ客服返回顶部
快速回复 返回顶部 返回列表