如何在Spring Integration Rest Service Call的Http.outboundGateway



我正在尝试使用自定义标头进行Rest Web服务POST方法调用。尝试在enrichHeaders中添加自定义标头,但仍得到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 mappings部分:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-标头映射

默认情况下,所有标准HTTP标头都会从消息映射到HTTP请求或响应标头,而无需进一步配置。

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

最新更新