首页 > 解决方案 > 异常:org.springframework.web.HttpRequestMethodNotSupportedException 使用 Spring post 方法时

问题描述

我有以下代码在我的控制器中映射发布 API。我已经为 POST 请求参数定义了一个模型。

public class CreateUserBody {
    String CandidateId = "";
    String EmployeeId = "";
    String CompanyId = "";

    public String getCandidateId() {
        return CandidateId;
    }
    public String getEmployeeId() {
        return EmployeeId;
    }
    public String getCompanyId() {
        return CompanyId;
    }
}

当我触发这个 API 时,我得到了 HttpRequestMethodNotSupportedException。请帮我解决这个问题。

  @RequestMapping(value = "/SetEmployeeIdOneByOne/", method = RequestMethod.POST)
    public @ResponseBody void SetEmployeeIdOneByOne(@RequestBody CreateUserBody createUserBody) throws Exception{
        String CandidateId = createUserBody.getCandidateId();
        String EmployeeId = createUserBody.getEmployeeId();
        String CompanyId = createUserBody.getCompanyId();
        SetEmployeeID.SetEmployeeIDOneByOneInteger(CandidateId, EmployeeId, CompanyId);
    }

我正在调用 API,例如...

在此处输入图像描述

标签: javaspringpostrequest

解决方案


而不是将数据放入form-data发布您的数据raw,然后选择内容类型为application/json. 在正文中,您的数据应如下所示:

{
  "CandidateId":"3",
  "EmployeeId":"5696969",
  "CompanyId":"15272"
}

您在邮递员中请求的网址应如下所示:

http://localhost:9199/SetEmployeeIdOneByOne/

推荐阅读