我回来的例外情况没有帮助。
找不到能够将[java.lang.String]类型转换为[java.util.Map]类型的转换器
对我来说似乎很清楚。
没有将映射的字符串表示形式转换为Map对象的内置支持。
您可以注册一个自定义函数,或者在SpEL表达式中使用Jackson ObjectMapper bean引用。
编辑
这是一种方法(使用使用Jackson的自定义Converter )...
public class So55485198Application {
public static void main(String[] args) {
Bean bean = new Bean();
getAndSet("list", bean, "abc, def");
getAndSet("map", bean, "{'abc':'def'}");
}
public static void getAndSet(String propertyName, Bean bean, String expression) {
ExpressionParser parser = new SpelExpressionParser();
Expression parsedPropertyNameExpression = parser.parseExpression(propertyName);
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new StringToMapConverter());
SimpleEvaluationContext evalContext = SimpleEvaluationContext.forReadWriteDataBinding()
.withConversionService(conversionService)
.build();
Object currentValue = parsedPropertyNameExpression.getValue(evalContext, bean);
System.out.println("old:" + currentValue);
parsedPropertyNameExpression.setValue(evalContext, bean, expression);
System.out.println("new:" + parsedPropertyNameExpression.getValue(evalContext, bean));
}
static class StringToMapConverter implements Converter> {
private static final ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
}
@SuppressWarnings("unchecked")
@Override
public Map convert(String source) {
try {
return this.objectMapper.readValue(source, LinkedHashMap.class);
}
catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException(e);
}
}
}
static class Bean {
private List list = new ArrayList<>(Arrays.asList("foo", "bar"));
private Map map = new HashMap<>(Collections.singletonMap("foo", "bar"));
public List getList() {
return this.list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return this.map;
}
public void setMap(Map map) {
this.map = map;
}
}
}