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

推荐订阅源

N
Netflix TechBlog - Medium
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
NISL@THU
NISL@THU
MongoDB | Blog
MongoDB | Blog
Microsoft Security Blog
Microsoft Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
The Register - Security
The Register - Security
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
J
Java Code Geeks
S
Securelist
Y
Y Combinator Blog
T
Threat Research - Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Vercel News
Vercel News
W
WeLiveSecurity
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
Martin Fowler
Martin Fowler
TaoSecurity Blog
TaoSecurity Blog
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
L
LINUX DO - 热门话题
T
Tailwind CSS Blog
T
Tor Project blog
博客园 - 叶小钗
Blog — PlanetScale
Blog — PlanetScale
I
Intezer
V
V2EX
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
The GitHub Blog
The GitHub Blog
量子位
AI
AI
Cyberwarzone
Cyberwarzone
T
Troy Hunt's Blog
N
News | PayPal Newsroom
H
Help Net Security

博客园 - 华安

Unity 相机:正交 (Orthographic) vs 透视 (Perspective) - 华安 unity中的button中的onclick控制面板中四个参数的意思分别是什么 - 华安 微信小程序开发中触发 onshow的几种特殊情况 SQL Server备份心得 MYSQL 备份数据库 微信与支付支付功能开发 微信小程序中页面配置下拉刷新 unity中 相机没有视锥效果线框了,如何打开 JSONPath表达式 C# 中的操作JSON类 JObject cookie中的 HttpOnly 、Secure、SameSite 解释 Spring-boot 中基于 IP 的限流和自动封禁 Filter 登录 用 HMAC-SHA256 实现 TwoFA(二重验证)的坑 利用Spring Boot的 filter 结合ConcurrentHashMap 实现“同一IP每分钟最多允许300个404请求,超出后禁用30分钟访问” pixi-filters中的BackdropBlurFilter使用注意事项 PixiJS中的 SplitBitmapText.chars中的每个 BitmapText的x值分析 legend隐藏不想显示的图例 spring-boot HttpServletResponse response.sendRedirect是会跳转到 http而不是https Javascript如何判断是触摸屏还是PC端 JavaScript获取鼠标点一个元素,获取鼠标点击的元素内的位置 spring-boot中配置Mongodbd的问题小结 Rest Template中添加 PATCH请求。 CSS实现修改CheckBox样式 SpringBoot+Thyemleaf报错:Error resolving template Template might not exist or might not be accessible div display flex 如何出现横向滚动条
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提供的数据绑定方式会更清晰、更安全。