我想使用Jaxb/Jersey將JSON-Response映射到我的Java對象。我有這樣的JSON響應:使用Jaxb/Jersey將動態JSON對象映射到Java POJO
{
"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
}
這是bean我想映射的JSON響應爲:
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」的名稱,因爲它們是在運行時創建的。這裏是我的過濾器豆:
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響應,在這種情況下, 「11223」和「11334」?非常感謝你提前。
問候, Suparno
JSON的XML註釋? – 2013-04-23 15:02:56
也許你可以使用'@ JsonDeserialize'或者用'ObjectMapper'註冊你自己的模塊? – Willy 2013-04-24 02:27:50
@Willy你是什麼意思「自己的模塊與ObjectMapper」? – 2014-06-26 13:27:14