【问题标题】:Spring integration outbound-gateway want to use URL as dynamic likeSpring集成出站网关希望将URL用作动态的
【发布时间】:2017-10-03 08:16:20
【问题描述】:

spring 集成,在 outbound-gateway 中想使用动态的 URL 像

    <bean id="requestValues"    class="com.src.model.RequestValues"/>
    <int-http:outbound-gateway
                request-channel="reqChannel" url="${UrlValue}"
                http-method="${reqmethod}" expected-response-type="java.lang.String"  header-mapper="headerMapper"
                charset="UTF-8" reply-timeout="5000" reply-channel="responseChannel"  >
            <int-http:uri-variable name="UrlValue" expression="#{requestValues.getUrl()}" />
           <int-http:uri-variable name="reqmethod" expression="#{requestValues.getReqMethod()}" />
        </int-http:outbound-gateway>

这里的Requestvalues是简单的POJO 就像

@Data
public class Requestvalues {

    public String Url;
    public String reqMethod;

}

org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler#0' 的 bean 时出错:无法创建 [org 类型的内部 bean'(inner bean)#6ea2bc93' .springframework.integration.config.ExpressionFactoryBean] 同时使用键 [url] 设置 bean 属性'uriVariableExpressions';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“(内部 bean)#6ea2bc93”的 bean 时出错:通过构造函数实例化 bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.integration.config.ExpressionFactoryBean]:构造函数抛出异常;嵌套异常是 java.lang.IllegalArgumentException: expressionString 不能为空或 null

【问题讨论】:

    标签: java spring spring-integration


    【解决方案1】:

    您可以将 URL 或 http 方法等元数据设置为标头。你甚至可以在设置标题时使用 Spring EL,f.e.

    <int:header-enricher>
        <int:header name="url" value="${url.base}/reports/"/>
    </int:header-enricher>
    

    然后对出站网关使用表达式

     <int-http:outbound-gateway id='httpGateway'
     url-expression="headers['url']"
    ...
      />
    

    【讨论】:

    • Acceptmethod 不是 URI 的一部分,因此不能使用 uri-variables。
    • 感谢您的回复,但这里的小修正 Accept & content-type 工作正常,只是想让 URL 成为动态的,该 URL 是根据特定条件在 java 代码中创建的
    • 嗯,感谢您的回复,请留下,我的疑问是如何使我的 URL 和 http 方法成为动态的,这必须来自 POJO
    【解决方案2】:

    http-method 不是 URI 的一部分;它不支持或使用uri-variables

    请改用http-method-expression="payload.reqMethod"

    同样,Accept 不是 uri 的一部分 - 在出站消息中设置 Accept 标头,它将被映射。

    编辑

    您将运行时表达式与 bean 声明表达式混淆了,正如我所说,如果您想要运行时方法选择,则需要使用方法表达式。

    但是,由于您为 Requestvalues 使用 bean,因此根本不需要运行时表达式。

    <int-http:outbound-gateway
                request-channel="reqChannel" url="#{requestValues.getUrl()}"
                http-method=""#{requestValues.getReqMethod()}" expected-response-type="java.lang.String"  header-mapper="headerMapper"
                charset="UTF-8" reply-timeout="5000" reply-channel="responseChannel"  >
    </int-http:outbound-gateway>
    

    如果您想在运行时根据消息选择方法和 url,您可以使用类似...

    <int-http:outbound-gateway
                request-channel="reqChannel" url="${UrlValue}"
                http-method-expression="headers['method']" expected-response-type="java.lang.String"  header-mapper="headerMapper"
                charset="UTF-8" reply-timeout="5000" reply-channel="responseChannel"  >
            <int-http:uri-variable name="UrlValue" expression="headers['url']" />
    </int-http:outbound-gateway>
    

    标头在上游某处动态设置,或

    <int-http:outbound-gateway
                request-channel="reqChannel" url="${UrlValue}"
                http-method-expression="@requestValues.getReqMethod()" expected-response-type="java.lang.String"  header-mapper="headerMapper"
                charset="UTF-8" reply-timeout="5000" reply-channel="responseChannel"  >
            <int-http:uri-variable name="UrlValue" expression="@requestValues.getUrl()" />
    </int-http:outbound-gateway>
    

    注意使用@ 来引用运行时表达式中的bean。

    【讨论】:

    • 感谢您的回复,但这里的小修正 Accept & content-type 工作正常,只是想让 URL 成为动态的,该 URL 是在特定条件下在 java 代码中创建的
    • &lt;int-http:uri-variable name="Accept" expression="application/json" /&gt; - Acceptcontent type 标头不是从 uri-variable 设置的;您必须在其他地方设置标题,也许在变压器中。
    • 嗯,很好,灰色,现在我修改了我的问题,再次重复我的问题,希望将我的 URL 和 http 方法设为通用
    • 您正在混合运行时和初始化时表达式;查看我的编辑。
    • 这不是答案 - 请改为编辑您的问题。第一个似乎表明您的requestValues 设置不正确;第二个是相同的(对于 URL);您需要显示所有代码。
    猜你喜欢
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多