找不到 Java 类型 myPackage.B 类和 MIME 媒体类型 application/octet-stream 的消息正文编写器
我是 RESTful Web 服务的新手,并尝试从独立客户端应用程序更新我的 @OneToMany
关系,但我无法做到这一点。我正在使用 Glassfish 3.1.1 附带的 JAX-RS 的 Jersey 实现。
我有一个类 A
,它与类 B
具有 @OneToMany
关系。
MyRestClient
是我的独立客户端,它正在调用已部署在 Glassfish 3.1.1 上的 RESTful Web 服务。
MyRestClient.java
public class MyRestClient {
public static void main(String[] args) {
Client client = Client.create();
WebResource resource = client.resource("http://localhost:8080/myapp/rest/a/update/123");
B b1 = new B("debris");
ClientResponse response = resource.put(ClientResponse.class, b1);
System.out.println(response.getEntity(A.class).getTitle() + " has " + response.getEntity(A.class).getBList().size() + " Bs.");
}
}
AResource
是一个 EJB 会话 bean,我将其用作 RESTful Web 服务。
AResource.java
@Stateless
@Path("/a")
public class AResource {
@EJB
private AManager aManager;
@PUT
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("/update/{id}")
public Response updateA(B b, @PathParam("id") int id) {
A a = aManager.findAById(id);
a.addB(b);
return Response.status(Status.OK).entity(a).build();
}
}
当我运行客户端时,收到以下错误消息: com.sun.jersey.api.client.ClientHandlerException:未找到 Java 类型 myPackage.B 类和 MIME 媒体类型 application/octet-stream 的消息正文编写器。
以下是我的独立客户端应用程序中的域对象,该应用程序正在调用我用作 RESTful Web 服务的 AResource
EJB 会话 bean。
A.java
@XmlRootElement
public class A implements Serializable{
private List<B> bList = new ArrayList<B>();
public List<B> getBList() {
return bList;
}
//remaining code
}
B.java
public class B implements Serializable {
private String text;
private A a;
@XmlTransient
public A getA() {
return a;
}
public void afterUnmarshal(Unmarshaller u, Object parent) {
this.a = (A) parent;
}
//remaining code
}
有人可以帮助我理解为什么会发生这种情况以及我应该如何解决这个问题吗?
I am new at RESTful webservices and was trying to update my @OneToMany
relationship from a standalone client application, but I am not able to do that. I am using the Jersey implementation of JAX-RS that ships with Glassfish 3.1.1.
I have a class A
that has a @OneToMany
relationship with class B
.
MyRestClient
is my standalone client that is calling my RESTful webservice which has been deployed on Glassfish 3.1.1.
MyRestClient.java
public class MyRestClient {
public static void main(String[] args) {
Client client = Client.create();
WebResource resource = client.resource("http://localhost:8080/myapp/rest/a/update/123");
B b1 = new B("debris");
ClientResponse response = resource.put(ClientResponse.class, b1);
System.out.println(response.getEntity(A.class).getTitle() + " has " + response.getEntity(A.class).getBList().size() + " Bs.");
}
}
AResource
is an EJB session bean which I am using as RESTful webservice.
AResource.java
@Stateless
@Path("/a")
public class AResource {
@EJB
private AManager aManager;
@PUT
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("/update/{id}")
public Response updateA(B b, @PathParam("id") int id) {
A a = aManager.findAById(id);
a.addB(b);
return Response.status(Status.OK).entity(a).build();
}
}
When I run the client I get the following error message:
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class myPackage.B, and MIME media type, application/octet-stream, was not found.
Following are the domain objects in my standalone client application which is making a call to the AResource
EJB session bean which I am using as the RESTful webservice.
A.java
@XmlRootElement
public class A implements Serializable{
private List<B> bList = new ArrayList<B>();
public List<B> getBList() {
return bList;
}
//remaining code
}
B.java
public class B implements Serializable {
private String text;
private A a;
@XmlTransient
public A getA() {
return a;
}
public void afterUnmarshal(Unmarshaller u, Object parent) {
this.a = (A) parent;
}
//remaining code
}
Could someone help me understand why this is happening and how I should solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在您的客户端代码中,您没有指定要发送的数据的内容类型 - 因此 Jersey 无法找到正确的 MessageBodyWritter 来序列化 b1 对象。
修改 main 方法的最后一行,如下所示:
并在服务器端和客户端向 B 类添加 @XmlRootElement 注释。
In your client code you are not specifying the content type of the data you are sending - so Jersey is not able to locate the right MessageBodyWritter to serialize the b1 object.
Modify the last line of your main method as follows:
And add @XmlRootElement annotation to class B on both the server as well as the client sides.
将此依赖项包含在 POM.xml 中并运行 Maven ->更新
Include this dependencies in your POM.xml and run Maven -> Update
您需要从 B.class 指定
@Produces(MediaType.APPLICATION_XML)
的@Provider
将
MessageBodyWriter
的包添加到/WEB_INF/web.xml
中,如下所示:You need to specify the
@Provider
that@Produces(MediaType.APPLICATION_XML)
from B.classAn add the package of your
MessageBodyWriter<B.class>
to your/WEB_INF/web.xml
as:您必须做两件事才能消除此错误。
@xmlElement
映射客户端:
response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); //消费
或
response = resource.accept(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); //产生
You have to do two things to remove this error.
@xmlElement
mapping in the modelThe client side:
response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); //consume
or
response = resource.accept(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); //produce
添加参考:
只要在客户端创建时添加 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true); 就解决了我的问题:
Adding reference to:
As long as adding clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true); on client creation solved the issue for me:
如果您最近升级了 Ant,也可能会发生这种情况。我在一个项目上使用 Ant 1.8.4,并将 Ant 升级到 1.9.4,并在使用 Ant 构建 fat jar 时开始出现此错误。
对我来说,解决方案是将命令行和 Eclipse 降级回 Ant 1.8.4 使用此处详细介绍的过程
This can also happen if you've recently upgraded Ant. I was using Ant 1.8.4 on a project, and upgraded Ant to 1.9.4, and started to get this error when building a fat jar using Ant.
The solution for me was to downgrade back to Ant 1.8.4 for the command line and Eclipse using the process detailed here
我在 get 方法中遇到了同样的问题,我为 @get 方法返回一个“int”奇怪的是,当我将返回类型更改为 String 时,错误消失了。尝试一下,如果有人知道其背后的逻辑,请分享它
i was facing the same problem for a get method i was returning an "int" for the @get method Strangely when i change the return type to String the error was gone.Give it a try and if someone knows the logic behind it kindly share it
这解决了我的问题。
在 POM.xml 中包含以下依赖项并运行 Maven ->更新也解决了我的问题。
This solved my issue.
http://www.markhneedham.com/blog/2012/11/28/jersey-com-sun-jersey-api-client-clienthandlerexception-a-message-body-reader-for-java-class-and-mime-media-type-applicationjson-was-not-found/
Including following dependencies in your POM.xml and run Maven -> Update also fixed my issue.
如果您缺少实体的空公共构造函数(可能是 JSON、XML 等),也会发生这种情况。
This also happens if you're missing an empty public constructor for the Entity (could be for JSON, XML etc)..
确保所有这些库都在您的类路径中:
将“Pojo Mapping”和“Jackson Provider”添加到球衣客户端配置中:
这对我来说解决了!
Make sure that all these libs are in your class path:
Add "Pojo Mapping" and "Jackson Provider" to the jersey client config:
This solve to me!