_class field and spring data couchbase

巧了我就是萌 提交于 2019-12-23 22:01:06

问题


So, I was trying to find a way to remove/rename( and change the fields value ) the _class field from the document generated by spring data couchbase as the document is going to be stored by one service and in all likeliness be consumed by someone totally different.

I was playing around with the api for spring couchbase and through a bit of trial and error found that I can rename the _class field with a custom value using the following way ->

1) Override the typeKey method in a class inheriting AbstractCouchbaseConfiguration . For example, let's say we overrided typeKey to do the following ->

@Override public String typeKey() { return "type"; }

2) In the POJO that stores the data into couchbase, add a field with the same field name as what you gave into the return value of the typeKey method and give it the custom value as needed -

private final String type = "studentDoc";

I wanted to check if this is a valid way of going about this or/and some better way is available to do something like this now


回答1:


That is the only way to do it with spring data at this moment, we would like to add a few extra ways to do that but we are limited to the Spring Data interface contracts. That is why most of the extra configs are done via AbstractCouchbaseConfiguration.




回答2:


Spring data library needs a field with the fully qualified class name as it's value to understand which class object to deserialize the data from couchbase into. By default, this field would be named _class, but can be modified by overriding the typeKey() method in your Couchbase configuration (extending AbstractCouchbaseConfiguration) as mentioned by you.

@Override
public String typeKey() {
    return "customType";
}

But as far as I know, you shouldn't modify the value of the field since the library would not be able to understand which object to deserialize the data into.



来源:https://stackoverflow.com/questions/55098306/class-field-and-spring-data-couchbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!