How allow case-insensitive mapping of enums in jackson/Spring boot?
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!