I am trying to marshal/unmarshal a Map<String, Map<String, Serializable>>
via JAXB. There are two problems:
1. JAXB cannot handle complex maps.
2. JAXB cannot handle interfaces (Serializable in this context).
How is one supposed to get that through JAXB?

- 10,269
- 4
- 46
- 64
-
1Ask yourself this: what XML would you expect to see as the output? XML isn't very good at representing maps... – skaffman Aug 09 '10 at 07:32
-
@skaffman: XML can represent maps quite well as long as the keys and values are representable in XML: – richj Aug 09 '10 at 09:29
-
@richj: I'd argue that's poor XML modelling. Too much noise, not enough signal. – skaffman Aug 09 '10 at 09:30
-
My point is that a map can be represented as a set of key-value pairs, and there's nothing inherently difficult about representing such a set in XML provided that both keys and values are representable in XML. So, to answer the question you posed in your first comment, that is the basic form of XML I would expect to see as the output. It might be optimized into something more compact, or not. – richj Aug 09 '10 at 10:28
2 Answers
I think the main problem is the interface as JAXB ought to be able to marshal Map<String, ConcreteType>. The problem with interfaces is that JAXB demarshalling does not know what concrete type to use to implement the interface. The marshalling stream may not have come from Java code, so the stream can't contain the concrete type information. JAXB would have to choose an implementation, and it needs help to do that.
JAXB: How should I marshall complex nested data structures
This is a common problem with Web Services marshalling. One robust method is to use Data Transfer Objects containing concrete types that can be precisely defined in WSDL for the data transfer. You have to map your domain objects into and out of these DTOs in your application code, which is a disadvantage. One benefit of this approach is that your application is loosely coupled to the data transfer format.
As I know, XML doesn't support Map type. So you can try using like this
<item key="somekey" value="hello" />
but you need to check duplicate key yourself.

- 7,649
- 5
- 35
- 52