Closed
Description
Question
In Spring Data Elasticsearch 4.0, removed jackson as the default json decompile.
So I need to write converter for my custom enum.
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum SexEnum {
MALE("male", "the male"),
FAMALE("famale", "the famale"),
;
@JsonValue
private final String code;
private final String desc;
}
But I must write multiple Converter for my entity, they have the same function. However, I just can't get the class of the enumeration from converter.
@Document(indexName = "user")
@Data
public class User {
private String userName;
@ValueConverter(SexEnumConverter.class)
private SexEnum sex;
@ValueConverter(StatusEnumConverter.class)
private StatusEnum status;
}
Because the read method can only get the String type.
public class SexEnumConverter implements PropertyValueConverter {
@Override
public Object write(Object value) {
return null;
}
@Override
public Object read(Object value) {
return null;
}
}
Solution Ideas
- Create interface add constructor to pass PersistentProperty or target class type, like org.springframework.data.elasticsearch.core.convert.AbstractPropertyValueConverter.
- Then add judgment condition initialization object in
org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentProperty#initPropertyValueConverterFromAnnotation