【问题标题】:Spring Expression Language to convert java.io.File payload to String将 java.io.File 有效负载转换为字符串的 Spring 表达式语言
【发布时间】:2015-11-13 00:20:27
【问题描述】:

我的 spring-integration 上下文文件中有以下 sn-p:

<int:header-enricher input-channel="headerEnricherChannel"
        output-channel="xpathHeaderEnricherChannel">
        <!-- java.io.File -->
        <int:header name="DecryptedRequestFile" expression="payload" />
        <!-- java.lang.String -->
        <int:header name="RequestFileName" expression="payload.getName()" />
        <int:header name="Priority"
            expression="#xpath(payload, '//RequestType') == 'SDR_CIRCLE' ? 'SDR_CIRCLE' : #xpath(payload, '//Priority')" />
        <int:header name="savedPayload" expression="payload" />
        **<int:header name="RequestFileAsString"
                    expression="#{ T(org.apache.commons.io.FileUtils).readFileToString(payload) }" />**
    </int:header-enricher>

headerEnricherChannel 的输入消息的有效负载是java.io.File。我想使用 Apache FileUtils 将此有效负载转换为字符串。但是,这样做会引发如下错误。请有人指出我正确的方向吗?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.transformer.HeaderEnricher#0': Cannot create inner bean '(inner bean)#38a1aa85' of type [org.springframework.integration.transformer.support.ExpressionEvaluatingHeaderValueMessageProcessor] while setting constructor argument with key [RequestFileAsString]; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 35): Property or field 'payload' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?

【问题讨论】:

    标签: spring-integration spring-el


    【解决方案1】:

    该表达式必须与您的所有其他表达式一样 - 没有 #{}

    <int:header name="RequestFileAsString"
           expression="T(org.apache.commons.io.FileUtils).readFileToString(payload)" />
    

    区别在于:

    • 第一个(带有#{})是初始化,一次性表达式。见here

    • 另一种情况是 Spring Integration 的一部分,它允许在运行时针对每个 message 评估表达式。在 Spring 集成手册中随处可见。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      相关资源
      最近更新 更多