JAX-RS / Jackson - 使用未知的根元素名称反序列化JSON?

时间:2015-12-16 16:03:31

标签: json jaxb jackson jax-rs resteasy

我正在编写一个RESTeasy代理客户端来使用Apple's 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 个答案:

答案 0 :(得分:4)

我在动态忽略root方面找不到多少,至少没有任何适合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进行的测试

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,您可以查看this post