如何解决Spring Integration HTTP Outbound Gateway标头未在连续请求时转发
我在以下流程中苦苦挣扎:
.enrichHeaders(h -> h.headerFunction("ocp-apim-subscription-key",m ->
"xxx"))
.handle(Http.outboundGateway("https://northeurope.api.cognitive.microsoft.com/vision/v3" +
".0/read/analyzeResults/abc")
.mappedRequestHeaders("ocp-apim-subscription-key")
.httpMethod(HttpMethod.GET))
.enrichHeaders(h -> h.headerFunction("ocp-apim-subscription-key",m ->
"xxx"))
.handle(Http.outboundGateway("https://northeurope.api.cognitive.microsoft.com/vision/v3" +
".0/read/analyzeResults/def")
.mappedRequestHeaders("ocp-apim-subscription-key")
.httpMethod(HttpMethod.GET))
第一个请求已正确提交,我得到了结果,第二个请求得到了 401 UNAUTHORIZED ,这意味着 ocp-apim-subscription-key 不是包括在内。我尝试过没有第二个富集步骤,因为我认为不会清除标头,但它也没有任何改变。
知道我可能做错了什么吗?我是否需要以其他方式配置标头映射器?
以下是调试的输出,该输出清楚地表明包含了标头:
17:45:31.468 [main] DEBUG org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler - bean 'ocrDocument.http:outbound-gateway#2' for component 'ocrDocument.org.springframework.integration.config.ConsumerEndpointFactoryBean#3'; defined in: 'processing.OCRIntegrationFlow'; from source: 'bean method ocrDocument' received message: GenericMessage [payload=<200,[Transfer-Encoding:"chunked",Content-Type:"application/json; charset=utf-8",x-envoy-upstream-service-time:"25",CSP-Billing-Usage:"CognitiveServices.ComputerVision.Transaction=1",Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload",x-content-type-options:"nosniff",Date:"Mon,31 Aug 2020 15:45:31 GMT"]>,headers={Transfer-Encoding=chunked,ocp-apim-subscription-key=xxx,id=11fa4a77-d97a-772b-69b6-059de29ef808,contentType=application/json;charset=utf-8,http_statusCode=200 OK,Date=1598888731000,timestamp=1598888731467}]
更新 我已经记录了wireshark的会话(由于无法正常工作,切换到了http而不是https)。似乎在第二个请求中,订阅密钥没有传播。由于某种原因,第二个中包含了更多标题。
第一个
第二个
解决方法
好。我知道问题出在哪里:
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 = mapHeaders(message);
由于您将ResponseEntity
从第一个调用传播到第二个调用,因此确实没有任何标头映射,因为我们只是在AbstractHttpRequestExecutingMessageHandler
中不执行该逻辑,而是使用提供的{{ 1}}。
我们无法假设您要怎么做,但是由于您已提供了整个实体,因此我们只是不对其进行突变并按原样对它执行请求。
为解决此问题,我建议在第二次调用之前添加一些简单的HttpEntity
以避免某些HTTP实体假设。
是的,如果.transform((p) -> "")
的值是相同的,则不需要第二个标头增强器。
我们可能需要改进此文档,并说明如何在此组件中处理请求消息。随时提出GH问题!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。