You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为变量添加@RequestBody?Postman请求及路径变量异常排查

问题解答

一、Postman请求接口的正确方式

首先你的接口存在一个问题:Spring MVC不支持一个请求中使用多个@RequestBody注解,一个POST请求只能接收一个请求体。需要先修改接口代码,把密码参数封装成一个DTO类:

// 定义请求DTO
public class ResetPasswordRequest {
    private String enteredPassword;
    private String reenterPassword;
    
    // 生成getter、setter方法
}

// 修改后的接口
@PostMapping("/api/v1.0/{username}/forgot")
public void forgotPassword(@PathVariable String username, @RequestBody ResetPasswordRequest request) {
    userServiceImpl.forgotPassword(username, request.getEnteredPassword(), request.getReenterPassword());
}

之后在Postman中按以下步骤请求:

  • 请求方法选择POST
  • 填写请求URL:将路径中的{username}替换为实际用户名,例如http://localhost:8080/api/v1.0/sravani/forgot
  • 切换到Body标签,选择raw格式,下拉菜单中选JSON
  • 在输入框中填写请求体内容:
{
    "enteredPassword": "your-new-password",
    "reenterPassword": "confirm-new-password"
}
  • 点击Send发送请求

二、MissingPathVariableException异常排查

错误提示要求的路径变量是email,但你提供的接口中使用的是username,原因可能有以下几种:

  1. 请求了错误的接口:你实际发送的请求不是当前的/api/v1.0/{username}/forgot接口,而是另一个需要{email}路径变量的接口
  2. 代码存在冲突接口:你的项目中还有其他接口方法,使用了{email}作为路径变量,但请求时没有在URL中传入该参数
  3. 应用未重启:修改接口后没有重启Spring Boot应用,旧的接口定义仍在生效
  4. URL拼写错误:请求URL中误将username写成email,或者漏传了路径变量

内容的提问来源于stack exchange,提问作者Sravani Upputuri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 09:39:28