Jersey 异常:严重:Java 类的消息正文阅读器
我有一个基于 Jersey 的 Rest WS,它输出 JSON。我正在实现 Jersey 客户端来调用 WS 并使用 JSON 响应。我的客户端代码如下。
WebResource r = restClient.resource(UriBuilder.fromUri("http://localhost/").port(8080).build()); String resp = r.path("/user").accept(MediaType.APPLICATION_JSON).get(String.class); User[] users = r.path("/user").accept(MediaType.APPLICATION_JSON).get(User[].class);
第二行正确输出 JSON 字符串响应,但是将 JSON 编组到 POJO 的第三行没有发生,我得到以下异常堆栈跟踪,
SEVERE: A message body reader for Java class [Lorg.shoppingsite.model.entity.jpa.User;, and Java type class [Lorg.shoppingsite.model.entity.jpa.User;, and MIME media type application/json was not found Dec 21, 2011 11:32:01 AM com.sun.jersey.api.client.ClientResponse getEntity SEVERE: The registered message body readers compatible with the MIME media type are: */* -> com.sun.jersey.core.impl.provider.entity.FormProvider com.sun.jersey.core.impl.provider.entity.StringProvider com.sun.jersey.core.impl.provider.entity.ByteArrayProvider com.sun.jersey.core.impl.provider.entity.FileProvider com.sun.jersey.core.impl.provider.entity.InputStreamProvider com.sun.jersey.core.impl.provider.entity.DataSourceProvider com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General com.sun.jersey.core.impl.provider.entity.ReaderProvider com.sun.jersey.core.impl.provider.entity.DocumentProvider com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General com.sun.jersey.core.impl.provider.entity.EntityHolderReader
我的请求中有正确的 MIME 类型。我的 POJO 已使用 XMLRootElement 进行注释。我缺少什么。
谢谢
I have a Jersey based Rest WS which outputs JSON. I am implementing a Jersey Client to invoke the WS and consume the JSON response. The client code I have is below
WebResource r = restClient.resource(UriBuilder.fromUri("http://localhost/").port(8080).build()); String resp = r.path("/user").accept(MediaType.APPLICATION_JSON).get(String.class); User[] users = r.path("/user").accept(MediaType.APPLICATION_JSON).get(User[].class);
The 2nd line outputs the JSON string response correctly, however the 3rd line to marshal JSON to the POJO is not happening and I get the following exception stacktrace
SEVERE: A message body reader for Java class [Lorg.shoppingsite.model.entity.jpa.User;, and Java type class [Lorg.shoppingsite.model.entity.jpa.User;, and MIME media type application/json was not found Dec 21, 2011 11:32:01 AM com.sun.jersey.api.client.ClientResponse getEntity SEVERE: The registered message body readers compatible with the MIME media type are: */* -> com.sun.jersey.core.impl.provider.entity.FormProvider com.sun.jersey.core.impl.provider.entity.StringProvider com.sun.jersey.core.impl.provider.entity.ByteArrayProvider com.sun.jersey.core.impl.provider.entity.FileProvider com.sun.jersey.core.impl.provider.entity.InputStreamProvider com.sun.jersey.core.impl.provider.entity.DataSourceProvider com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General com.sun.jersey.core.impl.provider.entity.ReaderProvider com.sun.jersey.core.impl.provider.entity.DocumentProvider com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General com.sun.jersey.core.impl.provider.entity.EntityHolderReader
I have the correct MIME TYPES in my request. My POJO has been annotated with XMLRootElement. What am I missing.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
要使其发挥作用,您只需要两件事。检查是否缺少:
如果缺少的话,您需要添加 jersey-json 的依赖项。为了供您参考,我将此依赖项添加到了我的 pom.xml 中。
To make it work you only need two things. Check if you are missing:
You need to add dependency for jersey-json if you are missing.For your reference I added this dependency into my pom.xml.
我能够通过添加 jersey-json 的 Maven 依赖项来解决该问题。
I was able to fix the issue by adding the maven dependency for jersey-json.
只是把这个
just put this
我知道这篇文章很旧,而且您很久以前就发现了这一点,但这只是为了拯救那些将来会读到这篇文章的人。
您可能忘记向传递到端点的实体添加注释,因此 Jersey 不知道如何处理它接收到的 POJO。
用这样的东西注释 pojo:
I know that this post is old and you figured this out a long time ago, but just to save the people who will read this some time.
You probably forgot to add annotation to the entity you are passing to the endpoint, so Jersey does not know how to process the POJO it receives.
Annotate the pojo with something like this:
如果您正在构建 uberjar 或“shaded jar”,请确保您的元 inf 服务文件已合并。 (这在 dropwizard 项目中多次困扰我。)
如果您使用 gradle ShadowJar 插件,则需要在
shadowJar
目标中调用mergeServiceFiles()
:https://github.com/johnrengelman/shadow#merging-service-files不确定 Maven 或其他构建系统的类似命令是什么。
If you are building an uberjar or "shaded jar", make sure your meta inf service files are merged. (This bit me multiple times on a dropwizard project.)
If you are using the gradle shadowJar plugin, you want to call
mergeServiceFiles()
in yourshadowJar
target: https://github.com/johnrengelman/shadow#merging-service-filesNot sure what the analogous commands are for maven or other build systems.
就我而言,我使用的是 POJO。我忘记将 POJOMappingFeature 配置为 true。
Maycon 在早期的回答中已经指出了这一点。然而,有些人可能无法在 web.xml 中正确配置它,这是我的示例。
In my case, I'm using POJO. And I forgot configure POJOMappingFeature as true.
Maycon has pointed it out in an early answer. However some guys might have trouble to configure it in web.xml correctly, here is my example.
我们还决定使用球衣作为解决方案。但由于我们在大多数情况下都使用 org.JSON,因此这种依赖关系是不必要的,我们感觉不好。
因此,我们使用 String 表示形式来获取 org.JSON 对象,而不是
现在这样使用它:
其中 JSONObject 来自 org.JSON 并且导入可以更改为:
We also decided to use jersey as a solution. But as we are using org.JSON in most of the cases this dependency is unecessary and we felt not good.
Therefore we used the String representation to get a org.JSON object, instead of
we use it this way now:
where JSONObject comes from org.JSON and the imports could be changed from:
只需在课程开始前在 POJO 中添加以下行,您的问题就可以解决。
@Produces(“应用程序/json”)
@XmlRootElement
参见示例
导入 javax.ws.rs.Produces;
导入 javax.xml.bind.annotation.XmlRootElement;
Just add below lines in your POJO before start of class ,and your issue is resolved.
@Produces("application/json")
@XmlRootElement
See example
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlRootElement;
尝试添加:
如果您将
HTTP GET
与消息正文一起使用,也可能会出现此问题,因此在这种情况下,添加 jersey-json lib、@XmlRootElement
或修改 web.xml 会成功没有帮助。您应该使用 URLQueryParam
或HTTP POST
。Try adding:
Also this problem may occur if you're using
HTTP GET
with message body so in this case adding jersey-json lib,@XmlRootElement
or modifying web.xml won't help. You should use URLQueryParam
orHTTP POST
.您需要为您的类
Lorg.shoppingsite.model.entity.jpa.User
实现自己的MessageBodyReader
和MessageBodyWriter
。You need to implement your own
MessageBodyReader
andMessageBodyWriter
for your classLorg.shoppingsite.model.entity.jpa.User
.只需检查您是否在 eclipse 中运行不同的实例即可。我退出了所有其他会话,干净的构建解决了问题
Just check if you are running different instances in eclipse. I quit all my other sessions, clean build fixed the problem
有些人可能会困惑为什么添加 jersey-json jar 不能解决这个问题。我发现这个 jar 必须比 jersey-json-1.7.jar 新(1.7.0 不起作用,但 1.7.1 工作正常。)。希望这可以帮助
Some may be confused why add jersey-json jar can't solve this problem. I found out that this jar has to newer than jersey-json-1.7.jar( 1.7.0 doesn't work, but 1.7.1 works fine.). hope this can help
对于 Python 和 Swagger 示例:
具有 MIME 类型的最重要字符串:
r = requests.post(api_url, json=api_data)
for Python and Swagger example:
Most important string with MIME type:
r = requests.post(api_url, json=api_data)
问)代码在 Intelj 中工作正常,但在命令行中失败。
Sol)将 jersey 的依赖项添加为直接依赖项而不是临时依赖项。
推理:因为,它是使用 IntelliJ 工作正常,依赖项配置正确。
通过以下方式之一获取所需的依赖项:
mvn dependency:tree
现在,显式添加那些有问题的球衣依赖项。
Q) Code was working fine in Intellj but failing in command line.
Sol) Add dependencies of jersey as a direct dependency rather than a transient one.
Reasoning: Since, it was working fine with IntelliJ, dependencies are correctly configured.
Get required dependencies by one of the following:
mvn dependency:tree
Now, add those problematic jersey dependencies explicitly.
更新我的 Java 版本(我的例子是 1.8)后我能够修复它。我在 com.sun.jersey.api.client.WebResource.entity(, ).accept().get() 中使用 get 方法。
I was able to fix it after updating my Java version (1.8 in my case). I was using get method in com.sun.jersey.api.client.WebResource.entity(, ).accept().get().
我的也是类似的情况。我在关键时刻遇到这个错误,但我不记得以前是如何解决的,我已经解决过很多次了。经过几个小时的艰苦检查,我已经解决并重现了该错误,并确定了这种情况的解决方案是多么简单。
好的 - 解决方案:删除(或更正)对主项目属性文件所做的任何新更改。
是的,就是这样。事实上,我的项目是一个多模块的大型项目,你知道没有正确的依赖关系是一种罕见的情况,因为经常从 git 获取(RAC 也是如此)。我的项目从属性文件中获取几乎所有可配置的内容,该文件是大约 15 个模块(子项目)所共有的,并且它们之间具有大量的内部依赖关系。 jersey-json 的依赖项始终存在于我合并的父 pom 中。 XML 注释不是问题,因为项目在修改它们后运行了大约 100 次。这里的一些解决方案指向 web.xml 和 POJOMappingFeature 之类的东西。就我而言,我什至没有触及此构建中的 webapp 模块。所以无论如何,这个解决方案对我有用,我花时间记录下来,以防万一我碰巧陷入这个错误,我就不必浪费我的瞌睡了。 (当然,也适合你)
Mine was similar situation. I get this error in crucial times and I did not remember how I had solved it before, which I did many number of times. After strenuous hours of inspection, I have solved and reproduced the error and made sure how simple is the solution for this situation.
Okay - solution : remove (or correct) any newly made changes to your main project's properties files.
Yes, this is it. In fact, my project is a multi-moduled huge one, and you know going without correct dependencies is a rare case scenario as is fetched often from git (RAC too). My project was getting almost all configurable things from a properties file that is common to about 15 or so modules (sub projects) with good amount of intra-dependencies among them. jersey-json's dependency is always there in my merged parent pom. XML Annotations aren't a problem as the project is being run for around 100 times after modifying them. some solutions here point to web.xml and things like POJOMappingFeature. In my case, I haven't even touched the webapp module in this build. so anyhow, this solution worked for me and I am spending time to record this in SO in case if I ever happen to fall into this error, I would not have to waste my sleepy nights. (of course, for you too)