【问题标题】:How to send HTTP-Request from my IntegrationFlow?如何从我的 IntegrationFlow 发送 HTTP 请求?
【发布时间】:2019-07-05 08:52:27
【问题描述】:

我收到来自客户端的请求,该请求返回具有 HttpMethod、路径和要发送的数据的 SendRequest-Object。 现在我想根据获取到 API 的对象发送请求。

发送后我会收到回复。

现在的问题是如何发送有效载荷并接收响应。

@Bean
    public IntegrationFlow httpPostSendRawData() {
        return IntegrationFlows.from(
                Http.inboundGateway("/api/data/send")
                        .requestMapping(r -> r.methods(HttpMethod.POST))
                        .statusCodeExpression(dataParser().parseExpression("T(org.springframework.http.HttpStatus).BAD_REQUEST"))
                        .requestPayloadType(ResolvableType.forClass(DataSend.class))
                        .crossOrigin(cors -> cors.origin("*"))
                        .headerMapper(dataHeaderMapper())
        )
                .channel("http.data.send.channel")
                .handle("rawDataEndpoint", "send")
                .transform(/* here i have some transformations*/)
                .handle(Http.outboundGateway((Message<SendRequest> r)->r.getPayload().getPath())
                        .httpMethod(/* Here I would like to get the Method of SendRequest*/)
                        //add payload
                        .extractPayload(true))
.get();

【问题讨论】:

    标签: java spring spring-integration spring-integration-dsl


    【解决方案1】:

    不清楚您的SendRequest 是什么,但method 的想法与您为url 所做的完全一样:

    .httpMethodFunction((Message<SendRequest> r)->r.getPayload().getMethod())
    

    虽然,由于您希望对请求正文进行一些提取,因此您需要提前在 transform() 中执行此操作,并将 url 和 method 的值移动到标头。 Http.outboundGateway 中没有任何有效负载提取:它将整个请求有效负载作为 HTTP 请求正文的实体处理。

    【讨论】:

    • 是否可以让 expectedResponseType 通用?因为不同的 Path 可能会发送不同的响应数据。它首先使用 responseType String.class
    • 请阅读 API 及其 JavaDocs。有一个类似的expectedResponseTypeFunction() 可以根据请求消息确定类型。
    猜你喜欢
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多