简体   繁体   English

如何在 jackson/Spring boot 中允许不区分大小写的枚举映射?

[英]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.另一个问题与 Spring 请求参数的自动编组无关。 It has a solution where you manually marshall objects with jackson.它有一个解决方案,您可以使用 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.到目前为止,我发现的唯一方法( initBinding )要求您在编译时指定确切的枚举类。 I am looking for a more generic way to marshall the strings in the JSON request into enums.我正在寻找一种更通用的方法来将 JSON 请求中的字符串编组为枚举。

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.但这需要使用已知的枚举进行编译。

you can see the class org.springframework.core.convert.support.StringToEnumConverterFactory, so you can customize yourself converterFactory like this.你可以看到类org.springframework.core.convert.support.StringToEnumConverterFactory,所以你可以像这样自定义converterFactory。

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 .并添加到 ConverterRegistry 。

@Configuration
public class MyConfiguration {

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

Hope to help you!希望能帮到你!

从 spring 2.0 开始,在application.properties设置以下内容就足够了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM