如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

HTTP 头映射

Spring Integration 为 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,并通过header-mapper属性将其注入到适配器中。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不支持的操作,可以直接实现HeaderMapper策略接口并提供您实现的引用。spring-doc.cadn.net.cn