如何在 Spring Integration Rest Service Call 中添加自定义 header in Http.outboundGateway?

How to add cutom header in Http.outboundGateway in Spring Integration Rest Service Call?

我正在尝试使用自定义 header 调用 Rest Web-service POST 方法。尝试在 enrichHeaders 中添加自定义 header 仍然收到 HTTP 403 Forbidden 响应。您能帮忙提供正确的代码片段吗?

.enrichHeaders(h -> h.header("X-API-Key","ABCEDABCED").header(CONTENT_TYPE, APPLICATION_JSON_VALUE).header(APP_NAME, XXX).header(ACCEPT,
                        APPLICATION_JSON_VALUE))
                .handle(Http.outboundGateway(config.getXxxWebServiceUrl()).httpMethod(HttpMethod.POST)
                        .expectedResponseType(String.class).requestFactory(xxxRequestFactory()),
                        c -> c.advice(sendToArchive.sendToArhive()))
                .log().get();

错误日志:-

[bean 'xxxDispatcher1.http:outbound-gateway#0' for component 'xxxDispatcher1.org.springframework.integration.config.ConsumerEndpointFactoryBean#2'; defined in: 'class path resource [service/xxxDispatcher.class]'; from source: 'bean method xxxDispatcher1']; nested exception is org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: [{"message":"Forbidden"}]
    at 

请参阅文档中的 Header 映射部分:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping

By default, all standard HTTP headers are mapped from the message to HTTP request or response headers without further configuration.

因此,由于您没有为 Http.outboundGateway() 提供 mappedRequestHeaders() 选项,因此您的 APP_NAME 自定义 header 未映射且未通过 HTTP 传输到REST 服务。