Skip to content

@JsonCreator factory ignored when deserializing enums #24233

@laurentpellegrino

Description

@laurentpellegrino

Using SpringBoot 2.4.0, Jackson 2.11.3 along with Java 11, I get a deserialization error when using an enum with a @JsonCreator factory in a simple input model.

Here is a minimal project for reproduction:
https://github.com/lpellegr/springboot-enum-jsoncreator

  1. Run ./gradlew bootRun.
  2. Open http://localhost:8888/demo/1?every=d in your browser

My purpose is to use a custom enum value representation for serialization/deserializaton.

The enum in question in the example is named EveryEnum. When I serialize EveryEnum.DAYS, I expect d in output. When I deserialize (i.e. receive d as input in a controller), I expect EveryEnum.DAYS.

The @jsonvalue annotation works properly and is used when serializing the enum value. However, the @JsonCreator seems completely ignored.

The error I get is the following:

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [com.example.demo.EveryEnum] for value 'd'; nested exception is java.lang.IllegalArgumentException: No enum constant com.example.demo.EveryEnum.d

The problem seems not specific to version 2.4.0 of Spring Boot. At least, I get the same problem with version 2.3.6. Also, testing deserialization directly using an ObjectMapper instance works properly.

All this combined along with the error message lets me think this is simply a use case that is not supported by Spring Boot. Is there a specific reason? it is a bug or a feature request?

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions