对于最新稳定版本,请使用 Spring Integration 7.0.0spring-doc.cadn.net.cn

HTTP头映射

Spring 集成支持HTTP请求和HTTP响应的HTTP头部映射。spring-doc.cadn.net.cn

默认情况下,所有标准 HTTP 头部都从消息映射到 HTTP 请求或响应头部,无需额外配置。 不过,如果你需要进一步的自定义,可以通过利用命名空间支持来提供额外的配置。 你可以提供一个逗号分隔的头部名称列表,也可以包含简单的模式,并用“*”字符作为万用符。 提供此类值覆盖默认行为。 基本上,它假设你在那个阶段完全掌控了局面。 不过,如果你想包含所有标准的HTTP头部,可以使用快捷方式模式:HTTP_REQUEST_HEADERSHTTP_RESPONSE_HEADERS. 以下列表展示了两个例子(第一个使用万用符):spring-doc.cadn.net.cn

<int-http:outbound-gateway id="httpGateway"
    url="http://localhost/test2"
    mapped-request-headers="thing1, thing2"
    mapped-response-headers="X-*, HTTP_RESPONSE_HEADERS"
    channel="someChannel"/>

<int-http:outbound-channel-adapter id="httpAdapter"
    url="http://localhost/test2"
    mapped-request-headers="thing1, thing2, HTTP_REQUEST_HEADERS"
    channel="someChannel"/>

适配器和网关使用DefaultHttpHeaderMapper该系统现在为入站和出站适配器提供了两种静态工厂方法,以便正确地应用方向(根据需要映射 HTTP 请求和响应的进出)。spring-doc.cadn.net.cn

如果你需要进一步的自定义,也可以配置一个DefaultHttpHeaderMapper并通过头部映射器属性。spring-doc.cadn.net.cn

在5.0版本之前,DefaultHttpHeaderMapper用户自定义的非标准HTTP头部的默认前缀为X-. 5.0版本将默认前缀改为空字符串。 根据RFC-6648,现在不鼓励使用此类前缀。 你仍然可以通过设置DefaultHttpHeaderMapper.setUserDefinedHeaderPrefix()财产。 以下示例为HTTP网关配置了一个头部映射器:spring-doc.cadn.net.cn

<int-http:outbound-gateway id="httpGateway"
    url="http://localhost/test2"
    header-mapper="headerMapper"
    channel="someChannel"/>

<bean id="headerMapper" class="o.s.i.http.support.DefaultHttpHeaderMapper">
    <property name="inboundHeaderNames" value="thing1*, *thing2, thing3"/>
    <property name="outboundHeaderNames" value="a*b, d"/>
</bean>

如果你需要做些别的事情DefaultHttpHeaderMapper支持,你可以实现头部映射器直接使用战略接口,并提供对你实施的参考。spring-doc.cadn.net.cn