简体   繁体   中英

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. 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,

id=1&firstName=aaa&lastName=bbb

but it's giving 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.

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.

JSON:

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

XML

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM