Don Rhummy
Don Rhummy

Reputation: 25870

How allow case-insensitive mapping of enums in jackson/Spring boot?

NOTE: This is not a duplicate. That other question is not about auto-marshalling of Spring request params. It has a solution where you manually marshall objects with jackson.

I want to allow devs to create request objects with enums that can match with case-insensitivity. Other fields/properties may need case-sensitive matching, but the enums should be case-insensitive.

The only way I've found so far (initBinding) requires you to specify the exact enum class at compile time. I am looking for a more generic way to marshall the strings in the JSON request into enums.

The only current way I've found:

@RestController
public class TestController
{
    //...elided...

    @InitBinder
    public void initBinder(final WebDataBinder webdataBinder)
    {
        webdataBinder.registerCustomEditor( MyEnum.class, new CaseInsensitiveEnumConverter() );
    }
}

But this requires compiling with the enums pre-known.

Upvotes: 9

Views: 7939

Answers (2)

Michal Hosala
Michal Hosala

Reputation: 5705

Starting with spring 2.0 it should be enough to set the following in your application.properties:

spring.jackson.mapper.accept-case-insensitive-enums = true 

Upvotes: 4

Tonney Bing
Tonney Bing

Reputation: 230

you can see the class org.springframework.core.convert.support.StringToEnumConverterFactory, so you can customize yourself converterFactory like this.

public class MyStringToEnumConverterFactory implements ConverterFactory<String, Enum> {

@Override
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
    return new StringToEnum(getEnumType(targetType));
}

private class StringToEnum<T extends Enum> implements Converter<String, T> {

    private final Class<T> enumType;

    public StringToEnum(Class<T> enumType) {
        this.enumType = enumType;
    }

    @Override
    public T convert(String source) {
        if (source.isEmpty()) {
            // It's an empty enum identifier: reset the enum value to null.
            return null;
        }

        return (T) Enum.valueOf(this.enumType, source.trim().toUpperCase());
    }
}

private static Class<?> getEnumType(Class targetType) {
    Class<?> enumType = targetType;
    while (enumType != null && !enumType.isEnum()) {
        enumType = enumType.getSuperclass();
    }
    if (enumType == null) {
        throw new IllegalArgumentException(
                "The target type " + targetType.getName() + " does not refer to an enum");
    }
    return enumType;
}
}

and add to ConverterRegistry .

@Configuration
public class MyConfiguration {

@Bean
public ConverterRegistry initConverter(ConverterRegistry registry) {
    registry.addConverterFactory(new MyStringToEnumConverterFactory());
    return registry;
}
}

Hope to help you!

Upvotes: 9

Related Questions