[英]How to send post request with spring @RequestBody in rest client
I have a class Person. 我有一个班级的人。
class Person{
Integer id;
String firstName;
String lastName;
//other params, constructors, getters & setters
}
& My method is 我的方法是
@RequestMapping(value = "/test", method = RequestMethod.POST)
public void testPerson(
@RequestBody Person person){
...
}
Now I need to test it using rest client. 现在我需要使用rest client测试它。 I tried setting up “request header” section of the Firefox plugin to have a “name” = “Content-Type” and “value” = “application/x-www-form-urlencoded” & then add parameters in body, 我尝试设置Firefox插件的“请求标题”部分,使其具有“name”=“Content-Type”和“value”=“application / x-www-form-urlencoded”然后在body中添加参数,
id=1&firstName=aaa&lastName=bbb
but it's giving 404. 但它给了404。
If you are getting 404
response, this means either your request URL is wrong or you using GET
method instead of POST
or vise versa. 如果您收到404
响应,则表示您的请求URL错误或您使用GET
方法而不是POST
,反之亦然。
Then regarding passing Person
in request, if @RequestBody
is used you have to pass JSON or XML in the body of the request as playload. 然后关于在请求中传递Person
,如果使用@RequestBody
,则必须在请求正文中将JSON或XML作为playload传递。
JSON: JSON:
{
"id":1,
"firstName":"aaa",
"lastName":bbb
}
XML XML
<person>
<id>1<id>
<firstName>aaa</firstName>
<lastName>bbb</lastName>
</person>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.