0

我想使用 Jaxb/Jersey 从 JSON-Response 映射到我的 Java 对象。我有这样的 Json 响应:

{
"fuzzy": false,
"hits": [
    { ... }
],
"filters": {
    "category": {
        "selected": false,
        "values": { }
        },
        "id": "category",
        "name": "Kategorie"
    },
    "shop": {
        "selected": false,
        "values": {
            "7817": {
                "count": 2,
                "selected": false,
                "numeric_sort_key": null,
                "id": "7817",
                "name": "AZUGA"
            }
        },
        "id": "shop",
        "name": "Shop"
    },
    "11223": {
        "selected": false,
        "values": { },
        "id": "certificate",
        "name": "Vertrauensgarantie"
    },
    "11334": {
        "selected": false,
        "values": {
            "2290": {
                "count": 1,
                "selected": false,
                "numeric_sort_key": null,
                "id": "2290",
                "name": "22.90"
            }
        },
        "id": "price",
        "name": "Preis"
    }
},
"total_hits": 2

}

这是我想将 Json 响应映射到的 bean:

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



@XmlRootElement
public class RestBean {

    @XmlElement
    private String fuzzy;

    @XmlElement
    private List<HitsBean> hits;

    @XmlElement
    private Filters filters;

    @XmlElement
    private String total_hits;
}

问题是,“过滤器”的子元素是动态的,这意味着无法提前知道子元素“11223”和“11334”的名称,因为它们是在运行时创建的。这是我的过滤器 bean:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="filters")
@XmlAccessorType(XmlAccessType.FIELD)
public class Filters {

    @XmlElement
    private Filter category;

    @XmlElement
    private Filter shop;

    @XmlAnyElement
    @XmlMixed
    private List<Filter> filt;
}

但是注释@XmlAnyElement 总是抛出这样的错误:

23.04.2013 16:26:11 com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SCHWERWIEGEND: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. 
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.checkQName(CoreDocumentImpl.java:2582)
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.setName(ElementNSImpl.java:117)
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.<init>(ElementNSImpl.java:80)
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createElementNS(CoreDocumentImpl.java:2095)
at com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM.startElement(SAX2DOM.java:164)
at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.closeStartTag(ToXMLSAXHandler.java:204)
at com.sun.org.apache.xml.internal.serializer.ToSAXHandler.flushPending(ToSAXHandler.java:277)
at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.startElement(ToXMLSAXHandler.java:646)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerHandlerImpl.startElement(TransformerHandlerImpl.java:263)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.DomLoader.startElement(DomLoader.java:107)
...

我的问题是,我应该如何映射动态生成的 Json-Response,在这种情况下是“11223”和“11334”?非常感谢您提前。

问候, 苏帕诺

4

1 回答 1

0

使用杰克逊映射器和@JsonAnyGetter/@JsonAnySetterorg.codehaus.jackson.annotate包)注释非常简单。
类过滤器(在这里使用 getter/setter 和默认 XmlAccessorType 很重要)

public class Filters {

    private Filter category;

    private Filter shop;

    private Map<String, Filter> filt = new HashMap<String, Filter>();  

    @JsonAnyGetter
    public Map<String, Filter> any() {
       return filt;
    }

    @JsonAnySetter
    public void set(String name, Filter value) {
        filt.put(name, value);
    }

    // getters/setters for category and shop  
}  

此外,不要@XmlAccessorType(XmlAccessType.FIELD)在所有类中使用序列化为 JSON。使用 getter/setter 对和默认访问器类型。

于 2013-04-24T21:01:55.337 回答