如何在不具有_class属性的情况下将Spring数据与Couchbase一起使用 [英] How to use spring data with couchbase without _class attribute

查看:129
本文介绍了如何在不具有_class属性的情况下将Spring数据与Couchbase一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以将Spring数据基于Couchbase与不具有_class属性的文档一起使用? 在沙发上,我的sampledata存储桶中有类似的内容:

Is there a simple way to use spring data couchbase with documents that do not have _class attribute? In the couchbase I have something like this in my sampledata bucket:

{
  "username" : "alice", 
  "created" : 1473292800000,
  "data" : { "a": 1, "b" : "2"},
  "type" : "mydata"
}

现在,有什么方法可以定义从此文档结构到Java对象的映射(请注意,缺少_class属性并且无法添加),反之亦然,这样我就可以从Spring获得所有(或大多数)自动魔术功能沙发床数据?

Now, is there any way to define mapping from this structure of document to Java object (note that _class attribute is missing and cannot be added) and vice versa so that I get all (or most) automagical features from spring couchbase data?

类似: 如果type字段的值为"mydata",请使用类MyData.java. 因此,执行查找而不是自动将AND _class = "mydata"添加到生成的查询时,请添加AND type = "mydata".

Something like: If type field has value "mydata" use class MyData.java. So when find is performed instead of automatically adding AND _class = "mydata" to generated query add AND type = "mydata".

推荐答案

Spring数据通常需要_class字段来知道反序列化时要实例化的内容.

Spring Data in general needs the _class field to know what to instantiate back when deserializing.

在Spring Data Couchbase中,通过覆盖AbsctractCouchbaseDataConfiguration中的typeKey()方法,使用与_class不同的字段名称非常容易.

It's fairly easy in Spring Data Couchbase to use a different field name than _class, by overriding the typeKey() method in the AbsctractCouchbaseDataConfiguration.

但是在默认情况下,仍然需要一个完全合格的类名

解决这个问题将需要做更多的工作:

Getting around that will require quite a bit more work:

  1. 您将需要遵循DefaultCouchbaseTypeMapper的模型来实现自己的CouchbaseTypeMapper.在super(...)构造函数中,您需要提供一个附加参数: TypeInformationMapper 的列表.默认实现没有明确提供一个,因此使用了SimpleTypeInformationMapper,它是放置FQN的那个.
  2. 还有一个可配置的替代实现,因此您可以通过Map:ConfigurableTypeInformationMapper ...
  3. 将特定的类别名为较短的名称.
  4. 因此,通过在列表中放置一个带有所需特定类别名的ConfigurableTypeInformationMapper +一个SimpleTypeInformationMapper(在这种情况下,您要序列化一个没有为其提供别名的类),可以实现您的目标.
  5. typeMapper用于MappingCouchbaseConverter中,不幸的是,您还需要扩展它(只是实例化typeMapper而不是默认值.)
  6. 具有此功能后,再次重写配置以返回使用自定义CouchbaseTypeMapper(mappingCouchbaseConverter()方法)的自定义MappingCouchbaseConverter的实例.
  1. You'll need to implement your own CouchbaseTypeMapper, following the model of DefaultCouchbaseTypeMapper. In the super(...) constructor, you'll need to provide an additional argument: a list of TypeInformationMapper. The default implementation doesn't explicitly provide one, so a SimpleTypeInformationMapper is used, which is the one that puts FQNs.
  2. There's an alternative implementation that is configurable so you can alias specific classes to a shorter name via a Map: ConfigurableTypeInformationMapper...
  3. So by putting a ConfigurableTypeInformationMapper with the alias you want for specific classes + a SimpleTypeInformationMapper after it in the list (for the case were you serialize a class that you didn't provide an alias for), you can achieve your goal.
  4. The typeMapper is used within the MappingCouchbaseConverter, which you'll also need to extend unfortunately (just to instantiate your typeMapper instead of the default.
  5. Once you have that, again override the configuration to return an instance of your custom MappingCouchbaseConverter that uses your custom CouchbaseTypeMapper (the mappingCouchbaseConverter() method).

这篇关于如何在不具有_class属性的情况下将Spring数据与Couchbase一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆