【发布时间】:2011-03-09 08:26:41
【问题描述】:
我正在使用一个看起来像这样的 XML 有效负载(有关更全面的示例,请查看:http://api.shopify.com/product.html)。
<products type="array">
<product>
...
</product>
<product>
...
</product>
</products>
现在我的代码确实可以工作,但它做的事情似乎真的很“错误”——即将“产品”与 List.class 相关联。所以相关代码如下:
xstream.alias( "products", List.class );
xstream.alias( "product", ShopifyProduct.class );
这很好,除非当我使用该 xstream 实例将任何对象外部化时,它当然总是使用“产品”,这不是我想要的。
我希望能够将通用集合映射到标签:
xstream.alias( "products", ( List<ShopifyProduct> ).class ); // way too easy
或者让下面的代码片段起作用,目前还不行:
ClassAliasingMapper mapper = new ClassAliasingMapper( xstream.getMapper( ) );
mapper.addClassAlias( "product", ShopifyProduct.class );
xstream.registerLocalConverter( ShopifyProductResponse.class, "products", new CollectionConverter( mapper ) );
我创建了 ShopifyProductResponse 类来尝试包装 ShopifyProduct,但它没有任何告诉我:
com.thoughtworks.xstream.mapper.CannotResolveClassException: products : products 在 com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:68) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
如果我添加:
xstream.alias( "products", List.class );
然后它消失了......所以在我看来,mapperwrapper 没有在这里占据一席之地 - 可能是因为它正在寻找 ShopifyProductResponse 对象并找到一个 List 代替 - 我真的不知道。
【问题讨论】: