如何在不具有_class属性的情况下将Spring数据与Couchbase一起使用 [英] How to use spring data with couchbase without _class attribute
问题描述
是否有一种简单的方法可以将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:
- 您将需要遵循
DefaultCouchbaseTypeMapper
的模型来实现自己的CouchbaseTypeMapper
.在super(...)
构造函数中,您需要提供一个附加参数:TypeInformationMapper
的列表.默认实现没有明确提供一个,因此使用了SimpleTypeInformationMapper
,它是放置FQN的那个. - 还有一个可配置的替代实现,因此您可以通过
Map
:ConfigurableTypeInformationMapper
... 将特定的类别名为较短的名称.
- 因此,通过在列表中放置一个带有所需特定类别名的
ConfigurableTypeInformationMapper
+一个SimpleTypeInformationMapper
(在这种情况下,您要序列化一个没有为其提供别名的类),可以实现您的目标. -
typeMapper
用于MappingCouchbaseConverter
中,不幸的是,您还需要扩展它(只是实例化typeMapper
而不是默认值.) - 具有此功能后,再次重写配置以返回使用自定义
CouchbaseTypeMapper
(mappingCouchbaseConverter()
方法)的自定义MappingCouchbaseConverter
的实例.
- You'll need to implement your own
CouchbaseTypeMapper
, following the model ofDefaultCouchbaseTypeMapper
. In thesuper(...)
constructor, you'll need to provide an additional argument: a list ofTypeInformationMapper
. The default implementation doesn't explicitly provide one, so aSimpleTypeInformationMapper
is used, which is the one that puts FQNs. - There's an alternative implementation that is configurable so you can alias specific classes to a shorter name via a
Map
:ConfigurableTypeInformationMapper
... - So by putting a
ConfigurableTypeInformationMapper
with the alias you want for specific classes + aSimpleTypeInformationMapper
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. - The
typeMapper
is used within theMappingCouchbaseConverter
, which you'll also need to extend unfortunately (just to instantiate yourtypeMapper
instead of the default. - Once you have that, again override the configuration to return an instance of your custom
MappingCouchbaseConverter
that uses your customCouchbaseTypeMapper
(themappingCouchbaseConverter()
method).
这篇关于如何在不具有_class属性的情况下将Spring数据与Couchbase一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!