I can successfully post XML data to my service, but, trying to do the same using JSON is failing. The POJO is:
@XmlRootElement
public class Address {
String city;
String zip;
//Getters & setters...
}
The service resource is:
@POST
@Produces("application/json")
public Address fix(Address a) {
return a;
}
I am doing a POST as follows:
POST /AcmeWeb/svc/simple HTTP/1.1
Content-Length: 30
Content-Type: application/json; charset=UTF-8
{"city":"Miami","zip":"33130"}
The server is responding with a 400 Bad Request. I scoured the Internet but found no good example of posting JSON. Any help is appreciated. Thanks.
Add @Consumes("application/json")
annotation to your fix()
method.
Update: This works for me:
Resource method:
@POST
@Produces("application/json")
@Consumes("application/json")
public Address post(Address addr) {
return addr;
}
Address class:
@XmlRootElement
public class Address {
public String city;
public String zip;
}
This is the request I am sending:
Accept application/json
Content-Type application/json; charset=UTF-8
{"city":"Miami","zip":"33130"}
This is what I get as a response:
{"city":"Miami","zip":"33130"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With