我正在使用类似于此的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 
或者让以下snipet工作,但目前还没有:
    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: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而不是 - 我真的不知道.
我的游戏有一个 REST 服务,并决定尝试一下 MongoDB - 除了“_id”字段要求之外,看起来非常简单。
这里的问题是我的客户端应用程序正在使用 REST 服务,并且不了解“mongodb”、其驱动程序或依赖项 - 也不应该这样做。为了将客户端与服务器端(REST 服务提供商)解耦,我需要解决 MongoDB 似乎需要 BsonObjectId 类型的“_id”字段这一事实。
目前我使用的是轻量级 DAO 层,因此不需要:
using System;
public class Item {
     private BsonObjectId _id;
     private string name;
}
我正在使用 DAO 将其转换为“与 mongodb 无关”的东西:
using System;
public class ItemDAO {
     private string _id;
     private string name;
}
理想情况下,完全摆脱 BsonObjectId 会很好 - 是否有一些可以使用的注释/自定义序列化处理程序,或者我可以使用字符串而不是 BsonObjectId 的某种方式?
如果做不到这一点,有什么方法可以获取 MongoDB 包装的对象,以便它们用 _id 装饰,我可以将其作为字符串注入回行中?
理想的结果是根本没有 DAO 类,只有“Item”,并让 Item 使用字符串作为 _id(或者不需要 mongodb 依赖项渗透到客户端实现中的东西)。