带有效负载的Http Outbound Gateway Post

时间:2018-04-30 21:11:02

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

我正在使用集成流来发送POST请求。我想知道如何指定请求体?

IntegrationFlow myFlow() {
    return IntegrationFlows.from("requestChannel")
            .handle(Http.outboundGateway(uri)
                    .httpMethod(HttpMethod.POST)
                    .expectedResponseType(String.class)
            )
            .handle("myAssembler", "myFonction")
            .channel("responseChannel")
            .get();
}

1 个答案:

答案 0 :(得分:1)

此情况下的请求正文是根据请求消息payload确定的:

private HttpEntity<?> generateHttpRequest(Message<?> message, HttpMethod httpMethod) {
    Assert.notNull(message, "message must not be null");
    return (this.extractPayload) ? this.createHttpEntityFromPayload(message, httpMethod)
            : this.createHttpEntityFromMessage(message, httpMethod);
}

createHttpEntityFromPayload()的含义如下:

private HttpEntity<?> createHttpEntityFromPayload(Message<?> message, HttpMethod httpMethod) {
    Object payload = message.getPayload();
    if (payload instanceof HttpEntity<?>) {
        // payload is already an HttpEntity, just return it as-is
        return (HttpEntity<?>) payload;
    }
    HttpHeaders httpHeaders = this.mapHeaders(message);
    if (!shouldIncludeRequestBody(httpMethod)) {
        return new HttpEntity<>(httpHeaders);
    }
    // otherwise, we are creating a request with a body and need to deal with the content-type header as well
    if (httpHeaders.getContentType() == null) {
        MediaType contentType = (payload instanceof String)
                ? resolveContentType((String) payload, this.charset)
                : resolveContentType(payload);
        httpHeaders.setContentType(contentType);
    }
    if (MediaType.APPLICATION_FORM_URLENCODED.equals(httpHeaders.getContentType()) ||
            MediaType.MULTIPART_FORM_DATA.equals(httpHeaders.getContentType())) {
        if (!(payload instanceof MultiValueMap)) {
            payload = this.convertToMultiValueMap((Map<?, ?>) payload);
        }
    }
    return new HttpEntity<>(payload, httpHeaders);
}

这应该会给你一些想法,你可以向payload发送什么样的requestChannel