简体   繁体   English

如何在 spring 集成网关中获取自定义 header 值?

[英]How to get custom header values in spring integration gateway?

This is my first question here.这是我在这里的第一个问题。

My web application is using spring integration for getting 3rd party APIs response.我的 web 应用程序正在使用 spring 集成来获取第 3 方 API 响应。

The response has custom header values start with 'X-'.响应具有以“X-”开头的自定义 header 值。 The X-Values are two. X 值为两个。 (X-AccessToken-Quota-Allotted, X-AccessToken-Quota-Current) (X-AccessToken-Quota-Allotted,X-AccessToken-Quota-Current)

So, I added inboundHeaderNames in header-mapper in config xml as below.因此,我在配置 xml 的 header-mapper 中添加了 inboundHeaderNames,如下所示。

    <int-http:outbound-gateway id="dms.put.outbound.gateway.cert"
        header-mapper="dmsHeaderMapper"
       ...
</int-http:outbound-gateway>

<bean id="dmsHeaderMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper" factory-method="outboundMapper" >
    <property name="outboundHeaderNames" value="X-Request-Id...blabla"/>
    <property name="inboundHeaderNames" value="X-AccessToken-Quota-Allotted, X-AccessToken-Quota-Current"/>
    <property name="userDefinedHeaderPrefix" value=""/>
</bean>

Below is a response.下面是一个回应。 I can see X-AccessToken-Quota-Allotted & X-AccessToken-Quota-Current in headers attribute.我可以在 headers 属性中看到 X-AccessToken-Quota-Allotted 和 X-AccessToken-Quota-Current。

[integration] GenericMessage [payload={"type":"commonResponse","serverName":"uslxacd20022use1c.intranet.local","errorMessage":null}, headers={errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@41208186, X-Request-Id=0ce88cd6-d04b-47dc-b3db-224656fe9090, uri=https://api-server.com/blabla, http_statusCode=200, X-AccessToken-Quota-Allotted=5000 , jsonObjectName=DmsCommonResponseDto, Authorization=Bearer qqdtbhtp5vk2ss69wjpbdfvh, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@41208186, json__TypeId__=class com.blabla.DmsPhoneRequestDto, requestId=0ce88cd6-d04b-47dc-b3db-224656fe9090, X-AccessToken-Quota-Current=3 , id=12072f4b-8403-a328-6a77-aaba935beba2, contentType=application/json, Content-Type=application/json;charset=UTF-8, timestamp=1592791409206}] [集成] GenericMessage [payload={"type":"commonResponse","serverName":"uslxacd20022use1c.intranet.local","errorMessage":null}, headers={errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$ TemporaryReplyChannel@41208186, X-Request-Id=0ce88cd6-d04b-47dc-b3db-224656fe9090, uri=https://api-server.com/blabla, http_statusCode=200, X-AccessToken-Quota-Allotted=5000 , jsonObjectName= DmsCommonResponseDto, Authorization=Bearer qqdtbhtp5vk2ss69wjpbdfvh, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@41208186, json__TypeId__=class com.blabla.DmsPhoneRequestDto, requestId=0ce88cd6-d04b-47dc-b3db-224656fe9090, X-AccessToken-Quota-当前=3 , id=12072f4b-8403-a328-6a77-aaba935beba2, contentType=application/json, Content-Type=application/json;charset=UTF-8, timestamp=1592791409206}]

Almost every filed value is mapped to responseDto's fields, but I don't know how to map X-custom header values to responseDto's field.几乎每个字段值都映射到 responseDto 的字段,但我不知道如何将 map X-custom header 值映射到 responseDto 的字段。

Below are current settings.以下是当前设置。

<int:gateway id="dmsHttpPUTGateway" service-interface="blabla.gateway.DmsHttpPUTGateway" 
        default-request-channel="dms.put.ch.request.header.router"><int:method name="call" /></int:gateway>

<int:header-value-router input-channel="dms.put.ch.reply.header.router" header-name="jsonObjectName">
    <int:mapping value="DmsCommonResponseDto" channel="dms.put.ch.reply.transformer.DmsCommonResponseDto"/>
</int:header-value-router>

<int:channel id="dms.put.ch.reply.transformer.DmsCommonResponseDto"></int:channel>

<int:json-to-object-transformer input-channel="dms.put.ch.reply.transformer.DmsCommonResponseDto" output-channel="dms.put.ch.reply" type="blabla.DmsCommonResponseDto"></int:json-to-object-transformer>

    DmsCommonResponseDto.java 
private static final long serialVersionUID = 1L;

private String serverName;
private DmsErrorMessageDto errorMessage;
private Date responseDate;

...setter, getter method.

Any even little clue would be helpful too.任何一点点线索也会有所帮助。 Thanks in advance.提前致谢。

Sounds like you need a custom transformer downstream of the json transformer.听起来您需要在 json 变压器下游定制变压器。

class MyTransformer extends AbstractTransformer {

    protected abstract Object doTransform(Message<?> message) {
        DmsCommonResponseDto dto = (DmsCommonResponseDto) message.getPayload();
        dto.setRequestId(message.getHeaders.get("X-request-id"));
        ...
        return dto.
    }    

}

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

相关问题 春季集成:自定义标头扩展器,将标头值记录到数据库 - spring integration: Custom header enricher that records header values to database Spring Integration Http Outbound Gateway Header Mapper - Spring Integration Http Outbound Gateway Header Mapper 如何通过Spring Integration从RabbitMQ获取标头 - How to get header from RabbitMQ with spring integration Spring Integration:如何将自定义HTTP标头添加到HTTP出站网关? - Spring Integration: how to add custom HTTP headers to the HTTP outbound gateway? 如何为spring集成ftp网关接口添加自定义方法? - How to add custom method to spring integration ftp gateway interface? 如何使用header-filter删除spring集成中的头值? - How to remove the header values in spring integration using header-filter? Spring Integration不转发自定义标头? - Spring Integration not forwarding a custom header? 自定义spring集成出站网关实现 - custom spring integration outbound gateway implementation Spring Integration-HTTP出站网关自定义标头 - Spring Integration - http outbound gateway custom headers 如何使用 Spring 批量集成在 IntegrationFlow 中添加自定义 header? - How to add custom header in IntegrationFlow with Spring Batch Integration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM