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
- Run
./gradlew bootRun
. - 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?
Comment From: snicoll
Thanks for the report and the sample. Spring Framework does not use Jackson
for web bindings (but use it, as you've found out yourself for the body of the request and the response). Having a converter using Jackson could be problematic as you need to know for sure when you can actually call it (vs. a more specific converter).
I should also mention that web binding is not part of Spring Boot but the core Spring Framework.
Comment From: lpellegr
@snicoll Thanks for your comment. Should I understand there is no way to support this use case with Spring? To be honest, I am a bit lost. Do you have a hint or a reference I could look at to support this use case?
Comment From: snicoll
No. Spring Framework does not use Jackson there but if you feel strongly this is the right approach for you, you can configure the ConversionService
to use whatever you want. Please follow-up on StackOverflow if you have more questions, as mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.