繁体   English   中英

Spring 集成 Http OutboundGateway PUT 与 header 参数

[英]Spring Integration Http OutboundGateway PUT with header parameters

我正在尝试在远程 REST 端点上进行 PUT,我需要为其提供凭据作为标头的一部分,但到目前为止没有成功。

方法一:

    @Bean
    public IntegrationFlow outboundGateway() {
        return flow -> flow
                .transform(transformer)
                .enrichHeaders(h -> h.header("x-api-key", "secret123")
                                     .header("contentType", MediaType.APPLICATION_JSON))
                .handle(Http.outboundGateway("https://remote-service.com/car")
                        .mappedRequestHeaders()
                .httpMethod(HttpMethod.PUT)
                .expectedResponseType(String.class))
                .log();
    }

我不断收到403 Forbidden。

我很容易用 RestTemplate 实现了同样的目标:

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(getHeaders());
restTemplate.put("https://remote-service.com/car", request);
...

    private HttpHeaders getHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("x-api-key", "secret123");
        return headers;
    }

如何使用 Http OutboundGateway 发送此 x-api-key header 及其值?

谢谢。

x-api-key不是标准的http header,所以需要让其进行传输:

 .mappedRequestHeaders(*)

对于您的Http.outboundGateway()

有关更多信息,请参阅文档: https://docs.spring.io/spring-integration/docs/5.2.6.RELEASE/reference/html/http.html#http-header-mapping

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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