Skip to content

Enhance Converter #2486

Closed
Closed
@HessTina-YuI

Description

@HessTina-YuI

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

  1. Create interface add constructor to pass PersistentProperty or target class type, like org.springframework.data.elasticsearch.core.convert.AbstractPropertyValueConverter.
  2. Then add judgment condition initialization object in
    org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentProperty#initPropertyValueConverterFromAnnotation

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions