JAX-RS/Jackson -- 如何反序列化具有未知根元素名称的JSON?

4

我正在编写一个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中?由于它直接对应于搜索结果返回的结果,因此我无法在开发/编译时知道根元素名称。有什么方法可以避免这种异常吗?感谢您能提供的任何帮助!
1个回答

6

我在动态忽略根节点方面没有找到太多相关的内容,至少在JAX-RS环境中没有找到合适的内容。我唯一能想到的是编写一个自定义反序列化器,并跳过根节点,类似于:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;

public abstract class IHateRootElemsJsonDeserializer<T> extends JsonDeserializer<T> {

    private final ObjectMapper mapper = new ObjectMapper();
    private final Class<T> cls;

    public IHateRootElemsJsonDeserializer(Class<T> cls) {
        this.cls = cls;
    }

    @Override
    public T deserialize(JsonParser jp, DeserializationContext dc) 
            throws IOException, JsonProcessingException {
       JsonNode rootNode = jp.getCodec().readTree(jp);
       Map.Entry<String,JsonNode> field = rootNode.fields().next();
       JsonNode node = field.getValue();
       T result = mapper.convertValue(node, cls);
       return result;
    }  
}

然后只需使用具体类型扩展它。

public class GenreDeserializer extends IHateRootElemsJsonDeserializer<Genre>{

    public GenreDeserializer() {
        super(Genre.class);
    }
}

以下是您提供的JSON的详细测试:

以下为您提供的JSON的详细测试:

public class Test {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        GenreDeserializer deserializer = new GenreDeserializer();
        SimpleModule module = new SimpleModule();
        module.addDeserializer(Genre.class, deserializer);
        mapper.registerModule(module);

        Genre genre = mapper.readValue(JSON_FILE, Genre.class);
        System.out.println(genre);

        genre = mapper.readValue(JSON_FILE, Genre.class);
        System.out.println(genre);
    }

    static final File JSON_FILE = new File("json.json");
}

模型
public class Genre {

    public String id;
    public String name;
    public String url;
    public RssUrls rssUrls;
    public ChartUrls chartUrls;

    @Override
    public String toString() {
        return "Category{" + "id=" + id + ", name=" 
                + name + ", url=" + url + ", rssUrls=" + rssUrls + '}';
    }

    public static class RssUrls {
        public String topVideoPodcastEpisodes;
        public String topAudioPodcasts;
        public String topVideoPodcasts;
        public String topPodcasts;
        public String topAudioPodcastEpisodes;
        public String topPodcastEpisodes;

        @Override
        public String toString() {
            return "RssUrls{" + "topVideoPodcastEpisodes=" + topVideoPodcastEpisodes 
                    + ", topAudioPodcasts=" + topAudioPodcasts 
                    + ", topVideoPodcasts=" + topVideoPodcasts 
                    + ", topPodcasts=" + topPodcasts 
                    + ", topAudioPodcastEpisodes=" + topAudioPodcastEpisodes 
                    + ", topPodcastEpisodes=" + topPodcastEpisodes + '}';
        }

    }

    public static class ChartUrls {
        public String videoPodcastEpisodes;
        public String podcasts;
        public String audioPodcastEpisodes;
        public String audioPodcasts;
        public String podcastEpisodes;
        public String videoPodcasts;

        @Override
        public String toString() {
            return "ChatUrls{" + "videoPodcastEpisodes=" + videoPodcastEpisodes 
                    + ", podcasts=" + podcasts 
                    + ", audioPodcastEpisodes=" + audioPodcastEpisodes 
                    + ", audioPodcasts=" + audioPodcasts 
                    + ", podcastEpisodes=" + podcastEpisodes
                    + ", videoPodcasts=" + videoPodcasts + '}';
        }   
    }
}

要在JAX-RS中配置ObjectMapper,你可以参考这篇文章


谢谢!我一定会看看这个并尝试一下! - Shadowman
运行得非常顺利!谢谢! - Shadowman
上帝保佑你。我已经为未知的根元素名称苦苦挣扎了6个小时。 - kryzystof

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接