Deserialize enum ignoring case in Spring Boot controller

Justas :

I have Spring Boot endpoint which has enum as query param:

@GetMapping("/example")
public List<Example> getByEnum(@RequestParam(name = "exampleEnum", required = false) ExampleEnum exampleEnum) {
    // code
}

And enum class:

public enum ExampleEnum {
    FIRST,
    SECOND,
}

If I pass uppercase enum value to the endpoit, it deserializes well but it throws error for lowercase:

java.lang.IllegalArgumentException: No enum constant 

How to deserialize enum ignoring case in Spring Boot Rest endpoint?

This question is not duplicate because it's related to query param deserialization.

Karol Dowbecki :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=459345&siteId=1