json - 未找到 Java 类型类 net.sf.json.JSONObject 和 MIME 媒体类型 application/json 的消息正文编写器

标签 json rest jersey

我正在使用 Jersey 客户端来调用休息网络服务。

我的网络服务正在使用 json,所以我需要将 json 放入我的网络服务提供者的调用中。

我正在按照以下方式进行操作。

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

但我收到以下异常:

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 转换为字符串表示,如:
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 对象而不会出现上述异常。什么是最好的方法?

提前致谢。

我得到了上述解决方案。现在我正在使用 jackson-mapper api 将 POJO 转换为 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

粘贴设置:

ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(JacksonJsonProvider.class);
Client clientWithJacksonSerializer = Client.create(cc);
JacksonJsonProviderjackson-jaxrs-json-provider包裹。

关于json - 未找到 Java 类型类 net.sf.json.JSONObject 和 MIME 媒体类型 application/json 的消息正文编写器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12048804/

相关文章:

java - Spring NumberFormatException : Failed to convert value of type 'java.lang.String' to required type 'java. lang.Long

http - 关于 RESTful PUT 原子性的意见

java - 有没有办法在Java中 "clean"接受 header ?

eclipse - tomcat 上的 java web 服务 (jersey) 仅在从 eclipse 运行时才有效

ajax - AJAX 请求后 'keyup' 上的 jQuery 不再工作

javascript - JSON 文件仅显示最后一个条目,尽管 id 不同

java - Restful PUT 方法的 ModelAttribute 未填充值 ( JSON )

java - 如何将 Jersey beans 注入(inject) Weld 中的 Quartz 作业中?

php - 如何执行另一个PHP文件?

Java - 如何对java对象进行Gzip压缩