如何在 rest 客户端中使用 spring @RequestBody 发送 post 请求

How to send post request with spring @RequestBody in rest client

我有一个 class 人。

class Person{
Integer id;
String firstName;
String lastName;
//other params, constructors, getters & setters
}

&我的方法是

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public void testPerson(
            @RequestBody Person person){
...
}

现在我需要使用 rest 客户端对其进行测试。我尝试将 Firefox 插件的“request header”部分设置为“name”=“Content-Type”和“value”=“application/x-www-form-urlencoded” & 然后在body,

中添加参数
id=1&firstName=aaa&lastName=bbb

但它给出了 404。

如果您收到 404 响应,这意味着您的请求 URL 是错误的,或者您使用的是 GET 方法而不是 POST,反之亦然。

然后关于在请求中传递Person,如果使用@RequestBody,则必须在请求正文中传递JSON或XML作为播放负载。

JSON:

{
  "id":1,
  "firstName":"aaa",
  "lastName":bbb
}

XML

<person>
  <id>1<id>
  <firstName>aaa</firstName>
  <lastName>bbb</lastName>  
</person>