【问题标题】:Dynamic URL for Custom Http-Client - Spring-XD自定义 Http-Client 的动态 URL - Spring-XD
【发布时间】:2015-12-02 02:41:27
【问题描述】:

我们已经为 spring XD 编写了一个自定义的 Http 客户端,并计划使用这个自定义处理器来调用不同的 API。但是,我们有一种情况,我们需要根据有效负载调用两个不同的 API。除了使用动态路由器(写入命名通道)之外,还有一种方法可以动态设置 URL 以供 http-client 调用?这是当前配置。

<header-filter input-channel="input" output-channel="inputX"
    header-names="x-death" />

<service-activator input-channel="inputX" ref="gw" />

<gateway id="gw" default-request-channel="toHttp"
    default-reply-timeout="0" error-channel="errors" />

<beans:bean id="inputfields"
    class="com.batch.httpclient.HTTPInputProperties">
    <beans:property name="nonRetryErrorCodes" value="${nonRetryErrorCodes}" />
</beans:bean>
<beans:bean id="responseInterceptor"
    class="com.batch.httpclient.ResponseInterceptor">
    <beans:property name="inputProperties" ref="inputfields" />
</beans:bean>

<chain input-channel="errors" output-channel="output">

    <transformer ref="responseInterceptor" />
</chain>


<int-http:outbound-gateway id='batch-http'
    header-mapper="headerMapper" request-channel='toHttp' url-expression="${url}"
    http-method="${httpMethod}" expected-response-type='java.lang.String'
    charset='${charset}' reply-timeout='${replyTimeout}' reply-channel='output'>
</int-http:outbound-gateway>

<beans:bean id="headerMapper"
    class="org.springframework.integration.http.support.DefaultHttpHeaderMapper"
    factory-method="outboundMapper">
    <beans:property name="outboundHeaderNames" value="*" />
    <beans:property name="userDefinedHeaderPrefix" value="" />
</beans:bean>

<channel id="output" />
<channel id="input" />
<channel id="inputX" />
<channel id="toHttp" />

【问题讨论】:

    标签: spring spring-integration spring-xd


    【解决方案1】:

    如果它基于有效负载类型,那么这样的东西应该可以工作......

    url-expression="payload.getClass().getSimpleName().equals("Bar") ? 'http://foo/bar' : 'http://foo/baz'"
    

    如果它是有效载荷上的一个属性,那么

    url-expression="payload.foo.equals("bar") ? 'http://foo/bar' : 'http://foo/baz'"
    

    编辑

    url-expression="payload.foo.equals("bar") ? '${url1}' : '${url2}'"
    

    这需要更改属性元数据类以重命名url 并添加url2

    【讨论】:

    • 感谢您的回复。由于您提到的此配置位于上下文 xml 中,我是否可以不将此自定义 http-client 作为单独实例重用在不同的流中以调用第三个 API(例如 [link] foo/xyz)?请确认。
    • 这只是一个示例。您的http-client 模块有url-expression="${url}",因此,对于不同的流实例,您应该使用不同的--url 选项值。
    • 如果您也增强了属性元数据,您也可以将其他 url 添加为属性 - 请参阅我的编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多