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

推荐订阅源

D
Docker
MyScale Blog
MyScale Blog
WordPress大学
WordPress大学
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
月光博客
月光博客
罗磊的独立博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
F
Full Disclosure
Simon Willison's Weblog
Simon Willison's Weblog
D
DataBreaches.Net
T
Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
阮一峰的网络日志
阮一峰的网络日志
TaoSecurity Blog
TaoSecurity Blog
Microsoft Azure Blog
Microsoft Azure Blog
Scott Helme
Scott Helme
S
Securelist
W
WeLiveSecurity
K
Kaspersky official blog
The GitHub Blog
The GitHub Blog
Attack and Defense Labs
Attack and Defense Labs
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
Stack Overflow Blog
Stack Overflow Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Security Latest
Security Latest
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
The Last Watchdog
The Last Watchdog
C
Check Point Blog
T
Troy Hunt's Blog
P
Proofpoint News Feed
J
Java Code Geeks
G
Google Developers Blog
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
S
Security @ Cisco Blogs
宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
L
LINUX DO - 最新话题
Jina AI
Jina AI

博客园 - 华安

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提供的数据绑定方式会更清晰、更安全。