есть код:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
@XmlRootElement
public class TodoList {
@XmlJavaTypeAdapter(MapAdapter.class)
public final Map items = new HashMap();
}
public class MapAdapter extends XmlAdapter<MapPair[],Map<String,Object>> {
public Map<String,Object> unmarshal(MapPair[] pairs) {
Map map = new HashMap();
for (MapPair pair : pairs) {
map.put(pair.key, pair.value);
}
return map;
}
public MapPair[] marshal(Map<String,Object> map) {
MapPair[] pairs = new MapPair[map.size()];
int i = 0;
for(Map.Entry<String,Object> c: map.entrySet()){
pairs[i++] = new MapPair(c.getKey(), c.getValue());
}
return pairs;
}
}
public class MapPair {
@XmlAttribute
public String key;
public Object value;
public MapPair() {} // Required for marshalling to work
public MapPair(String key, Object value) {
this.key = key;
this.value = value;
}
}
public class Item {
@XmlAttribute
public String title;
public Kind type;
@XmlAttribute
public String description;
}
public enum Kind {
PERSONAL, WORK
}
public class Main{
static public void main(String[] arg){
try{
JAXBContext context = JAXBContext.newInstance(TodoList.class);
TodoList todo = new TodoList();
Item item = new Item();
item.title = "priva";
item.type = Kind.PERSONAL;
todo.items.put("first",item);
context.createMarshaller().marshal(todo,System.out);
}catch(Exception ex){
ex.printStackTrace(System.out);
}
}
}
В результате выдаёт ошибку:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: Item nor any of its super class is known to this context]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at Main.main(Main.java:13)
Caused by: javax.xml.bind.JAXBException: Item nor any of its super class is known to this context
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unk
nown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ArrayBeanInfoImpl.serializeBody(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unk
nown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)
... 4 more
Caused by: javax.xml.bind.JAXBException: Item nor any of its super class is known to this context
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(Unknown Source)
... 15 more
Может кто знает как лечить?
MapAdapter и MapPair хотелось бы сделать универсальными.