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

sor*_*nki 9 rest json jersey

我正在使用泽西客户端来打电话给休息网络服务.

我的web服务正在使用json,所以我需要让json调用我的webservice提供者.

我是以下面的方式做的.

    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);
Run Code Online (Sandbox Code Playgroud)

但我得到以下例外:

09:52:01,625 ERROR [[mvc-dispatcher]] servlet mvc-dispatcher的Servlet.service()抛出异常 com.sun.jersey.api.client.ClientHandlerException:Java类型的消息体编写器,类net.sf. 在com.sun.jersey.client.url连接.URLConnectionClientHandler的com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)中找不到json.JSONObject和MIME媒体类型application/json. _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的com.nec.jp.pflow.unc.service.EmployeeService.addEmployee(EmployeeService.java:44). controller.EmployeeController.addCus tomer(EmployeeController.java:29)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)的sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)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);
Run Code Online (Sandbox Code Playgroud)

然后它工作正常.

请建议如何在不获得上述异常的情况下放置我的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)); 
Run Code Online (Sandbox Code Playgroud)

Emm*_*ery 12

更好的方法是告诉jersey-client它可以jackson用于序列化:https: //stackoverflow.com/a/2580315/516188

粘贴设置:

ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(JacksonJsonProvider.class);
Client clientWithJacksonSerializer = Client.create(cc);
Run Code Online (Sandbox Code Playgroud)

JacksonJsonProviderjackson-jaxrs-json-provider包中.


Jug*_*hah 7

只是复制上面问题的解决方案,以防万一有人在寻找答案.

使用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)); 
Run Code Online (Sandbox Code Playgroud)


小智 5

添加.toString()为我工作:

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());
Run Code Online (Sandbox Code Playgroud)