2

I found that when I want to make a REST call using Spring-Integration, it automatically appends 'x' in case its a custom header.

For example in Spring-integration while sending custom request headers such as API-KEY, the actual request header name in the API call becomes X-API-KEY and so it fails.

It seems like Spring is standardizing by enforcing the Custom request headers to start with X, is there a work around?

<int:channel id="requestChannel"/>
<int:channel id="httpHeaderEnricherChannel"/>
<int-http:outbound-gateway request-channel="requestChannel"
                                       url="http://localhost:9090/balance"
                                       http-method="GET"
                                       mapped-request-headers="Api-Key"
                                       expected-response-type="java.lang.String"/>

<int:header-enricher input-channel="httpHeaderEnricherChannel" 
                     output-channel="requestChannel">
    <int:header name="Api-Key" value="pass"/>
</int:header-enricher>
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94

2 Answers2

3

You should declare DefaultHttpHeaderMapper.outboundMapper() bean with the setUserDefinedHeaderPrefix(null) and including that your custom Api-Key header mapping. After that you should replace mapped-request-headers attribute with the header-mapper reference.

We have revised the feature and decided to remove "X-" default prefix in the next version.

For more info please, see here Custom HTTP headers : naming conventions and here https://jira.spring.io/browse/INT-3903.

Community
  • 1
  • 1
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
0

Thanks to @Artem for clarifying, and Gary's post here Spring Integration Http Outbound Gateway Header Mapper

I was able to solve the issue

<int:channel id="requestChannel"/>
<int:gateway id="requestGateway" 
             service-interface="org.springframework.integration.samples.http.RequestGateway"
             default-request-channel="requestChannel">
            <int:default-header name="Api-Key" value="pass" />
</int:gateway>

<int-http:outbound-gateway request-channel="requestChannel"
                           header-mapper="headerMapper"
                           url="http://localhost:9090/balance"
                           http-method="GET"
                           expected-response-type="java.lang.String"/>

<beans:bean id="headerBean"
            class="org.springframework.integration.samples.http.HeaderBean" />

<bean id="headerMapper"
    class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="inboundHeaderNames" value="*" />
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, Api-Key" />
    <property name="userDefinedHeaderPrefix" value="" />
</bean>
Community
  • 1
  • 1
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94
  • That's great to hear that you overcame your issue, but it is sad do not see acceptance for the answer. I'd disable here on SO the "answer" functionality for requestors... – Artem Bilan Dec 07 '15 at 19:43
  • @Artem I can accept my answer after 2 days, if you think I should accept yours, will do that. Please let me know. Thanks! – Himalay Majumdar Dec 07 '15 at 20:06
  • @ArtemBilan In that case, please have a look here :). This question of mine has not been addressed for a while. http://stackoverflow.com/questions/34039632/rest-api-layer-orchestration-using-spring-integration – Himalay Majumdar Dec 08 '15 at 14:43