问题描述
我正在尝试使用自定义标头调用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服务。