Http Outbound Gateway Post с полезной нагрузкой

Я использую поток интеграции для отправки запроса POST. Я хотел бы знать, как я могу указать тело запроса?

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

person Hippolyte Fayol    schedule 30.04.2018    source источник


Ответы (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.

person Artem Bilan    schedule 30.04.2018