当前位置: 首页 > 知识库问答 >
问题:

从简单语言表达式执行bean方法时发生转换错误

赏阳嘉
2023-03-14

我想在XML DSL中将exchange标头设置为执行bean方法的结果。我在注释中找到了这个问题的解决方案-驼峰-将特定参数从路由传递到通用bean方法,我的代码是

XML DSL:

<setHeader headerName="dateInterval">
    <simple>${bean:tableRequestRouteUtil?method=getIntervalForTable(${in.body})}</simple>
</setHeader>

此时的主体类型是我的特定于域的POJO(类webbanking.dto.W4MMsg)。主体的初始类型是XML String,然后它转换为POJO(由JAXB注释注释):

<dataFormats>
    <jaxb id="xml" prettyPrint="true"
          contextPath="dto"/>
</dataFormats>

在blueprint xml和

<unmarshal ref="xml" id="xml.unmarshall"/> 

骆驼路线中

tableRequest estRouteUtil bean的getIntervalForTable方法的参数类型为webbanking.dto.W4MMsg(我的pojo),返回类型为String。这很好,直到在输入xml中没有出现括号。输入字符串中的括号导致以下异常(我省略了请求正文,它很大):

org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to invoke method: getIntervalForTable('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w4MMsg scheme="WAY4Doc" msg_type="Doc" direction="Rq" version="2.0">...') on tableRequestRouteUtil due to: org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to invoke method: getIntervalForTable('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w4MMsg scheme="WAY4Doc" msg_type="Doc" direction="Rq" version="2.0">...') on tableRequestRouteUtil due to: org.apache.camel.component.bean.ParameterBindingException: Error during parameter binding on method: public router.domain.HistoryInterval router.TableRequestRouteUtil.getIntervalForTable(webbanking.dto.W4MMsg) at parameter #0 with type: class webbanking.dto.W4MMsg with value type: class java.lang.String and value: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w4MMsg scheme="WAY4Doc" msg_type="Doc" direction="Rq" version="2.0">...
    at org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:117)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:132)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.builder.ExpressionBuilder$65.evaluate(ExpressionBuilder.java:1541)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.support.ExpressionAdapter.evaluate(ExpressionAdapter.java:36)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.language.simple.ast.SimpleFunctionStart$1.evaluate(SimpleFunctionStart.java:101)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.builder.SimpleBuilder.evaluate(SimpleBuilder.java:83)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.builder.ProcessorBuilder$4.process(ProcessorBuilder.java:103)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:111)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:51)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:110)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:111)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.component.jetty.CamelContinuationServlet.service(CamelContinuationServlet.java:151)[234:org.apache.camel.camel-jetty:2.12.0.redhat-610379]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)[91:org.apache.geronimo.specs.geronimo-servlet_3.0_spec:1.0]
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1496)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.servlets.MultiPartFilter.doFilter(MultiPartFilter.java:136)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.apache.camel.component.jetty.CamelFilterWrapper.doFilter(CamelFilterWrapper.java:44)[234:org.apache.camel.camel-jetty:2.12.0.redhat-610379]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1467)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:499)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.server.Server.handle(Server.java:370)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)[92:org.eclipse.jetty.aggregate.jetty-all-server:8.1.14.v20131031]
    at java.lang.Thread.run(Thread.java:724)[:1.7.0_25]
Caused by: org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to invoke method: getIntervalForTable('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w4MMsg scheme="WAY4Doc" msg_type="Doc" direction="Rq" version="2.0">...') on tableRequestRouteUtil due to: org.apache.camel.component.bean.ParameterBindingException: Error during parameter binding on method: public router.domain.HistoryInterval router.TableRequestRouteUtil.getIntervalForTable(webbanking.dto.W4MMsg) at parameter #0 with type: class webbanking.dto.W4MMsg with value type: class java.lang.String and value: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w4MMsg scheme="WAY4Doc" msg_type="Doc" direction="Rq" version="2.0">...
    at org.apache.camel.language.bean.BeanExpression$OgnlInvokeProcessor.process(BeanExpression.java:285)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:114)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    ... 56 more
Caused by: org.apache.camel.component.bean.ParameterBindingException: Error during parameter binding on method: public router.domain.HistoryInterval router.TableRequestRouteUtil.getIntervalForTable(webbanking.dto.W4MMsg) at parameter #0 with type: class webbanking.dto.W4MMsg with value type: class java.lang.String and value: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w4MMsg scheme="WAY4Doc" msg_type="Doc" direction="Rq" version="2.0">...
    at org.apache.camel.component.bean.MethodInfo$2.evaluateParameterValue(MethodInfo.java:572)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.component.bean.MethodInfo$2.evaluate(MethodInfo.java:472)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.component.bean.MethodInfo.createMethodInvocation(MethodInfo.java:239)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.component.bean.BeanInfo.createInvocation(BeanInfo.java:278)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.component.bean.BeanInfo.createInvocation(BeanInfo.java:180)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:148)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:105)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:67)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.language.bean.BeanExpression$InvokeProcessor.process(BeanExpression.java:189)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.language.bean.BeanExpression$OgnlInvokeProcessor.process(BeanExpression.java:281)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    ... 57 more
Caused by: org.apache.camel.TypeConversionException: Error during type conversion from type: java.lang.String to the required type: webbanking.dto.W4MMsg with value <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w4MMsg scheme="WAY4Doc" msg_type="Doc" direction="Rq" version="2.0">...
    at org.apache.camel.converter.jaxb.FallbackTypeConverter.convertTo(FallbackTypeConverter.java:103)[231:org.apache.camel.camel-jaxb:2.12.0.redhat-610379]
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.doConvertTo(BaseTypeConverterRegistry.java:315)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:164)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    at org.apache.camel.core.osgi.OsgiTypeConverter.mandatoryConvertTo(OsgiTypeConverter.java:116)[150:org.apache.camel.camel-blueprint:2.12.0.redhat-610379]
    at org.apache.camel.component.bean.MethodInfo$2.evaluateParameterValue(MethodInfo.java:564)[142:org.apache.camel.camel-core:2.12.0.redhat-610379]
    ... 66 more
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.ctc.wstx.exc.WstxEOFException: Unexpected EOF; was expecting a close tag for element <Value>
 at [row,col {unknown-source}]: [186,77]]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:426)[128:org.apache.servicemix.bundles.jaxb-impl:2.2.1.1_2]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:362)[128:org.apache.servicemix.bundles.jaxb-impl:2.2.1.1_2]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:332)[128:org.apache.servicemix.bundles.jaxb-impl:2.2.1.1_2]
    at org.apache.camel.converter.jaxb.FallbackTypeConverter.unmarshal(FallbackTypeConverter.java:276)[231:org.apache.camel.camel-jaxb:2.12.0.redhat-610379]
    at org.apache.camel.converter.jaxb.FallbackTypeConverter.unmarshall(FallbackTypeConverter.java:181)[231:org.apache.camel.camel-jaxb:2.12.0.redhat-610379]
    at org.apache.camel.converter.jaxb.FallbackTypeConverter.convertTo(FallbackTypeConverter.java:95)[231:org.apache.camel.camel-jaxb:2.12.0.redhat-610379]
    ... 70 more
Caused by: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF; was expecting a close tag for element <Value>
 at [row,col {unknown-source}]: [186,77]
    at com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(StreamScanner.java:677)[156:woodstox-core-asl:4.2.0]
    at com.ctc.wstx.sr.BasicStreamReader.throwUnexpectedEOF(BasicStreamReader.java:5532)[156:woodstox-core-asl:4.2.0]
    at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2720)[156:woodstox-core-asl:4.2.0]
    at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1072)[156:woodstox-core-asl:4.2.0]
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:192)[128:org.apache.servicemix.bundles.jaxb-impl:2.2.1.1_2]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360)[128:org.apache.servicemix.bundles.jaxb-impl:2.2.1.1_2]
    ... 74 more

我认为这是因为简单语言表达式执行bean方法的方式是将消息转换为字符串,用这个字符串替换占位符${in.body},然后执行方法,使用转换器框架将这个字符串转换回POJO。此字符串中的右括号会打断消息体,因为它被解释为方法参数的右括号。这是真的吗?有没有办法解决这个问题?从bean的执行中设置头的正确方法应该是什么?

共有1个答案

曾飞雨
2023-03-14

尝试不使用正文,因为驼峰将自动绑定到参数,而正文是绑定的默认值

method=getIntervalForTable

然后不要使用(),所以只告诉Camel方法名称,它可以使用具有参数的方法。

我还认为我们已经在即将到来的Camel 2.15.1中修复了bean语言的()iusse

 类似资料:
  • 我在骆驼路线中使用了这个表达: 然而,它对这个符号感到震惊。 如何构造这个表达式,假定它从消息体上POJO的getter获取一个值,并将其压缩到Exchange上的一个属性(加1)。

  • 问题内容: 我有一个简单的Spring Bean表达式,当我在应用程序上下文文件中定义它时,它的计算效果很好: 现在,我想以编程方式进行相同的评估。我使用了以下代码: 抛出一个异常: 我想我必须以某种方式设置一个根对象,该根对象允许像属性一样配置的bean。但是我还没有开始工作。任何人都已经这样做并且可以提供提示了吗? 问题答案: 实现BeanFactoryAware以获得对bean工厂的引用;然

  • 问题内容: 在我的Java应用程序中,用户可以指定如何从一系列元数据字段中命名其文件。即 然后,我的代码解析这些字段,并相应地重命名文件。但我希望用户能够使用“表达式语言”,以便他们能够说出类似以下内容: 使用,比较长度和大小写等。 我不想从头开始编写此代码,有什么可以为我提供这些代码的插件吗? 编辑:我认为我收到了一些好评,但我的知识让我失望。让我们简化我希望用户能够写的问题 $ if(%dis

  • JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单。JSP EL既可以用来创建算术表达式也可以用来创建逻辑表达式。在JSP EL表达式内可以使用整型数,浮点数,字符串,常量true、false,还有null。 一个简单的语法 典型的,当您需要在JSP标签中指定一个属性值时,只需要简单地使用字符串即可: <jsp:setProperty name="box" propert

  • 问题内容: 是否有将正则表达式从一种流行语言的语法转换为另一种语法的工具?例如,将Python样式的正则表达式转换为Java样式的正则表达式? 或至少有人是否制定了一套规则来进行这些转换? 而且显然某些构造将无法转换。 问题答案: 转至本文,并单击 “ Regex信息对Regex口味的比较”链接,该链接使我进入了一个名为RegexBuddy的工具,听起来它可能可以满足您的要求。

  • 无法将属性“Item”的类型[java.lang.String]的属性值转换为所需的类型[com.spring.first.Item];嵌套异常为java.lang.IllegalArgumentException:无法将属性“Item”的类型[java.lang.String]的值转换为所需的类型[com.spring.first.Item]:未找到匹配的编辑器或转换策略 server.java