Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a class that contained a Long value, that actually only had three possible numbers 1,2,3.

public class ASD {
    ...
    private Long downloadType;
}

I changed it to:

public class ASD {
    ...
    private DownloadType downloadType;
}

public enum DownloadType {
    @JsonProperty("1")
    IMAGE,
    @JsonProperty("2")
    TEXT,
    @JsonProperty("3")
    BOTH;
}

When I try to get the entity ASD from the database(Mongo) it comes as integer, and it tries to make a conversion of Enum.valueOf("1"). There's no enum of value 1, and it throws this exception.

java.lang.IllegalArgumentException: No enum constant com.example.domain.enums.DownloadType.1
at java.lang.Enum.valueOf(Enum.java:238) ~[na:1.8.0_251]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:829) ~[spring-data-mongodb-1.10.10.RELEASE.jar:na]

I have tried to use the @Enumerated annotation, make a custom converter that implements AttributeConverter(but it seems that this only works for sql databases).

I have seen solutions where you can convert a number into an Enum. But the conversion will try with EVERY number.

Any idea of how to make a custom converter for this enum?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...