未找到 Java 类型类 net.sf.json.JSONObject 和 MIME 媒体类型 application/json 的消息正文编写器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12048804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A message body writer for Java type, class net.sf.json.JSONObject, and MIME media type, application/json, was not found
提问by sorabh solanki
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.
我的网络服务正在使用 json,所以我需要将 json 放入我的网络服务提供者的调用中。
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 foundat 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)
09:52:01,625 错误 [[mvc-dispatcher]] servlet mvc-dispatcher 的 Servlet.service() 抛出异常 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) at com.sun.jersey.api.client.Client.handle(Client.java:648) at 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(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
But if i convert my json to string representation like :
但是,如果我将我的 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);
then it is working fine.
然后它工作正常。
Please suggest how can i put my JSON Object without getting the above exception. What is the best way?
请建议我如何放置我的 JSON 对象而不会出现上述异常。什么是最好的方法?
Thanks in advance.
提前致谢。
I got a solution for the above. Now I am making use of Hymanson-mapper api for converting the POJO to json.
我得到了上述解决方案。现在我正在使用 Hymanson-mapper api 将 POJO 转换为 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));
回答by Emmanuel Touzery
A better way is to tell jersey-clientthat it may use Hymansonfor serialization:
https://stackoverflow.com/a/2580315/516188
更好的方法是告诉jersey-client它可以Hymanson用于序列化:https:
//stackoverflow.com/a/2580315/516188
Pasting the setup:
粘贴设置:
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(HymansonJsonProvider.class);
Client clientWithHymansonSerializer = Client.create(cc);
HymansonJsonProvideris in the Hymanson-jaxrs-json-providerpackage.
HymansonJsonProvider是在Hymanson-jaxrs-json-provider包中。
回答by Jugal Shah
Just copying the solution from the question above, just in case someone is looking for answer.
只是从上面的问题中复制解决方案,以防万一有人正在寻找答案。
Use Hymanson-mapper api for converting the POJO to json as shown in the code snippet below:
使用 Hymanson-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));
回答by xparticle
adding a .toString() worked for me:
添加 .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());
回答by jaskirat Singh
JSONObject object=new JSONObject();
need to provide @XMLrootElementAnnotation....if you are accessing the web service from remote....i.e XML Mapping..for Model class.
需要提供@XMLrootElementAnnotation....如果您从远程访问 Web 服务....即 XML Mapping..for Model 类。
回答by Suraj Sharma
After lot of reading here is the solution which i arrived at :
经过大量阅读这里是我到达的解决方案:
If you are using JERSEY-CLIENT as specified as below of version 1.9 or higher
如果您使用 JERSEY-CLIENT 如下指定的 1.9 或更高版本
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9.1</version>
</dependency>
Then use the MOXY Jar as specified below of version 2.19 or higher
然后使用下面指定的 2.19 或更高版本的 MOXY Jar
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.19</version>
</dependency>
And then add the MOXyJsonProvider.class in the Class of Client configuration as shown below :
然后在 Class of Client 配置中添加 MOXyJsonProvider.class 如下所示:
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MOXyJsonProvider.class);
Client client = Client.create(cc);
Now make the required configuration to Client Object to connect to server and execute the request:
现在对客户端对象进行所需的配置以连接到服务器并执行请求:
WebResource webResource = client.resource(*restFulPath*);
Builder builder = webResource.accept(MediaType.APPLICATION_JSON);
builder.header("Content-Type", "application/json");
builder.header("Accept ", "application/json");
builder.accept(MediaType.APPLICATION_JSON);
try
{
String requestData = "*requestInJson*";
ClientResponse response = null;
response = builder.post(ClientResponse.class, requestData);
if (response.getStatus() == 200)
{
MappedJsonReportOut res = response.getEntity(MappedJsonReportOut.class);
}
}
回答by abhishek ringsia
Error Which m getting is little Bit Different But Finally Solution is Work for Me
我得到的错误有点不同,但最终解决方案对我有用
Caused by: com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.siemens.spring.model.User, and MIME media type, application/json, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
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.access0(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563)
at com.siemens.controller.UserRegistration.process(UserRegistration.java:156)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
And This is Solution Worked for Me :
这是对我有用的解决方案:
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/NewsTickerServices/Siemens/CreateJUser");
// value added
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class,mapper.writeValueAsString(user));//user is object of User Pojo class
回答by haggisandchips
You can also use the entity() methods to achieve this, as follows:
您还可以使用 entity() 方法来实现这一点,如下所示:
ClientResponse response = resource.path("rest").path("vtn").path("addEmplyee")
.entity(employee, MediaType.APPLICATION_JSON_TYPE)
.post(ClientResponse.class);
or
或者
ClientResponse response = resource.path("rest").path("vtn").path("addEmplyee")
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(employee)
.post(ClientResponse.class);

