Spring Expression Language将java.io.File有效负载转换为String

时间:2015-11-13 00:20:28

标签: spring-integration spring-el

我在spring-integration上下文文件中有以下代码段:

<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在String中转换此有效负载。但是,这样做会引发如下错误。请有人指出我正确的方向吗?

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?

1 个答案:

答案 0 :(得分:0)

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

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

区别在于:

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

  • 另一种情况是Spring Integration的一部分,允许在运行时针对每个message计算表达式。请参阅Spring Integration Manual中的所有内容。