How to send JSON data as request body in Apache CXF jax-rs (REST)

I am using Apache-CXF to create REST web services and trying to submit a form.

Server:
This is my method which is expected to receive 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 use the firefox REST plugin to send the request: Using the REST client, I sent the following json as the request body:

 {"Customer":{"name":"Arnav Awasthi","id":6}} 

But I get "415: Unsupported Media Type" .

+6
source share
7 answers

use restclient plugin for fire fox and add http headers as Accept: application / json, content-type: application / json.

+3
source

You need to find a way to tell firefox to set the content type to application / json. The error indicates that it is sending something else.

+2
source

Sorry for the late reply, but it may serve others.

You must double what your client class is annotated with JAXB @XmlRootElement, since Jackson needs to deserialize his JSON message.

+2
source

I had the same error a while ago. It seems that the main reason was the exception: "No message body readers were found for the query class."

According to http://www.javatips.net/blog/2012/02/cxf-restful-tutorial I added a dump library to solve this problem.

+1
source

I ran into the same problem using CXF 2.7.4 with Jasckon 2.XX But this was fixed when I upgraded to CXF 2.7.7. Or use Jackson 1.9.X with CXF 2.7.4.

+1
source

You need to add custom headers to tell the client what data you are sending back, for example: Header Header: Content Type Header Value: application / json

0
source

I had the same problem. The solution was to remove the bean class name from the json string. In your case, Json, which should be sent as body, will be

 {"name":"Arnav Awasthi","id":6} 
0
source

Source: https://habr.com/ru/post/887027/


All Articles