我正在编写一个RESTeasy代理客户端,用于消费苹果公司的API,以检索其iTunes类别列表。当您查询有关给定类别的信息时,例如使用此URL:
https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/genres?id=1420
当你获取到一个JSON响应时,它看起来像这样:
{
"1420":{
"name":"Self-Help",
"id":"1420",
"url":"https://itunes.apple.com/us/genre/podcasts-health-self-help/id1420?mt=2",
"rssUrls":{
"topVideoPodcastEpisodes":"https://itunes.apple.com/us/rss/topvideopodcastepisodes/genre=1420/json",
"topAudioPodcasts":"https://itunes.apple.com/us/rss/topaudiopodcasts/genre=1420/json",
"topVideoPodcasts":"https://itunes.apple.com/us/rss/topvideopodcasts/genre=1420/json",
"topPodcasts":"https://itunes.apple.com/us/rss/toppodcasts/genre=1420/json",
"topAudioPodcastEpisodes":"https://itunes.apple.com/us/rss/topaudiopodcastepisodes/genre=1420/json",
"topPodcastEpisodes":"https://itunes.apple.com/us/rss/toppodcastepisodes/genre=1420/json"
},
"chartUrls":{
"videoPodcastEpisodes":"https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?cc=us&g=1420&name=VideoPodcastEpisodes",
"podcasts":"https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?cc=us&g=1420&name=Podcasts",
"audioPodcastEpisodes":"https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?cc=us&g=1420&name=AudioPodcastEpisodes",
"audioPodcasts":"https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?cc=us&g=1420&name=AudioPodcasts",
"podcastEpisodes":"https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?cc=us&g=1420&name=PodcastEpisodes",
"videoPodcasts":"https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?cc=us&g=1420&name=VideoPodcasts"
}
}
}
我正在尝试使用JAXB和Jackson将这个JSON响应映射到Java对象。然而,“1420”根元素名称似乎会导致问题,因为在调用我的客户端时我得到以下异常:
Unrecognized field "1420" (class foo.bar.ITunesCategoryList), not marked as ignorable
我的JAXB类如下所示:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ITunesCategory implements TransferObject {
private static final long serialVersionUID = 3443545925023804457L;
@XmlElement(name = "id")
@JsonProperty("id")
private String identifier = null;
@XmlElement
private String name = null;
@XmlElementWrapper(name = "subgenres")
private List<ITunesCategory> subcategories = new ArrayList<ITunesCategory>();
...
}
我甚至尝试创建了一个包装类,因为搜索可能会返回多个类别。它看起来像这样:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ITunesCategoryList implements TransferObject {
private static final long serialVersionUID = 3303125979016445238L;
@XmlElement
private List<ITunesCategory> categories = new ArrayList<ITunesCategory>();
...
}
无论我指定哪个类作为返回类型,由于类别标识符是JSON对象的根元素名称,所以我都会收到相同的错误。
有没有办法告诉JAXB/Jackson/JAX-RS/RESTeasy忽略根元素名称,只将基础对象映射到Java中?由于它直接对应于搜索结果返回的结果,因此我无法在开发/编译时知道根元素名称。有什么方法可以避免这种异常吗?感谢您能提供的任何帮助!