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 thejsonPath
methodThen the implementation adds the
session
variable to the integration flow message header with theenrichHeaders
methodThen the implementation adds the
session
variable to the HTTP call header with theHeaderMapper
classMy 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?