惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
A
About on SuperTechFans
量子位
Engineering at Meta
Engineering at Meta
B
Blog
The Cloudflare Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
J
Java Code Geeks
D
DataBreaches.Net
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 华安

自适应网格系统:CSS Grid中repeat()、auto-fill与auto-fit的深度解析 CSS3中响应式布局两大神器display:flex和display:grid springBoot中的 pom.xml文件 bulid学习 CSS中元素的display显示方式有多种,隐藏、块级、内联、内联-块级 SQl Server 中的 go 是什么作用 CSS中 display:flex的align-items: stretch; 移动端浏览器(尤其是 iOS Safari)的橡皮筋回弹效果(Overscroll / Bounce Effect) 手机端浏览器上ES6中的Fetch回调执行 window.open没效果 用Flex实现兼容性好的全屏布局 在 VS Code 中使用 C# Dev Kit 和 Unity Tools 调试 Unity 2022 Unity 可编程物件(ScriptableObject) 微信小程序中的 联系客服 最基本的使用方法 wx.requestSubscribeMessage(Object object) 和 wx.requestSubscribeDeviceMessage(Object object) 这两个有什么区别 微信小程序中 wx.hideLoading() 后调用 wx.showToast()的问题 Windows 中启动 Nginx的常用命令 CSS进阶技巧:字体渐变、描边、倒影与渐变色描边全解析 netCore 中各DLL引用了 SkiaSharp.dll的问题 unity中预制体解包 在 Unity 中,Time.timeScale实现游戏暂停加速等 微信小程序中关联微信支付 unity中的 Navigation AI使用 C#中TaskCompletionSource(简称 TCS)学习 Unity 编辑器 中,快捷键 Ctrl + Shift + F 的功能 unity中按下 F键,让物体聚焦 微信中进入定页面的,判断时通过扫二维码进入的,还是点小程序名称进入的 MYSQL中从JSON字符串中提取指定的值 Unity2022中创建动画 Animation(旧方法) Unity 中区别,public 和 [SerializeField] Unity中 onCollisionEnter2D与OnTriggerEnter2D 区别 asp.netCore中给动态请求路径加客户端缓存
springboot获取post请求参数
华安 · 2025-12-10 · via 博客园 - 华安

在Spring Boot中,获取POST请求参数的方法取决于你使用的数据绑定方式。以下是几种常见的方式:

1. 使用@RequestParam

如果你的POST请求的参数在URL的查询字符串中(例如,POST /example?param1=value1),你可以使用@RequestParam注解来获取这些参数。

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExampleController {

    @GetMapping("/example")
    public String getExample(@RequestParam(name = "param1", required = false) String param1) {
        return "Received: " + param1;
    }
}

2. 使用@RequestBody

如果你的POST请求的主体(body)包含JSON或XML格式的数据,你可以使用@RequestBody注解来绑定请求体中的数据到一个Java对象。

首先,定义一个Java类来映射请求体中的数据:

public class RequestBodyExample {
    private String field1;
    private int field2;
    // getters and setters
}

然后,在你的控制器中使用@RequestBody注解:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExampleController {

    @PostMapping("/example")
    public String postExample(@RequestBody RequestBodyExample body) {
        return "Received: " + body.getField1() + ", " + body.getField2();
    }
}
 @ResponseBody
    @PostMapping("/addsave")
    public Result<String> addsave(@RequestBody Map<String, String> requestMap,
                                  Model model
                          )throws IOException {
        String account=requestMap.get("account");
        String nickname=requestMap.get("nickname");
        String status=requestMap.get("status");
}

3. 使用@ModelAttribute@RequestBody结合DTO(Data Transfer Object)

如果你需要从表单数据(例如,HTML表单提交)中获取数据,可以使用@ModelAttribute注解。但如果你的表单数据是以JSON格式提交的,最好还是使用@RequestBody

4. 使用HttpServletRequest获取原始参数(不推荐)

虽然不推荐这种方法,因为Spring提供了更简洁的数据绑定方式,但如果你需要直接访问底层的HttpServletRequest对象,可以这样做:

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExampleController {

    @PostMapping("/example")
    public String postExample(HttpServletRequest request) {
        String param1 = request.getParameter("param1"); // 获取单个参数
        return "Received: " + param1;
    }
}

总结:

  • 对于JSON或XML格式的请求体,使用@RequestBody
  • 对于表单数据(尤其是JSON格式),优先考虑使用@RequestBody配合DTO。
  • 对于URL查询参数,使用@RequestParam
  • 直接操作HttpServletRequest通常不推荐,除非你有特殊需求。使用Spring提供的数据绑定方式会更清晰、更安全。