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));
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.
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));
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());
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.
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