5

我正在尝试实现某种代理作为我的数据流的一部分,我想在我的入站网关上接收一个 http 请求并通过出站网关传递它。我想保留所有查询字符串参数。我的网关配置是:

<int:channel id="searchRequestChannel" />
<int:channel id="searchReplyChannel" />

<int-http:inbound-gateway id="searchRequestInboundGateway"      
    supported-methods="GET" 
    request-channel="searchRequestChannel"
    reply-channel="searchReplyChannel"      
    path="/services/normalization"
    reply-timeout="50000"
/>

<int-http:outbound-gateway id="searchServiceGateway"
    http-method="GET"
    request-channel="searchRequestChannel"
    url="http://localhost:8080/query"
    extract-request-payload="false"
    expected-response-type="java.lang.String"
    reply-timeout="50000"
    charset="UTF-8"
/>

我预计它将按以下方式工作:

  • 客户端向入站网关/services/normalization发送请求:

    GET /services/normalization q=cat&exclude=black

  • 入站网关接收请求并通过searchRequestChannel将其发送到出站网关。

  • 出站网关将整个请求发送到外部服务:

    GET /查询 q=cat&exclude=black

但实际上,出站网关发送不包含任何查询参数的空请求:

GET /query

所以我的问题是,通过出站网关发送在入站网关上接受的 http 请求的最简单方法是什么。换句话说,如何通过 spring 集成工具实现简单的代理?

4

2 回答 2

5

这有点杂乱无章,但有效;将DispatcherServlet请求绑定到线程...

<int-http:inbound-gateway id="searchRequestInboundGateway"      
    supported-methods="GET" 
    request-channel="searchRequestEnricherChannel"
    reply-channel="searchReplyChannel"      
    path="/services/normalization{queryString}"
    reply-timeout="50000"
/>

<int:header-enricher input-channel="searchRequestEnricherChannel" output-channel="searchRequestChannel">
    <int:header name="queryString" 
        expression="T(org.springframework.web.context.request.RequestContextHolder).requestAttributes.request.queryString" />
</int:header-enricher>

然后在出站端,使用

<int-http:outbound-gateway id="searchServiceGateway"
    http-method="GET"
    request-channel="searchRequestChannel"
    url="http://localhost:8080/query?{queryString}"
    encode-uri="false"
    extract-request-payload="false"
    expected-response-type="java.lang.String"
    reply-timeout="50000"
    charset="UTF-8">
    <uri-variable name="queryString" expression="headers.queryString" />
</int-http:outbound-gateway>

但是,这不适用于 2.2.x 和更早版本,因为查询字符串是在出站端编码的(foo=bar&baz=qux变成foo%3Dbar%26baz%3Dqux)。在 3.0 中,我们添加了不使用属性对 URI 进行编码的功能,方法是使用encode-uri="false". 这在发行版中尚不可用,但在3.0.0.BUILD-SNAPSHOT.

编辑:

以上是适用于所有查询字符串的通用解决方案;如果您知道实际参数,另一种解决方案是分别提取每个参数并在出站端重建查询字符串...

<int-http:inbound-gateway ... >
    <int-http:header name="foo" expression="#requestParams.foo.get(0)"/>                          
    <int-http:header name="baz" expression="#requestParams.baz.get(0)"/>
</int-http:inbound-gateway>

<int-http:outbound-gateway request-channel="requestChannel" 
                           url="http://localhost:18080/http/receiveGateway?foo={foo}&amp;baz={baz}"
                           http-method="POST"
                           expected-response-type="java.lang.String">
    <int-http:uri-variable name="foo" expression="headers.foo"/>
    <int-http:uri-variable name="baz" expression="headers.baz"/>
</int-http:outbound-gateway>

在入站方面,如果我们提供 queryString 作为第一类表达式变量会更好#queryString

请随时打开“改进”JIRA 问题

于 2013-05-07T17:17:09.770 回答
4

我自己的解决方案是使用转换器,将消息有效负载(查询字符串参数映射)中的参数转换为准备好的查询字符串,并在出站网关中使用url 表达式来避免查询字符串编码:

<bean id="payloadToQueryString" 
    class="com.dph.integration.PayloadToQueryStringTransformer" />

<int-http:inbound-gateway id="searchRequestInboundGateway"      
 supported-methods="GET"
 request-channel="searchRequestChannel"
 path="/services/normalization"
 reply-timeout="50000" />

<int:transformer input-channel="searchRequestChannel" 
     output-channel="searchGatewayChannel" 
     ref="payloadToQueryString" method="transform" />

<int-http:outbound-gateway id="searchServiceGateway"
    http-method="GET"
    request-channel="searchGatewayChannel"
    url-expression="'http://localhost:8080/query?' + payload"
    expected-response-type="java.lang.String"
    reply-timeout="50000"
    charset="UTF-8">
</int-http:outbound-gateway>

PayloadToQueryStringTransformer 类是:

public class PayloadToQueryStringTransformer extends AbstractTransformer {

@Override
protected Object doTransform(final Message<?> message) throws Exception {
    return MessageBuilder
        .withPayload(urlEncodeUTF8(((MultiValueMap) message.getPayload()).toSingleValueMap()))
        .copyHeaders(message.getHeaders())
        .build();
}

private static String urlEncodeUTF8(final String s) {
    try {
        return URLEncoder.encode(s, "UTF-8");
    } catch (final UnsupportedEncodingException e) {
        throw new UnsupportedOperationException(e);
    }
}
private static String urlEncodeUTF8(final Map<?,?> map) {
    final StringBuilder sb = new StringBuilder();
    for (final Map.Entry<?,?> entry : map.entrySet()) {
        if (sb.length() > 0) {
            sb.append("&");
        }
        sb.append(String.format("%s=%s",
                urlEncodeUTF8(entry.getKey().toString()),
                urlEncodeUTF8(entry.getValue().toString())
                ));
    }
    return sb.toString();
}

}
于 2013-05-08T16:39:35.817 回答