我使用SpEL ()计算模板字符串,代码如下所示:
Expression exp = parser.parseExpression(templatedString, templateParserContext);
return exp.getValue(readOnlyEvalContext, contextRoot);当templatedString == "flow.config:${flow.config}“和contextRoot中的"flow.config”是LinkedHashMap (由杰克逊在JSON反序列化期间创建)时,我得到以下异常:
org.springframework.expression.spel.SpelEvaluationException: EL1001E: Type conversion problem, cannot convert from java.util.LinkedHashMap<?, ?> to java.lang.String为了解决这个问题,我尝试创建一个转换器来使用ObjectMapper序列化LinkedHashMap,以便在我的计算表达式中显示映射的内联JSON。
@Configuration
public class ConverterConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new ConverterLinkedHashMapToString());
}
}@Component
public class ConverterLinkedHashMapToString implements Converter<LinkedHashMap<?, ?>, String> {
@Override
public String convert(LinkedHashMap<?, ?> source) {
return objMapper.writeValueAsString(source);
}
@Override
public <U> Converter<LinkedHashMap<?, ?>, U> andThen(Converter<? super String, ? extends U> after) {
return Converter.super.andThen(after);
}
}不管我做什么,我似乎都做不到。我只是一直得到同样的例外。我可能做错了什么,但尽管我花了大约一天的时间寻找和挣扎,但我一直未能解决这个问题。任何帮助都将不胜感激!谢谢!
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1001E: Type conversion problem, cannot convert from java.util.LinkedHashMap<?, ?> to java.lang.String
at org.springframework.expression.spel.support.StandardTypeConverter.convertValue(StandardTypeConverter.java:87) ~[spring-expression-5.3.22.jar:5.3.22]
at org.springframework.expression.common.ExpressionUtils.convertTypedValue(ExpressionUtils.java:57) ~[spring-expression-5.3.22.jar:5.3.22]
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:378) ~[spring-expression-5.3.22.jar:5.3.22]
at org.springframework.expression.common.CompositeStringExpression.getValue(CompositeStringExpression.java:129) ~[spring-expression-5.3.22.jar:5.3.22]
at org.springframework.expression.common.CompositeStringExpression.getValue(CompositeStringExpression.java:43) ~[spring-expression-5.3.22.jar:5.3.22]
[...]
... 3 common frames omitted
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.LinkedHashMap<?, ?>] to type [java.lang.String]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322) ~[spring-core-5.3.22.jar:5.3.22]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-5.3.22.jar:5.3.22]
at org.springframework.expression.spel.support.StandardTypeConverter.convertValue(StandardTypeConverter.java:82) ~[spring-expression-5.3.22.jar:5.3.22]
... 38 common frames omitted发布于 2022-08-12 03:52:46
通过将Converter添加到EvaluationContext中,通过TypeConverter,我能够让它与您的转换器一起工作。将Converter添加到EvaluationContext的代码如下所示:
GenericConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new ConverterLinkedHashMapToString());
TypeConverter typeConverter = new StandardTypeConverter(conversionService);
StandardEvaluationContext evalContext = new StandardEvaluationContext();
evalContext.setTypeConverter(typeConverter);
Expression exp = parser.parseExpression(templatedString, templateParserContext);
String message = (String) exp.getValue(evalContext, contextRoot);
System.out.println(message);不过,我没有在spring/spring引导应用程序中测试这个。我使用独立的groovy脚本进行了测试。下面是供参考的完整脚本:
@Grab(group='org.springframework', module='spring-expression', version='5.3.22')
@Grab(group='com.fasterxml.jackson.core', module='jackson-databind', version='2.13.3')
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeConverter;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.TypeConverter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService;
ExpressionParser parser = new SpelExpressionParser();
ParserContext templateParserContext = new TemplateParserContext('${', '}');
String templatedString = "flow.config: \${flow['config']}";
Dummy contextRoot = new Dummy();
GenericConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new ConverterLinkedHashMapToString());
TypeConverter typeConverter = new StandardTypeConverter(conversionService);
StandardEvaluationContext evalContext = new StandardEvaluationContext();
evalContext.setTypeConverter(typeConverter);
Expression exp = parser.parseExpression(templatedString, templateParserContext);
String message = (String) exp.getValue(evalContext, contextRoot);
System.out.println(message);
class Dummy {
Map<String, ?> flow = ['config': ['hola': 'hello']];
}
class ConverterLinkedHashMapToString implements Converter<LinkedHashMap<?, ?>, String> {
private ObjectMapper objMapper = new ObjectMapper();
@Override
public String convert(LinkedHashMap<?, ?> source) {
return objMapper.writeValueAsString(source);
}
}在将Converter添加到EvaluationContext之前,我得到了相同的SpelEvaluationException。添加转换器后,代码运行良好,并将包含映射的JSON值的解析字符串打印为flow.config: {"hola":"hello"}
https://stackoverflow.com/questions/73296047
复制相似问题