Search code examples
spring-integrationspring-webflux

How to change request method from POST to GET and extract from body request param using spring integration?


I have a running api which expects prop as parameter. The task that I would like to solve is to create a proxy using spring integration which accepts post request with body. For example:

POST http://localhost:8080/proxy
    {
       "prop":"someval"
    }

then change request method to GET, extract prop value, set it as request param and send it to downstream api as

http://localhost:9999/greet/param?prop=someval

get response from the api and send it back to the client.

Here is what has been done:

@Bean
public IntegrationFlow yourIntegrationFlow2() {
    return IntegrationFlow.from(WebFlux.inboundGateway("/proxy")
                    .requestMapping(mapping -> mapping.methods(HttpMethod.GET, HttpMethod.POST))
                    .requestPayloadType(Response.class)
            )
            .handle((GenericHandler<Response>) (payload, headers) -> {
                System.out.println(payload.getProp());
                return MessageBuilder.withPayload("").setHeader("queryParam",payload.getProp());
            })
            .handle(m->WebFlux.outboundGateway("http://localhost:9999/greet/param?{query}")
                    .uriVariable("query","prop="+m.getHeaders().get("queryParam"))
                    .httpMethod(HttpMethod.GET).charset("utf-8")
                    .mappedRequestHeaders("**")
                    .expectedResponseType(String.class))
            .get();
}

@Data
public class Response {
    private String prop;
}

The problem is when I send request to the proxy service using .http file, it is hanging

POST http://localhost:8080/proxy
Content-Type: application/json

{
  "prop": "val"
} 

Solution

  • The syntax .handle(m->WebFlux is wrong. You must not use a lambda variant, since you provide the whole handler via WebFlux.outboundGateway(). Plus plain handle(m ->) is a lambda for MessageHandler.handleMessage() which returns void therefore no any reply produced as it is expected by the inbound gateway in the beginning of your flow.

    The fix is just like this:

    .handle(WebFlux.outboundGateway("http://localhost:9999/greet/param?{query}")