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

Angular发送带必填参数的POST请求时出现MissingServletRequestParameterException

问题根源分析

你遇到的MissingServletRequestParameterException错误,核心是前后端参数传递方式不匹配:

  • 后端通过@RequestParam注解,要求参数从URL的查询参数(即?key=value格式)中获取;
  • 但你的Angular代码是把email和password放在了POST请求的JSON请求体里,后端无法识别这种方式传递的参数,因此抛出错误。而Postman测试时你是把参数放在URL中,完全符合后端的参数接收规则,所以请求成功。

解决方法1:修改Angular代码,传递查询参数(和Postman请求方式对齐)

调整Angular的请求逻辑,将登录参数作为查询参数拼在URL后,和你在Postman的操作一致:

// service
export class LoginService {
  private apiUrl = 'http://localhost:55624/doctors/login';
  constructor(private http: HttpClient) { }

  login(email: string, password: string): Observable<any> {
    // 构造查询参数
    const params = new HttpParams()
      .set('email', email)
      .set('password', password);

    // POST请求体传空对象,参数通过params选项传递
    return this.http.post(this.apiUrl, {}, { params });
  }
}

修改后,请求URL会自动拼接成http://localhost:55624/doctors/login?email=xxx&password=xxx,和后端的参数接收规则匹配,即可正常获取参数。


解决方法2:修改后端代码,接收请求体参数(更符合REST规范)

登录数据属于敏感信息,放在URL中存在安全风险,更合理的方式是让后端接收请求体中的JSON数据:

  1. 先创建一个登录请求的DTO类(规范写法,也可以直接用Map接收):
public class LoginRequest {
    private String email;
    private String password;

    // 生成getter和setter方法
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }
}
  1. 修改Controller的登录方法,用@RequestBody注解接收参数:
@PostMapping("/login")
public Map<String, Object> loginDoctor(@RequestBody LoginRequest loginRequest) {
    String email = loginRequest.getEmail();
    String password = loginRequest.getPassword();
    
    Doctor doctor = doctorRepository.findByEmail(email);
    
    if (doctor != null && doctor.getPassword().equals(password)) {
        return Response.success("Login successful", doctor);
    } else {
        return Response.failure("Invalid email or password", null);
    }
}

这种方式下,你的Angular代码无需任何改动,后端就能直接解析请求体中的JSON参数,同时也符合POST请求传递数据的最佳实践。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 18:32:50