简体   繁体   English

使用Jettison映射JSON Twitter到Jaxb的帮助

[英]Help for Mapping JSON Twitter answer to Jaxb with Jettison

I'm a bit new to use JSON format manipulation and not very experimented in JAXB either. 我对使用JSON格式操作有点陌生,并且在JAXB中也没有做过非常尝试。 What I try to do is building a small client to launch search on Twitter. 我试图做的是建立一个小型客户端,以便在Twitter上启动搜索。 My starting framework is JBoss RESTEasy (JAX-RS implementation) that provides an elegant way to consume Rest services JSON services by mapping JSON to JAXB thru Jettison framework (it works also in the other way if you want to provide REST service and produce JSON from JAXB). 我的起始框架是JBoss RESTEasy(JAX-RS实现),它通过JSON通过Jettison框架将JSON映射到JAXB提供了一种使用Rest服务JSON服务的优雅方法(如果您要提供REST服务并从中产生JSON,它也可以以其他方式工作JAXB)。

So I launch a simple request to Twitter : 因此,我向Twitter发出了一个简单的请求:

http://search.twitter.com/search.json?q=java

And the answer comes in the following JSON format 答案来自以下JSON格式

{
"results":
[
  {"from_user_id_str":"67875385",
  "profile_image_url":"http://a2.twimg.com/axt_normal.png",
  "created_at":"Sun, 28 Nov 2010 22:38:39 +0000",
  "from_user":"extant",
  "id_str":"9013327095136256",
  "metadata":{"result_type":"recent"},
  "to_user_id":null,
  "text": "New blog post: No fancy swap in java",
  "id":9013327095136256,
  "from_user_id":67875385,
  "geo":null,
  "iso_language_code":"en",
  "to_user_id_str":null,
  "source":"wordpress"
  }, 
  <more tweets...>
],
"max_id":9013327095136256,
"since_id":0,
"refresh_url":"?since_id=9013327095136256&q=java",
"next_page":"?page=2&max_id=9013327095136256&q=java",
"results_per_page":15,
"page":1,"completed_in":0.020154,
"since_id_str":"0",
"max_id_str":"9013327095136256",
"query":"java"
} 

So I created two classes to map this answer 所以我创建了两个类来映射这个答案

@BadgerFish
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SearchResults { 
 @XmlElement
 public List<Tweet> results;
 @XmlElement(name = "max_id")
 public long maxId;
 @XmlElement(name = "since_id")
 public long sinceId;
}

and

@BadgerFish
@XmlRootElement(name="tweet")
public class Tweet {

 @XmlElement(name = "id")
 public long id;

 @XmlElement(name = "text")
 public String text;

 @XmlElement(name = "created_at")
 public Date createdAt;

 @XmlElement(name = "from_user")
 public String fromUser;

 @XmlElement(name = "profile_image_url")
 public String profileImageUrl;

 @XmlElement(name = "to_user_id")
 public Long toUserId;

 @XmlElement(name = "from_user_id")
 public long fromUserId;

 @XmlElement(name = "language_code")
 public String languageCode;

 @XmlElement(name = "source")
 public String source;

}

My Twitter RESTEasy client is a simple interface 我的Twitter RESTEasy客户端是一个简单的界面

public interface TwitterResource {
 @Path("/search.json")
 @Consumes("application/*+json")
 @GET
 SearchResults search(@QueryParam("q")String query); 
}

Which is exploited with the following RESTEasy code : 以下RESTEasy代码可利用该代码:

...
TwitterResource tr = ProxyFactory.create(TwitterResource.class, "http://search.twitter.com");
SearchResults sr = tr.search("java");
...

This code give the following Exception : 此代码给出以下异常:

Exception in thread "main" org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: javax.xml.bind.JAXBException
 - with linked exception:
[org.codehaus.jettison.json.JSONException: JSONObject["results"] is not a JSONObject.]
 at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.readFrom(AbstractJAXBProvider.java:86)

I assume it comes from a wrong JAXB mapping on SearchResults class but can't figure how to correct it (and it's complicated with the JSON translation to XML). 我以为它来自SearchResults类上的错误的JAXB映射,但无法弄清楚如何纠正它(并且将JSON转换为XML很复杂)。 Any clue to correct it would be great. 任何纠正它的线索都很好。

Thanks in advance 提前致谢

Ok, no solution found with Jettison (I'm convinced it's a bug) and JAXB mapping for JSON. 好的,Jettison(我坚信这是一个错误)和JSON的JAXB映射都找不到解决方案。 But as RESTEasy support also the Jackson Framework, I switched to Jackson and everything thing is ok now. 但是由于RESTEasy也支持Jackson框架,所以我切换到Jackson,现在一切正常。

In fact I find more clean to avoid this JSON to JAXB translation my first solution was using. 实际上,我发现更干净的方法是避免将我的第一个解决方案使用的JSON到JAXB的转换。

Thanks myself ;-) 谢谢我自己;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM