How to submit JSON data as request body in Apache CXF jax-rs (REST)
I am using Apache-CXF for creating REST web services and trying to submit a form.
Server:
This is my method, which is expected to get json data.@POST
@Path("/addCustomer/")
@Consumes(MediaType.APPLICATION_JSON)
//{"Customer":{"name":"Some Name","id":6}}
public Customer addCustomer(Customer customer){
logger.debug(customer);
return customer;
}
Client: I am using firefox REST plugin for submitting request: Using REST client, I have posted following json as request body:
{"C开发者_Python百科ustomer":{"name":"Arnav Awasthi","id":6}}
But I am getting "415: Unsupported Media Type"
.
use restclient , a plugin for fire fox and add the http headers as Accept:application/json ,content-type: application/json.
You have to find a way to tell firefox to set the content-type to application/json. The error indicates that it's sending something else.
Sorry for the late answer, but it may serve to others.
You should doublecheck than your Customer class is annotated with JAXB's @XmlRootElement, since jackson needs it to deserialize JSON message.
I had the same error some time ago. It seems the root reason was exception "No message body reader has been found for request class ".
According to http://www.javatips.net/blog/2012/02/cxf-restful-tutorial I added jettison library to resolve this issue.
I faced the same issue using CXF 2.7.4 with Jasckon 2.X.X . But it was fixed when i upgraded to CXF 2.7.7 . Or use Jackson 1.9.X with CXF 2.7.4 .
You have to add custom headers to inform the client what kind of data you are sending back e.g: Header Name: Content-type Header-Value : application/json
I had the same problem. The solution was to remove the bean class name from the json string. In your case, the Json which should be sent as the body would be,
{"name":"Arnav Awasthi","id":6}
精彩评论