我无法将下面的转换器工厂类转换为将字符串在Spring 5中转换为枚举类型以转换为Kotlin。请帮我。
@Component
public class StringToEnumConverterFactory
implements ConverterFactory<String, Enum> {
private static class StringToEnumConverter<T extends Enum>
implements Converter<String, T> {
private Class<T> enumType;
public StringToEnumConverter(Class<T> enumType) {
this.enumType = enumType;
}
public T convert(String source) {
return (T) Enum.valueOf(this.enumType, source.trim());
}
}
@Override
public <T extends Enum> Converter<String, T> getConverter(
Class<T> targetType) {
return new StringToEnumConverter(targetType);
}
}
答案 0 :(得分:1)
我做了类似的事情:
class StringToEnumConverterFactory : ConverterFactory<String, Enum<*>?> {
override fun <E : Enum<*>?> getConverter(clazz: Class<E>): Converter<String, E> {
return StringToEnumConverter(clazz)
}
}
class StringToEnumConverter<T : Enum<*>?>(private val enumClass: Class<T>) : Converter<String, T> {
override fun convert(source: String): T? {
if (source.isEmpty()) {
return null
}
return enumClass.enumConstants.first { it!!.name == source.trim() }
}
}
我希望这会有所帮助!