设为首页
收藏本站
首页
自丢网
产品
软件开发产品
教程
联系
联系我们
热搜
活动
交友
discuz
登录
自丢网
»
教程
›
【教程】编程记录
›
JAVA教程
›
spring boot请求接收参数post,put,delect,get参考案例代 ...
返回列表
发新帖
spring boot请求接收参数post,put,delect,get参考案例代码
[复制链接]
admin
发表于 2025-9-14 19:48:06
|
显示全部楼层
|
阅读模式
spring boot请求接收参数post,put,delect,get参考案例代码
参考案例一:
package com.jinhei.controller;
import com.jinhei.pojo.Dept;
import com.jinhei.pojo.Result;
import com.jinhei.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@Slf4j
@RequestMapping("/depts")
public class DeptController {
//private static final Logger log = LoggerFactory.getLogger(DeptController.class); //固定的
@Autowired
private DeptService deptService;
/**
* 查询部门列表
*/
//@RequestMapping(value = "/depts", method = RequestMethod.GET) //method: 指定请求方式
@GetMapping
public Result list(){
//System.out.println("查询全部部门数据");
log.info("查询全部部门数据");
List<Dept> deptList = deptService.findAll();
return Result.success(deptList);
}
/**
* 删除部门 - 方式一: HttpServletRequest 获取请求参数
*/
/*@DeleteMapping("/depts")
public Result delete(HttpServletRequest request){
String idStr = request.getParameter("id");
int id = Integer.parseInt(idStr);
System.out.println("根据ID删除部门: " + id);
return Result.success();
}*/
/**
* 删除部门 - 方式二: @RequestParam
* 注意事项: 一旦声明了@RequestParam, 该参数在请求时必须传递, 如果不传递将会报错. (默认 required 为 true)
*/
/*@DeleteMapping("/depts")
public Result delete(@RequestParam(value = "id", required = false) Integer deptId){
System.out.println("根据ID删除部门: " + deptId);
return Result.success();
}*/
/**
* 删除部门 - 方式三: 省略@RequestParam (前端传递的请求参数名与服务端方法形参名一致) [推荐]
* delete htt p://localhost:8080/depts?id=1
*/
@DeleteMapping
public Result delete(Integer id){
//System.out.println("根据ID删除部门: " + id);
log.info("根据ID删除部门: {}", id);
deptService.deleteById(id);
return Result.success();
}
/**
* 新增部门
* @RequestBody 的作用是:读取请求体:从客户端的 POST 请求的 Body 中读取 JSON 数据反序列化:将 JSON
* 数据转换为 Dept 类型的 Java 对象,自动映射字段(例如 JSON 的 name 映射到 Dept 的 name 属性)。
* 新增部门 - POST http://localhost:8080/depts 请求参数:{"name":"研发部"}
*/
@PostMapping
public Result add(@RequestBody Dept dept){
//System.out.println("新增部门: " + dept);
log.info("新增部门:{}", dept);
deptService.add(dept);
return Result.success();
}
/**
* 根据ID查询部门
*/
/*@GetMapping("/depts/{id}")
public Result getInfo(@PathVariable("id") Integer deptId){
System.out.println("根据ID查询部门 : " + deptId);
return Result.success();
路径参数接收:/depts/1,/depts/2 这种在url中传递的参数,我们称之为路径参数
需要使用 @PathVariable获取路径参数
}*/
/**
* 根据ID查询部门
*/
@GetMapping("/{id}")
public Result getInfo(@PathVariable Integer id){
//System.out.println("根据ID查询部门 : " + id);
log.info("根据ID查询部门: {}", id);
Dept dept = deptService.getById(id);
return Result.success(dept);
}
/**
* 修改部门
*/
@PutMapping
public Result update(@RequestBody Dept dept){
//System.out.println("修改部门: " + dept);
log.info("修改部门:{}", dept);
deptService.update(dept);
return Result.success();
}
}
复制代码
前端字符串,后端集合接收案例
@RequestParam
/**
* 菜品批量删除
*
* @param ids
* @return
*/
@DeleteMapping
@ApiOperation("菜品批量删除")
public Result delete(@RequestParam List<Long> ids) {
log.info("菜品批量删除:{}", ids);
dishService.deleteBatch(ids);//后绪步骤实现
return Result.success();
}
复制代码
springboot教程
,
java教程
相关帖子
•
java面向对象高级二(共3集)
•
java面向对象高级三完结(共3集)
•
lombok依赖
•
读取文本中的数据工具包hutool依赖
•
IOC详解Bean的声明以及DI详解,存在多个Bean解决方法
网站建设,公众号小程序开发,多商户单商户小程序制作,高端系统定制开发,App软件开发联系我们【手机/微信:
17817817816
】
回复
举报
返回列表
发新帖
微信扫码
Archiver
|
手机版
|
自丢网
网站建设,公众号小程序开发,商城小程序,系统定制开发,App软件开发等
粤ICP备2024252464号
在本版发帖
微信扫码
QQ客服
返回顶部
快速回复
返回顶部
返回列表