What is the easiest way to add the HTTP.outboundGateway
header in my program?
What I want to do is:
I first do the HTTP GET
for the URL
http://localhost:8050/session
then I get the JSON
{
"session": "session8050"
}
I extract the value of the session
variable and add that to the next HTTP GET
as the session
header variable.
Currently I have working code, but I was thinking could I do this easier? My implementation
Extracts the session
variable from the JSON with the jsonPath
method
Then the implementation adds the session
variable to the integration flow message header with the enrichHeaders
method
Then the implementation adds the session
variable to the HTTP call header with the HeaderMapper
class
My implementation is
integrationFlowBuilder
.transform(p -> authenticationJson)
.enrichHeaders(h -> h.header("Content-Type", "application/json"))
.handle(Http.outboundGateway("http://localhost:8050/session").httpMethod(HttpMethod.POST)
.expectedResponseType(String.class))
.enrichHeaders(
h -> h.headerExpression("session", "#jsonPath(payload, '$.session')", true)
.handle(Http
.outboundGateway(completeFromUrl)
.httpMethod(HttpMethod.GET).mappedRequestHeaders("session").headerMapper(headerMapper())
.expectedResponseType(String.class))
My headerMapper
is
@Bean
HeaderMapper headerMapper() {
final DefaultHttpHeaderMapper headerMapper = new DefaultHttpHeaderMapper();
final String[] headerNames = { "session" };
headerMapper.setOutboundHeaderNames(headerNames);
headerMapper.setUserDefinedHeaderPrefix("");
return headerMapper;
}
Is it possible to extract the session variable from the JSON and add it straight to the HTTP headers??
Why the HeaderMapper
must be used? Why the integration flow message headers don't go straight to the HTTP.outboundGateway
call as the payload goes?
First of all you need to understand that main goal of Spring Integration as any other EIP solution is to make components in the flow as isolated as possible, so in the future you can add some intermediate steps or remove without big impact for the whole solution and other components in your integration flow. This should be an answer to your questions about why HeaderMapper
must be used.
As you see the contract of the HeaderMapper
to remap MessageHeaders
to the target protocol headers representation. There is nothing about payload
, hence you need to map the value from the payload
into the headers, first of all. And then say Http.outboundGateway()
what should be remapped from the MessageHeaders
into the HttpHeaders
.
By default the DefaultHttpHeaderMapper
(it is present there in the Http.outboundGateway()
) maps only standard HTTP headers suitable for the HTTP request.
If you need to include some custom header, like in your case with that session
, you really can use a custom configuration for the DefaultHttpHeaderMapper
, or just configure a convenient option on the Http.outboundGateway()
:
.mappedRequestHeaders("session")
The setUserDefinedHeaderPrefix("")
is not necessary from version 5.0
. It is empty string by default now, since there is no requirements in the prefixes for custom headers in the HTTP protocol.