You need to enable JavaScript to run this app. 如何解决Spring Boot与Angular场景下的MissingServletRequestParameterException错误
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Spring Boot与Angular场景下的MissingServletRequestParameterException错误

问题根源

你调用Angular HttpClient.post 方法时参数顺序不符合规范,导致参数配置未生效:
HttpClient.post 标准传参顺序为 post(请求URL, 请求体, 配置项),你当前把headers、params等配置项放到了第二个「请求体」参数位,params没有被正确解析到URL的查询参数中,所以后端抛出MissingServletRequestParameterException异常。

修复方案

方案1:保持后端现有接口逻辑不变

后端仍用@RequestParam接收tripName,仅调整前端传参顺序,把配置项放到第三个参数位,第二个参数传null(无请求体需要发送):

public createSuccessMessage(userId: number,tripName: string): Observable<any> {      
    return this.http.post(`${AppComponent.API}/Success/${userId}`, 
    // 第二个参数为请求体,无请求体传null
    null,
    // 第三个参数放配置项
    {      
      headers: new HttpHeaders({ 'Accept': 'text/plain' }),
      params: {        
        tripName: tripName
      },
      observe: 'response'
    });
  }

提示:当前你没有传递请求体,不需要设置Content-Type为text/plain,删除该配置即可。

方案2:改为传递JSON请求体(适合参数较多的场景)

如果要通过请求体传JSON,前后端对应调整即可:

  • 前端代码调整:
public createSuccessMessage(userId: number,tripName: string): Observable<any> {      
    return this.http.post(`${AppComponent.API}/Success/${userId}`, 
    // 第二个参数放请求体JSON
    { tripName: tripName },
    {      
      headers: new HttpHeaders({ 'Accept': 'text/plain', 'Content-Type': 'application/json' }),
      observe: 'response'
    });
  }
  • 后端接口调整:
@PostMapping("/Success/{userId}")   
public void createSuccessMessage(@PathVariable("userId") String userId, @RequestBody Map<String, String> req) {    
    messagesService.createSuccessMessage(Long.valueOf(userId), req.get("tripName"));       
}

验证方式

修改代码后打开浏览器开发者工具的「网络」面板,查看请求:

  • 方案1的请求URL末尾会自动拼接?tripName=xxx
  • 方案2的请求负载中会展示传递的JSON结构

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 04:36:02