简体   繁体   中英

Spring integration Java DSL: How to choose the HttpMethod dynamically with the Http.outboundGateway method

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?

Add the method to a header and...

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM