HashMap 的 XMLAdapter
我想转换我的有效负载内的项目列表并将它们转换为哈希图。基本上,我拥有的是一个 Item xml 表示形式,其中包含 ItemID 列表。每个 ItemID 中都有一个 idType。但是,在我的 Item 类中,我希望将这些 ItemID 表示为 Map。
HashMap<ItemIDType, ItemID>
传入的有效负载会将其表示为列表
<Item>...
<ItemIDs>
<ItemID type="external" id="XYZ"/>
<ItemID type="internal" id="20011"/>
</ItemIDs>
</Item>
,但我需要一个适配器将其转换为 HashMap
"external" => "xyz"
"internal" => "20011"
我现在使用 LinkedList,
public class MapHashMapListAdapter extends XmlAdapter<LinkedList<ItemID>, Map<ItemIDType, ItemID>> {
public LinkedList<ItemID> marshal(final Map<ItemIDType, ItemID> v) throws Exception { ... }
public Map<ItemIDType, ItemID> unmarshal(final LinkedList<ItemID> v) throws Exception { ... }
}
但由于某种原因,当我的有效负载转换时,它无法将列表转换为哈希图。 unmarshal 方法传入的 LinkedList 是一个空列表。你们知道我在这里做错了什么吗?我需要在这里创建自己的数据类型来处理 LinkedList 吗?
I want to convert a list of items inside of my payaload and convert them into a hashmap. Basically, what I have is an Item xml representation which have a list of ItemID. Each ItemID has an idType in it. However, inside my Item class, i want these ItemIDs to be represented as a Map.
HashMap<ItemIDType, ItemID>
The incoming payload will represent this as a list
<Item>...
<ItemIDs>
<ItemID type="external" id="XYZ"/>
<ItemID type="internal" id="20011"/>
</ItemIDs>
</Item>
but I want an adapter that will convert this into a HashMap
"external" => "xyz"
"internal" => "20011"
I am right now using a LinkedList
public class MapHashMapListAdapter extends XmlAdapter<LinkedList<ItemID>, Map<ItemIDType, ItemID>> {
public LinkedList<ItemID> marshal(final Map<ItemIDType, ItemID> v) throws Exception { ... }
public Map<ItemIDType, ItemID> unmarshal(final LinkedList<ItemID> v) throws Exception { ... }
}
but for some reason when my payload gets converted, it fails to convert the list into a hashmap. The incoming LinkedList of the method unmarshal is an empty list. Do you guys have any idea what I am doing wrong here? Do I need to create my own data type here to handle the LinkedList?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将
Map
转换为LinkedList
,而不是将其转换为域对象。了解更多信息
Instead of converting the
Map
to aLinkedList
you need to convert it to a domain object.For More Information
你可以参考oracle官方文档:
XmlAdapter。他们为 HashMap 给出了类似的例子。
You may refer to official oracle documentation over here :
XmlAdapter. They have given a similar example for HashMap.
您可以使用我旧的通用解决方案,但永远不要在实际项目中这样做,它不是跨语言的(它仅适用于 java 客户端 + java 服务器解决方案)。 SerializedAdapler.java 可以编组和解组任何 java 对象。为了封送,它将 java 对象序列化为 String,然后将其编码为 base64。为了解组,它解码 Base64 字符串并反序列化它以获取 java 对象。顺便说一句,在此解决方案中,您在服务器端和客户端都需要帮助程序(SerializedAdapler.java、Base64Coder.java)。在正常的解决方案中,您只需要一个接口,例如客户端的 SomeService.java(以及服务器端的 SomeService.java 和 SomeServiceImpl.java)。我用 http://cxf.apache.org/docs 测试了它/a-simple-jax-ws-service.html ,但您可以使用其他Web服务框架。
SomeService.java
SerializedAdapler.java
Base64Coder.java
SomeServiceImpl.java
P.S.:此解决方案示例适用于 Java 7(没有正常的 base64 支持)。请不要为更高版本的 Java 进行编辑。
You can use my old universal solution, but never do this in real project, it is not cross-language (it's only for java client + java server solution). SerializableAdapler.java can marshal and unmarshal any java Object. For marshaling it serializes java object to String, then encodes it to base64. For unmarshaling it decodes base64 string and deserialize it for getting java object. Btw, in this solution you need helpers (SerializableAdapler.java, Base64Coder.java) on both server and client sides. In normal solution you need only one interface like SomeService.java on client side (and both SomeService.java and SomeServiceImpl.java on server side). I tested it with http://cxf.apache.org/docs/a-simple-jax-ws-service.html , but you can use other webservices framework.
SomeService.java
SerializableAdapler.java
Base64Coder.java
SomeServiceImpl.java
P.S.: This solution example is for Java 7 (without normal base64 support). Please do not edit for later Java versions.