0

I'm trying to deserialize a JSON value to the following enum:

public enum MyEnum {
    A("1", "a"),
    B("2", "b");

    @JsonValue
    private final String code;

    private final String description;

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    private MyEnum(String code, String description) {
        this.code = code;
        this.description = description;
    }
}

This is the exception thrown:

2019-12-16 00:22:31.641 DEBUG 6680 --- [qtp798874225-20] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'request' on field 'myEnum': rejected value [2]; codes [typeMismatch.request.myEnum,typeMismatch.myEnum,typeMismatch.com.company.MyEnum,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [request.myEnum,myEnum]; arguments []; default message [myEnum]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.company.MyEnum' for property 'myEnum'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [com.company.MyEnum] for value '2'; nested exception is java.lang.ArrayIndexOutOfBoundsException: 2]]

I'm using Spring Boot 2.1.6 which uses Jackson 2.9.9.

Not sure what is wrong here since all the examples I see on the web are pretty similar and work.

manash
  • 6,985
  • 12
  • 65
  • 125

0 Answers0