问题
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