Deserialize enum ignoring case in Spring Boot controller
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Arrays;
import java.util.Optional;
public enum ExampleEnum {
FIRST,
SECOND;
@JsonCreator
public static ExampleEnum setValue(String key) {
return Arrays.stream(ExampleEnum.values())
.filter(exampleEnum -> exampleEnum.toString().equals(key.toUpperCase()))
.findAny()
.orElse(null);
}
EDIT: The answer below is incorrect. You have to define a custom PropertyEditor
and register it with Spring @InitBinder
which I explained in this post. Thanks to @Dave for pointing this out in the comments.
Spring Boot 2.0 is using Jackson 2.9 which has ACCEPT_CASE_INSENSITIVE_ENUMS feature. You should be able to enable it by setting
spring.jackson.mapper.ACCEPT_CASE_INSENSITIVE_ENUMS = true
property as per docs, Appendix A.