java expression 强制出现_java - 无法通过地图调用SPeL Expression.setValue() - 堆栈内存溢出...

我回来的例外情况没有帮助。

找不到能够将[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;

}

}

}

ERROR 5436 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/back/comment_list.html]")] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'list' cannot be found on null at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:90) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:109) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:328) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值