找不到用于Java类型net.sf.json.JSONObject和MIME媒体类型application/json的消息正文编写器 [英] A message body writer for Java type, class net.sf.json.JSONObject, and MIME media type, application/json, was not found
问题描述
我正在使用jersey客户端拨打电话来休息Web服务.
I am using jersey client for making call to rest webservice.
我的网络服务正在使用json,因此我需要在向我的网络服务提供商打电话时使用json.
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错误[[mvc-dispatcher]] Servlet的Servlet.service() mvc-dispatcher抛出异常 com.sun.jersey.api.client.ClientHandlerException:Java类型,类net.sf.json.JSONObject和MIME媒体的消息正文编写器 找不到类型application/json 在com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288) 在com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204) 在com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147) 在com.sun.jersey.api.client.Client.handle(Client.java:648) 在com.sun.jersey.api.client.WebResource.handle(WebResource.java:670) 位于com.sun.jersey.api.client.WebResource.access $ 200(WebResource.java:74) 在com.sun.jersey.api.client.WebResource $ Builder.post(WebResource.java:563) 在com.nec.jp.pflow.unc.service.EmployeeService.addEmployee(EmployeeService.java:44) 在com.nec.jp.pflow.unc.controller.EmployeeController.addCustomer(EmployeeController.java:29) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
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)
但是如果我将json转换为字符串表示形式,例如:
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);
然后工作正常.
请提出如何在不出现上述异常的情况下放置JSON对象的建议.最好的方法是什么?
Please suggest how can i put my JSON Object without getting the above exception. What is the best way?
谢谢.
我为上述问题找到了解决方案.现在,我正在使用jackson-mapper api将POJO转换为json.
I got a solution for the above. Now I am making use of jackson-mapper api for converting the POJO to json.
下面是代码段.
ObjectMapper mapper = new ObjectMapper();
ClientResponse response = resource.path("rest").path("vtn").path("addEmplyee")
.type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, mapper.writeValueAsString(employee));
推荐答案
更好的方法是告诉jersey-client
它可以使用jackson
进行序列化:
https://stackoverflow.com/a/2580315/516188
A better way is to tell jersey-client
that it may use jackson
for serialization:
https://stackoverflow.com/a/2580315/516188
粘贴设置:
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(JacksonJsonProvider.class);
Client clientWithJacksonSerializer = Client.create(cc);
JacksonJsonProvider
在jackson-jaxrs-json-provider
软件包中.
这篇关于找不到用于Java类型net.sf.json.JSONObject和MIME媒体类型application/json的消息正文编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!