Spring集成Groovy脚本和SpEL

Spring集成Groovy脚本和SpEL,groovy,spring-integration,spring-el,Groovy,Spring Integration,Spring El,我试图在spring集成配置中使用Groovy脚本实现特定场景。有两个错误我无法解决或超出了我的理解范围。请原谅,因为我不太精通groovy脚本 我的库中有spring-integration-groovy-3.0.2.RELEASE.jar 下面是我的spring集成配置: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta

我试图在spring集成配置中使用Groovy脚本实现特定场景。有两个错误我无法解决或超出了我的理解范围。请原谅,因为我不太精通groovy脚本

我的库中有
spring-integration-groovy-3.0.2.RELEASE.jar

下面是我的spring集成配置:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:int="http://www.springframework.org/schema/integration" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:stream="http://www.springframework.org/schema/integration/stream"
   xmlns:int-groovy="http://www.springframework.org/schema/integration/groovy" xmlns:lang="http://www.springframework.org/schema/lang"
   xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
        http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Works fine: Using service-activator and SpEL to call a java bean.method(params) -->
    <int:chain input-channel="requestChannel" output-channel="responseChannel">
        <int-xml:xslt-transformer xsl-templates="BookingClassFilter_Template" result-transformer="resultToDOMSource" />
        <int:service-activator expression="@errorDetectionUtil.checkAndThrowExceptionOnError(#root)" />
    </int:chain>

    // Note: I want to replace the above int:chain with the below. Only the below section appears in my final configuration file deployed. The above int:chain is only as an example for Q&A.
    <!-- NOT WORKING: Using Groovy script -->
    <int:chain id="businessLayerResponse" input-channel="requestChannel" output-channel="responseChannel">
        <int-xml:xslt-transformer xsl-templates="BookingClassFilter_Template" result-transformer="resultToDOMSource" />
        <int-groovy:script>
             def valBool = errorDetectionUtil.hasErrors(payload)
             if(valBool) throw new AppException(#root)     
             return #root    
        </int-groovy:script>
    </int:chain>
</beans>
  • 本身不是组件。它只能反映其他EIP特定组件的特性
  • 因此,您的
    也将在那里。但是使用
    而不是
    表达式

  • #root
    变量是SpEL的一部分,它正是
    消息
    。对于脚本,它被拆分为
    有效负载
    标题
    变量。是的,您应该在没有
    #
    的情况下使用它们 右键自Spring集成以来,Groovy脚本中提供了您的
    errorDetectionUtil

  • 本身不是组件。它只能反映其他EIP特定组件的特性
  • 因此,您的
    也将在那里。但是使用
    而不是
    表达式

  • #root
    变量是SpEL的一部分,它正是
    消息
    。对于脚本,它被拆分为
    有效负载
    标题
    变量。是的,您应该在没有
    #
    的情况下使用它们
    从Spring集成以来,Groovy脚本中提供了您的
    errorDetectionUtil

    对my Spring集成的以下更改解决了此问题:

    <int:chain id="businessLayerResponse" input-channel="requestChannel" output-channel="responseChannel">
        <int-xml:xslt-transformer xsl-templates="BookingClassFilter_Template" result-transformer="resultToDOMSource" />
        <int:service-activator>
           <int-groovy:script>
             def valBool = errorDetectionUtil.hasErrors(payload)
             def message = org.springframework.integration.support.MessageBuilder.withPayload(payload).copyHeaders(headers).build()
             if(java.lang.Boolean.valueOf(valBool)) { 
                 throw new x.y.z.AppException(message) 
             }     
             return message
           </int-groovy:script>
        </int:service-activator>
    </int:chain>
    
    
    def valBool=errorDetectionUtil.hasErrors(有效负载)
    def message=org.springframework.integration.support.MessageBuilder.withPayload(payload).copyHeaders(headers.build())
    if(java.lang.Boolean.valueOf(valBool)){
    抛出新的x.y.z.AppException(消息)
    }     
    回信
    

    注意:脚本中需要完全限定的java类名。

    对my spring integration的以下更改解决了该问题:

    <int:chain id="businessLayerResponse" input-channel="requestChannel" output-channel="responseChannel">
        <int-xml:xslt-transformer xsl-templates="BookingClassFilter_Template" result-transformer="resultToDOMSource" />
        <int:service-activator>
           <int-groovy:script>
             def valBool = errorDetectionUtil.hasErrors(payload)
             def message = org.springframework.integration.support.MessageBuilder.withPayload(payload).copyHeaders(headers).build()
             if(java.lang.Boolean.valueOf(valBool)) { 
                 throw new x.y.z.AppException(message) 
             }     
             return message
           </int-groovy:script>
        </int:service-activator>
    </int:chain>
    
    
    def valBool=errorDetectionUtil.hasErrors(有效负载)
    def message=org.springframework.integration.support.MessageBuilder.withPayload(payload).copyHeaders(headers.build())
    if(java.lang.Boolean.valueOf(valBool)){
    抛出新的x.y.z.AppException(消息)
    }     
    回信
    


    注意:脚本中需要完全限定的java类名。

    谢谢Artem!我使用
    内部
    进行了实现更改。是的,我去掉了
    componentName
    异常。但是我需要发送
    AppException
    中的
    消息
    对象。在抛出
    AppException
    时,是否有一种方法可以设置
    消息
    对象?例如
    抛出新的AppException(#root)
    很高兴听到!如果你不介意的话,接受答案是一个很好的语气。我刚刚编辑了我之前的评论。你能再看一遍吗?谢谢您可以使用
    MessageBuilder.withPayload(payload).copyHeaders(headers).build()
    根据Groovy脚本状态为您的用例创建
    Message
    。这正是我想要的。谢谢你的指导。我将更新答案部分。谢谢你!我使用
    内部
    进行了实现更改。是的,我去掉了
    componentName
    异常。但是我需要发送
    AppException
    中的
    消息
    对象。在抛出
    AppException
    时,是否有一种方法可以设置
    消息
    对象?例如
    抛出新的AppException(#root)
    很高兴听到!如果你不介意的话,接受答案是一个很好的语气。我刚刚编辑了我之前的评论。你能再看一遍吗?谢谢您可以使用
    MessageBuilder.withPayload(payload).copyHeaders(headers).build()
    根据Groovy脚本状态为您的用例创建
    Message
    。这正是我想要的。谢谢你的指导。我将更新答案部分。???当你的答案是基于另一个答案时,添加你自己的答案是不好的语气…嗨,阿泰姆,很抱歉,我一开始不理解你所说的
    接受答案的好语气
    添加你自己的答案是不好的语气,当它是基于另一个答案时
    。在这篇文章中,我有没有选择不接受评论作为答案?我会做出必要的改变,这将是我今后的指导方针。没问题!这只是例行公事,但我喜欢参与这场赌博:-)。你应该跟着哈哈哈!!终于明白了。还有一个答案。谢谢:)???当你的答案是基于另一个答案时,添加你自己的答案是不好的语气…嗨,阿泰姆,很抱歉,我一开始不理解你所说的
    接受答案的好语气
    添加你自己的答案是不好的语气,当它是基于另一个答案时
    。在这篇文章中,我有没有选择不接受评论作为答案?我会做出必要的改变,这将是我今后的指导方针。没问题!这只是例行公事,但我喜欢参与这场赌博:-)。你应该跟着哈哈哈!!终于明白了。还有一个答案。谢谢:)
    <int:chain id="businessLayerResponse" input-channel="requestChannel" output-channel="responseChannel">
        <int-xml:xslt-transformer xsl-templates="BookingClassFilter_Template" result-transformer="resultToDOMSource" />
        <int:service-activator>
           <int-groovy:script>
             def valBool = errorDetectionUtil.hasErrors(payload)
             def message = org.springframework.integration.support.MessageBuilder.withPayload(payload).copyHeaders(headers).build()
             if(java.lang.Boolean.valueOf(valBool)) { 
                 throw new x.y.z.AppException(message) 
             }     
             return message
           </int-groovy:script>
        </int:service-activator>
    </int:chain>