我正在使用MOXy来解组一些推文并将它们存储在数据库表中。 目前我正专注于解析推文。
我用以下内容开始了上下文:
unmarshaller = JAXBContext.newInstance(Tweet.class, User.class).createUnmarshaller();
unmarshaller.setProperty("eclipselink.media-type", "application/json");
unmarshaller.setProperty("eclipselink.json.include-root", false);
编辑3:简化模型
这是我正在使用的简单Json / JPA映射:
@Entity
@XmlRootElement
public class User implements Serializable {
@Id public long id;
public String name;
public String screen_name;
}
和
@Entity
@XmlRootElement
public class Tweet implements Serializable {
public @XmlJavaTypeAdapter(DateAdapter.class) Date created_at;
public @Id long id;
public String text;
public @ManyToOne User user;
}
问题是,当我用以下内容解组推文时
Tweet tweet = unmarshaller.unmarshal(new StreamSource(new StringReader(msg)), Tweet.class).getValue();
tweet.getUser()
始终返回null
。
我无法理解为什么会这样,如果我遗失了什么;我无法找到启发性的答案。
编辑2 :(将EDIT 1替换为工作示例和输出):
根据评论请求,我提供了一个示例推文:
{
"created_at":"Wed Nov 02 14:18:24 +0000 2016",
"id":793819553092558848,
"id_str":"793819553092558848",
"text":"Day of the Dead Google doodle marks Mexico’s holiday honoring life & death *url removed*,
"source":"<a href=\"http://www.hootsuite.com\" rel=\"nofollow\">Hootsuite</a>",
"truncated":false,
"in_reply_to_status_id":null,
"in_reply_to_status_id_str":null,
"in_reply_to_user_id":null,
"in_reply_to_user_id_str":null,
"in_reply_to_screen_name":null,
"user":{
"id":1266274663,
"id_str":"1266274663",
"name":"Nitin Solanki",
"screen_name":"SolankiNit",
"location":"India,Mumbai",
"url":"*url removed*",
"description":"I am passionate & engaged in #SEO #PPC #SocialMedia activity Since 2008. Sharing Trending Content & Useful Resources on #DigitalMarketing & much more. Thanks!!",
"protected":false,
"verified":false,
"followers_count":689,
"friends_count":632,
"listed_count":64,
"favourites_count":304,
"statuses_count":11297,
"created_at":"Thu Mar 14 05:30:59 +0000 2013",
"utc_offset":19800,
"time_zone":"Chennai",
"geo_enabled":true,
"lang":"en",
"contributors_enabled":false,
"is_translator":false,
"profile_background_color":"8B542B",
"profile_background_image_url":"http://abs.twimg.com/images/themes/theme8/bg.gif",
"profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme8/bg.gif",
"profile_background_tile":false,
"profile_link_color":"9D582E",
"profile_sidebar_border_color":"D9B17E",
"profile_sidebar_fill_color":"EADEAA",
"profile_text_color":"333333",
"profile_use_background_image":true,
"profile_image_url":"http://pbs.twimg.com/profile_images/479285935293468672/6bqotzuS_normal.jpeg",
"profile_image_url_https":"https://pbs.twimg.com/profile_images/479285935293468672/6bqotzuS_normal.jpeg",
"profile_banner_url":"https://pbs.twimg.com/profile_banners/1266274663/1415196380",
"default_profile":false,
"default_profile_image":false,
"following":null,
"follow_request_sent":null,
"notifications":null
},
"geo":null,
"coordinates":null,
"place":null,
"contributors":null,
"is_quote_status":false,
"retweet_count":0,
"favorite_count":0,
"entities":{
"hashtags":[
],
"urls":[
{
"url":"*url removed*",
"expanded_url":"*url removed*",
"display_url":"*url removed*",
"indices":[
79,
102
]
}
],
"user_mentions":[
],
"symbols":[
]
},
"favorited":false,
"retweeted":false,
"possibly_sensitive":false,
"filter_level":"low",
"lang":"en",
"timestamp_ms":"1478096304568"
}
这是解析后的结果:
{id_str=793819553092558848, created_at=Wed Nov 02 14:18:24 CET 2016, truncated=false, id=793819553092558848, text=Day of the Dead Google doodle marks Mexico’s holiday honoring life & death *url removed*, retweeted_status=null, user=null, retweeted=false}