如何在rest客户端中使用spring@RequestBody发送post请求
我有一个班上的人如何在rest客户端中使用spring@RequestBody发送post请求,spring,rest,post,firefox-addon,Spring,Rest,Post,Firefox Addon,我有一个班上的人 class Person{ Integer id; String firstName; String lastName; //other params, constructors, getters & setters } &我的方法是 @RequestMapping(value = "/test", method = RequestMethod.POST) public void testPerson( @RequestBody
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插件的“请求头”部分设置为“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作为playload
JSON:
XML
1.
aaa
bbb
您需要以json格式发送有效负载。我添加了这两个标题。Accept:application/json Content Type:application/json&从body.com发送json请求,如果您正在使用Jackson或其他库将json转换为对象。不要忘记向对象添加空构造函数
{
"id":1,
"firstName":"aaa",
"lastName":bbb
}
<person>
<id>1<id>
<firstName>aaa</firstName>
<lastName>bbb</lastName>
</person>