泽西岛异常:严重:Java 类的消息正文阅读器

2022-01-21 00:00:00 json rest java jersey

我有一个基于 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 的第三行没有发生,我得到以下异常堆栈跟踪

<上一页>严重:Java 类 [Lorg.shoppingsite.model.entity.jpa.User; 和 Java 类型类 [Lorg.shoppingsite.model.entity.jpa.User; 和 MIME 媒体类型 application/json 的消息正文阅读器不是成立2011 年 12 月 21 日上午 11:32:01 com.sun.jersey.api.client.ClientResponse getEntitySEVERE:与 MIME 媒体类型兼容的注册消息正文阅读器是:*/* ->com.sun.jersey.core.impl.provider.entity.FormProvidercom.sun.jersey.core.impl.provider.entity.StringProvidercom.sun.jersey.core.impl.provider.entity.ByteArrayProvidercom.sun.jersey.core.impl.provider.entity.FileProvidercom.sun.jersey.core.impl.provider.entity.InputStreamProvidercom.sun.jersey.core.impl.provider.entity.DataSourceProvidercom.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Generalcom.sun.jersey.core.impl.provider.entity.ReaderProvidercom.sun.jersey.core.impl.provider.entity.DocumentProvidercom.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReadercom.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReadercom.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReadercom.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$Generalcom.sun.jersey.core.impl.provider.entity.XMLListElementProvider$Generalcom.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$Generalcom.sun.jersey.core.impl.provider.entity.EntityHolderReader

我的请求中有正确​​的 MIME 类型.我的 POJO 已经用 XMLRootElement 进行了注释.我错过了什么.

谢谢

解决方案

我能够通过添加 jersey-json 的 maven 依赖来解决这个问题.

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

解决方案

I was able to fix the issue by adding the maven dependency for jersey-json.

相关文章