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

推荐订阅源

AI
AI
O
OpenAI News
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
Jina AI
Jina AI
D
Docker
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
雷峰网
雷峰网
V
V2EX
小众软件
小众软件
N
News | PayPal Newsroom
GbyAI
GbyAI
Recorded Future
Recorded Future
SecWiki News
SecWiki News
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
Security Latest
Security Latest
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
Cyberwarzone
Cyberwarzone
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
U
Unit 42
The Last Watchdog
The Last Watchdog
V
Visual Studio Blog
C
Cisco Blogs
T
Tor Project blog
Google Online Security Blog
Google Online Security Blog
I
InfoQ
Attack and Defense Labs
Attack and Defense Labs
Y
Y Combinator Blog
博客园 - 聂微东
L
LangChain Blog
Blog — PlanetScale
Blog — PlanetScale
Apple Machine Learning Research
Apple Machine Learning Research
S
Schneier on Security
S
Securelist
博客园_首页
W
WeLiveSecurity
P
Privacy International News Feed
S
SegmentFault 最新的问题
博客园 - 【当耐特】
L
LINUX DO - 热门话题
Latest news
Latest news
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence

博客园 - 华安

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