spring-integrationcustom-headersspring-integration-http

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.

The response has custom header values start with 'X-'. The X-Values are two. (X-AccessToken-Quota-Allotted, X-AccessToken-Quota-Current)

So, I added inboundHeaderNames in header-mapper in config xml as below.

    <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.

[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}]

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.

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.


Solution

  • Sounds like you need a custom transformer downstream of the json transformer.

    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.
        }    
    
    }