0

I have different flow methods for the POST, PUT, PATCH, and DELETE, like this

    private IntegrationFlow myChannelPost() {
      return f -> f
            .handle(Http.outboundGateway("url")
                    .uriVariable("url", m -> m.getHeaders().get("url"))
                    .httpMethod(HttpMethod.POST).mappedRequestHeaders("*")
                    .headerMapper(myHeaderMapper()).expectedResponseType(String.class))
           .route("nextChannel.input");
    }

    private IntegrationFlow myChannelPut() {
       return f -> f
            .handle(Http.outboundGateway("url")
                    .uriVariable("url", m -> m.getHeaders().get("url"))
                    .httpMethod(HttpMethod.PUT).mappedRequestHeaders("*")
                    .headerMapper(myHeaderMapper()).expectedResponseType(String.class))
           .route("nextChannel.input");
    }

    private IntegrationFlow myChannelPatch() {
      return f -> f
            .handle(Http.outboundGateway("url")
                    .uriVariable("url", m -> m.getHeaders().get("url"))
                    .httpMethod(HttpMethod.PATCH).mappedRequestHeaders("*")
                    .headerMapper(myHeaderMapper()).expectedResponseType(String.class))
           .route("nextChannel.input");
    }

    private IntegrationFlow myChannelDelete() {
      return f -> f
            .handle(Http.outboundGateway("url")
                    .uriVariable("url", m -> m.getHeaders().get("url"))
                    .httpMethod(HttpMethod.DELETE).mappedRequestHeaders("*")
                    .headerMapper(myHeaderMapper()).expectedResponseType(String.class))
           .route("nextChannel.input");
    }

They contain almost same code four times. Is it possible to have one method which handles dynamically those four cases?

Mike
  • 541
  • 1
  • 4
  • 18

1 Answers1

1

Add the method to a header and...

.httpMethodFunction(m -> m.getHeaders().get("httpMethod")

(or derive it from some SpEL expression with .httpMethodExpression(expression)).

Gary Russell
  • 166,535
  • 14
  • 146
  • 179