繁体   English   中英

Spring集成出站网关想像动态一样使用URL

[英]Spring integration outbound-gateway want to use URL as dynamic like

spring集成,在出站网关中想使用动态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.springframework的内部bean'(inner bean)#6ea2bc93'。而使用键[url]设置bean属性'uriVariableExpressions'时,则为Integration.config.ExpressionFactoryBean]; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为“((内部bean)#6ea2bc93”)的bean时出错:通过构造函数实例化Bean失败; 嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.integration.config.ExpressionFactoryBean]:构造方法抛出了异常; 嵌套异常为java.lang.IllegalArgumentException:expressionString不能为空或null

您可以将元数据(例如URL或http方法)设置为标题。 您甚至可以在设置标头fe时使用Spring EL

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

然后将表达式用于出站网关

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

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

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

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

编辑

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

但是,由于您正在为Requestvalues使用Bean, Requestvalues根本不需要运行时表达式。

<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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM