如何解决Spring Integration Rest Service调用中如何在Http.outboundGateway中添加cutom标头?
我正在尝试使用自定义标头调用Rest Web服务POST方法。试图在richHeaders中添加自定义标头,但仍获得HTTP 403禁止响应。您能帮忙提供正确的代码段吗?
.enrichHeaders(h -> h.header("X-API-Key","ABCEDABCED").header(CONTENT_TYPE,APPLICATION_JSON_VALUE).header(APP_NAME,MBP).header(ACCEPT,APPLICATION_JSON_VALUE))
.handle(Http.outboundGateway(config.getMbpWebServiceUrl()).httpMethod(HttpMethod.POST)
.expectedResponseType(String.class).requestFactory(mbpRequestFactory()),c -> c.advice(sendToArchive.sendToArhive()))
.log().get();
错误日志:-
[bean 'mbpDispatcher1.http:outbound-gateway#0' for component 'mbpDispatcher1.org.springframework.integration.config.ConsumerEndpointFactoryBean#2'; defined in: 'class path resource [service/MbpDispatcher.class]'; from source: 'bean method mbpDispatcher1']; nested exception is org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: [{"message":"Forbidden"}]
at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.exchange(HttpRequestExecutingMessageHandler.java:168)
at org.springframework.integration.http.outbound.AbstractHttpRequestExecutingMessageHandler.handleRequestMessage(AbstractHttpRequestExecutingMessageHandler.java:288)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler$AdvisedRequestHandler.handleRequestMessage(AbstractReplyProducingMessageHandler.java:201)
at sun.reflect.GeneratedMethodAccessor144.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice$CallbackImpl.execute(AbstractRequestHandlerAdvice.java:151)
at org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.doInvoke(ExpressionEvaluatingRequestHandlerAdvice.java:237)
at org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice.invoke(AbstractRequestHandlerAdvice.java:67)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy135.handleRequestMessage(Unknown Source)
解决了该问题的解决方案:-
.enrichHeaders(h -> h.header("X-API-Key","ABCEDF").header(CONTENT_TYPE,APPLICATION_JSON_VALUE))
.handle(Http.outboundGateway(config.getMbpWebServiceUrl()).httpMethod(HttpMethod.POST)
.mappedRequestHeaders("X-API-Key")
.expectedResponseType(String.class).requestFactory(mbpRequestFactory()),c -> c.advice(sendToArchive.sendToArhive()))
.log().get();
解决方法
请参阅文档中的“页眉映射”部分:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-header-mapping
默认情况下,所有标准HTTP标头都从消息映射到HTTP请求或响应标头,而无需进一步配置。
因此,由于您没有为mappedRequestHeaders()
提供Http.outboundGateway()
选项,因此APP_NAME
自定义标头不会被映射,也不会通过HTTP传输到REST服务。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。