java 尝试使用 Jersey 将 JSON 转换为 POJO 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6427478/
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
Error when trying to convert JSON to POJO using Jersey
提问by Marouane Gazanayi
I'm doing this :
我正在这样做:
WebResource resource = client.resource(urlStr);
resource.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE);
GenericType<List<EMailInformations>> genericType = new GenericType<List<EMailInformations>>() {};
List<EMailInformations> response = null;
try{
response = resource.get(genericType);
} catch (UniformInterfaceException ue) {
ClientResponse clientResponse = ue.getResponse();
}
Class EMailInformations
类电子邮件信息
@XmlRootElement
public class EMailInformations {
private long id;
public EMailInformations(){
}
public EMailInformations(long id) {
super();
this.id = id;
}
//getters & setters ...
}
Some of the JSON response
一些 JSON 响应
{"cn":[{"id":"302","l":"7","d":1308239209000,"rev":14667,"fileAsStr":"TAICHIMARO, Marouane","_attrs":{"lastName":"TAICHIMARO","imAddress1":"other:......
I got this error :
我收到此错误:
21 juin 2011 16:56:01 com.sun.jersey.api.client.ClientResponse getEntity
GRAVE: A message body reader for Java class java.util.List, and Java type java.util.List<fr.liberacces.pool.liferay.connecteur.modele.EMailInformations>, and MIME media type text/plain was not found
21 juin 2011 16:56:01 com.sun.jersey.api.client.ClientResponse getEntity
GRAVE: The registered message body readers compatible with the MIME media type are:
text/plain ->
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ReaderProvider
*/* ->
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.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
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
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
com.sun.jersey.json.impl.provider.entity.HymansonProviderProxy
But I got this error : GRAVE: A message body reader for Java class java.util.List, and Java type ... was not found
但是我收到了这个错误:GRAVE: A message body reader for Java class java.util.List, and Java type ... was not found
This is firebug trace :
这是萤火虫跟踪:
Réponsevtheitroad le code source
Date Wed, 22 Jun 2011 10:36:19 GMT
Content-Encoding gzip
Content-Length 634
Via 1.1 zimbra.server.com
Keep-Alive timeout=15, max=100
Connection Keep-Alive
Content-Type text/plain
Requêtevtheitroad le code source
Host zimbra.server.com
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Cookie ZM_AUTH_TOKEN=0_01e07d9cb12b86ef4675604362137c08c1d9fd0d_69643d33363a61343831353331382d336436362d343766632d386662393d3133613638633661323165393b6578703d31333a313330383832313935333436393b747970653d363a7a696d6172613b; JSESSIONID=1eb0ksxao39jj
回答by Djarshi
I ran into the same problem. I could get somewhat further by using the tips from 'yves amsellem'. For me I could fix this using the POJO mapper feature. (They need to be enabled on server AND client side. Also the library jersey-json is necessary to get this to work)
我遇到了同样的问题。通过使用 'yves amsellem' 中的提示,我可以走得更远。对我来说,我可以使用 POJO 映射器功能来解决这个问题。(它们需要在服务器和客户端启用。此外,库 jersey-json 是使其工作所必需的)
ClientConfig config = new DefaultClientConfig();
..
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
..
Client client = Client.create(config);
I hope this helps you further along, even though the question is old.
我希望这可以帮助你进一步前进,即使这个问题很老。
回答by yves amsellem
By default, the server seems to produces a content-type text/plain
. Careful, you're negotiating a JSON content-type but you don't pass it to the call:
默认情况下,服务器似乎生成一个 content-type text/plain
。小心,您正在协商 JSON 内容类型,但没有将其传递给调用:
WebResource resource = client.resource(url);
Builder builder = resource.accept(MediaType.APPLICATION_JSON);
GenericType<List<EMailInformations>> genericType =
new GenericType<List<EMailInformations>>() {};
List<EMailInformations> response = builder.get(genericType);
First, you define the path, then Jersey gives you a builder to add content-type negotiation, headers, query parameters, etc. If you call the resource directly, you lose those parameters.
首先,您定义路径,然后 Jersey 为您提供一个构建器来添加内容类型协商、标头、查询参数等。如果您直接调用资源,则会丢失这些参数。
回答by Song X. Gao
I ran into similar error and it turned out that my client does not have http connection to my server.
我遇到了类似的错误,结果我的客户端没有到我的服务器的 http 连接。
So use Linux curl command, or a browser, to read the response body, this may give you more insight about the so generic error.
因此,请使用 Linux curl 命令或浏览器来读取响应正文,这可能会让您更深入地了解此类通用错误。