String spring 3.0类型转换,从预定义格式的字符串转换为以键作为枚举类型、以值作为任何类型的映射
我正在尝试使用Spring3.0的类型转换功能创建一个通用转换器,用于转换格式为“:,:,:” 指向包含键-值对的映射,其中键可以是枚举类型,值可以是任何用户定义的或Java的内置类型 下面是我试用过的代码 注意:我不擅长使用泛型,所以如果我以错误的方式使用泛型,请容忍我String spring 3.0类型转换,从预定义格式的字符串转换为以键作为枚举类型、以值作为任何类型的映射,string,spring-mvc,map,type-conversion,String,Spring Mvc,Map,Type Conversion,我正在尝试使用Spring3.0的类型转换功能创建一个通用转换器,用于转换格式为“:,:,:” 指向包含键-值对的映射,其中键可以是枚举类型,值可以是任何用户定义的或Java的内置类型 下面是我试用过的代码 注意:我不擅长使用泛型,所以如果我以错误的方式使用泛型,请容忍我 import java.lang.reflect.GenericDeclaration; import java.lang.reflect.TypeVariable; import java.util.HashMap; imp
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.TypeVariable;
import java.util.HashMap;
import java.util.Map;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
/**
* Converts a string in a format
* "<KEY2>:<VAL>,<KEY2>:<VAL2>,<KEY3>:<VAL3>"
* to a {@link Map} instance holding the key-value pairs <i>where key
* can be an Enum type and value can be of any type</i>.
*
* @author jigneshg
*
* @param <K>
* @param <V>
*/
public class StringToMapConvertorFactory<V> implements ConverterFactory<String, Map<Enum<?>, V>> {
private static final String COMMA = ",";
private static final String COLON = ":";
@Override
public <T extends Map<Enum<?>, V>> Converter<String, T> getConverter(
Class<T> targetType) {
return new StringToMapConverter<T>(targetType);
}
private final class StringToMapConverter<T extends Map<Enum<?>, V>> implements Converter<String,T> {
public StringToMapConverter(Class<T> targetType) {
}
@SuppressWarnings("unchecked")
@Override
public T convert(String source) {
checkArg(source);
String[] keyValuePairs = source.split(COMMA);
// value at index 0 is assumed as the key
// value at index 0 is assumed as the value
Map<Enum<?>,V> resultMap = new HashMap<Enum<?>, V>();
String[] keyValueArr = null;
String key = null;
String value = null;
for (String keyValuePair : keyValuePairs) {
keyValueArr = keyValuePair.split(COLON);
key = keyValueArr[0];
value = keyValueArr[1];
// TODO: How to specify the enumType here ??
resultMap.put(Enum.valueOf(enumType, key.trim()), (V) value);
}
return resultMap;
}
private void checkArg(String source) {
// In the spec, null input is not allowed
if (source == null) {
throw new IllegalArgumentException("null source is in allowed");
}
}
}
}
import java.lang.reflect.GenericDeclaration;
导入java.lang.reflect.TypeVariable;
导入java.util.HashMap;
导入java.util.Map;
导入org.springframework.core.convert.converter.converter;
导入org.springframework.core.convert.converter.ConverterFactory;
/**
*将字符串转换为格式
*键2:VAL,键2:VAL2,键3:VAL3
*到一个{@link Map}实例,该实例包含键-值对,其中
*可以是枚举类型,值可以是任何类型。
*
*@author jigneshg
*
*@param
*@param
*/
公共类StringToMapConvertorFactory实现ConverterFactory,V>>Converter getConverter(
类(目标类型){
将新的StringToMapConverter(targetType)返回给转换程序;
}
私有最终类StringToMapConverter,V>resultMap=new HashMap您可以实现而不是Converter
,访问密钥类型为TypeDescriptor.getMapKeyType()
您可以实现而不是Converter
,访问密钥类型为TypeDescriptor.getMapKeyType())
感谢axtavt的指导。我已经达到了我的要求。虽然我在实现GenericConverter后遇到了一些问题,但我已经解决了,我的解决方案也发布在这里。感谢axtavt的指导。我已经达到了我的要求。虽然我在实现GenericConverter后遇到了一些问题,但我已经解决了问题,并且我的我在这里发布的解决方案