Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A message body writer for Java type, class net.sf.json.JSONObject, and MIME media type, application/json, was not found

Tags:

json

rest

jersey

I am using jersey client for making call to rest webservice.

My webservice is consuming the json, so i need to put json in making call to my webservice provider.

I am doing it in below way.

    JSONObject object=new JSONObject();
    object.put("name", employee.getName());
    object.put("empId", employee.getEmpId());
    object.put("organizationName", employee.getOrganizationName());


ClientResponse response = service.path("rest").path("vtn").path("addEmplyee")
            .type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, object);

but i am getting the below exception:

09:52:01,625 ERROR [[mvc-dispatcher]] Servlet.service() for servlet mvc-dispatcher threw exception com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class net.sf.json.JSONObject, and MIME media type, application/json, was not found at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147) at com.sun.jersey.api.client.Client.handle(Client.java:648) at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670) at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563) at com.nec.jp.pflow.unc.service.EmployeeService.addEmployee(EmployeeService.java:44) at com.nec.jp.pflow.unc.controller.EmployeeController.addCustomer(EmployeeController.java:29) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

But if i convert my json to string representation like :

String input = "{\"name\" : \"" + employee.getName() + "\",\"empId\" : \"" + employee.getEmpId() + "\",\"organizationName\" : \"" + employee.getOrganizationName() + "\"}";

ClientResponse response = service.path("rest").path("vtn").path("addEmplyee")
            .type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, input);

then it is working fine.

Please suggest how can i put my JSON Object without getting the above exception. What is the best way?

Thanks in advance.


I got a solution for the above. Now I am making use of jackson-mapper api for converting the POJO to json.

Below is the code snippet.

ObjectMapper mapper = new ObjectMapper();
ClientResponse response = resource.path("rest").path("vtn").path("addEmplyee")
        .type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, mapper.writeValueAsString(employee)); 
like image 278
sorabh solanki Avatar asked Aug 21 '12 05:08

sorabh solanki


4 Answers

A better way is to tell jersey-client that it may use jackson for serialization: https://stackoverflow.com/a/2580315/516188

Pasting the setup:

ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(JacksonJsonProvider.class);
Client clientWithJacksonSerializer = Client.create(cc);

JacksonJsonProvider is in the jackson-jaxrs-json-provider package.

like image 172
Emmanuel Touzery Avatar answered Nov 14 '22 21:11

Emmanuel Touzery


Just copying the solution from the question above, just in case someone is looking for answer.

Use jackson-mapper api for converting the POJO to json as shown in the code snippet below:

ObjectMapper mapper = new ObjectMapper();
ClientResponse response = resource.path("rest").path("vtn").path("addEmplyee")
        .type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, mapper.writeValueAsString(employee)); 
like image 8
Jugal Shah Avatar answered Nov 14 '22 21:11

Jugal Shah


adding a .toString() worked for me:

JSONObject object=new JSONObject();
object.put("name", employee.getName());
object.put("empId", employee.getEmpId());
object.put("organizationName", employee.getOrganizationName());


ClientResponse response = service.path("rest").path("vtn").path("addEmplyee").type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class,object.toString());
like image 5
xparticle Avatar answered Nov 14 '22 22:11

xparticle


JSONObject object=new JSONObject();

need to provide @XMLrootElement Annotation....if you are accessing the web service from remote....i.e XML Mapping..for Model class.

like image 2
jaskirat Singh Avatar answered Nov 14 '22 20:11

jaskirat Singh