0

MyGateway(defined in service-interface) is a Interface will contain one request body(JsonFormat) which I will be passing it in http-outbound-gateway for rest call

<!-- Gateway -->
    <int:gateway id="requestGateway" 
                 service-interface="com.spring.example.MyGateway"
                 default-request-channel="requestChannel12345"/>
    
    <int:channel id="requestChannel12345"/>
    

    
<bean id="methodInsideService"    class="com.spring.example.ServiceImpl"/>

Using below Service-Activator I created one model class which will contains values of headers so that I can use dynamic headers while doing a rest call ---> below

<int:service-activator input-channel="requestChannel12345"
output-channel="responseChannel1234567" 
ref="methodInsideService" 
method="methodExample"/>

<bean id="clientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"/>

Here I am accepting request channel(responseChannel1234567) as outpt-channel(responseChannel1234567) of dynamic headers response ---> here my actual json request overwritten with headers payload which I am creating to use it as dynamic header values

<int:chain input-channel="responseChannel1234567" >
     <int:header-enricher>
     <int:header name="header1"  expression="payload.getheader1()" ></int:header>
     <int:header name="header2" expression="payload.getheader2()" ></int:header>
     <int:header name="header3" expression="payload.getheader3()" ></int:header>
     
     </int:header-enricher>
     <int-http:outbound-gateway 
     id="requestGateway" 
     url="http://localhost/test?queryParam1={queryParam1}&amp;queryParam2={queryParam2}&amp;queryParam3={queryParam3}"
     http-method="POST"
     request-factory="clientHttpRequestFactory" 
     header-mapper="headerMapper12345"
     expected-response-type="java.lang.String">
     <int-http:uri-variable name="queryParam1" expression="payload.getqueryParam1()"/>
     <int-http:uri-variable name="queryParam2" expression="payload.getqueryParam2()"/>
     <int-http:uri-variable name="queryParam3" expression="payload.getqueryParam3()"/>
     </int-http:outbound-gateway>
     
     </int:chain>
    <bean id="headerMapper12345" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
     <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, header1,header2,header3" />
     <property name="userDefinedHeaderPrefix" value="" /></bean>
     
    
     
</beans>

Now My Question is how to pass multiple headers(dynamic will pass at code level),multiple query-params(dynamic at code level) and request body(JSON Format) to make a rest call to other services. Request body we can pass from Interface, but how to pass the headers and query params from interface or using service-activator and then configure xml accordingly to make a rest http outbound gateway call to other rest endpoint. Thanks in advance

1 Answers1

0

The question is not clear and requires some details to reveal what exactly are you doing and what are your expectations.

The request body is mapped from the message payload by default. The underlying RestTemplate is going to use its HttpMessageConverters to serialize such a body to respective data presentation over the network.

The headers are mapped from the message to the HTTP request according these rules:

https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping

So, if you have some set of headers before sending to this HTTP channel adapter, you can specify their names in the mapped-request-headers. Or just use * to map all the headers.

The <header-enricher> can be configured with the ref and method to populate something dynamic map of headers:

    <xsd:attribute name="ref" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>
                Reference to an Object to be invoked for header values.
                The 'method' attribute is required
                along
                with this.
            </xsd:documentation>
            <xsd:appinfo>
                <tool:annotation kind="ref"/>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="method" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>
                Method to be invoked on the referenced Object as specified by the
                'ref' attribute. The method
                should return a Map with String-typed keys.
            </xsd:documentation>
            <xsd:appinfo>
                <tool:annotation>
                    <tool:expected-method type-ref="@ref"/>
                </tool:annotation>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>

https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#header-enricher

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118