技术 我正在使用 Spring Boot 2.x、Spring Integration 5.x。
我正在尝试使用 http-outbound 网关 (POST) 发送表单参数。我的弹簧集成文件看起来像
<int-http:inbound-gateway id="lpaGateway"
request-channel="myGatewayRequests"
supported-methods="POST"
path="/test"
request-payload-type="java.lang.String"
reply-timeout="180000"
reply-channel="myGatewayResponses"
error-channel="httpErrorChannel">
<int-http:request-mapping consumes="application/xml" produces="application/xml"/>
</int-http:inbound-gateway>
<!-- Transformer to generate OAuth request payload -->
<int:transformer input-channel="oAuthRequestGeneration"
ref="myTransformer"
method="oAuthRequestPayloadGenerator"
output-channel="oAuthRequests"/>
<int:chain input-channel="oAuthRequests" output-channel="oAuthResponses">
<int:header-enricher>
<int:header name="Content-Type" value="application/x-www-form-urlencoded"/>
</int:header-enricher>
<int:transformer expression="payload.getoAuthRequestCredentialsMap()"/>
<int-http:outbound-gateway id="lpaPostGateway"
url="${oauth_endpoint}"
http-method="POST"
extract-request-payload="true"
expected-response-type="java.lang.String"
header-mapper="headerMapper">
<!-- <int-http:uri-variable name="request" expression="payload"/> -->
</int-http:outbound-gateway>
</int:chain>
在我的转换器中,我正在创建一个表单参数映射,如下所示,并将该映射设置到我的有效负载中。
public MultiValueMap<String, String> oAuthRequestCredentialsMap(){
MultiValueMap<String, String> oAuthRequestCredentialsMap = new LinkedMultiValueMap<>();
oAuthRequestCredentialsMap.add("username", username);
oAuthRequestCredentialsMap.add("password", password);
return oAuthRequestCredentialsMap;
}
当点击 POST URL 时,我收到以下异常。
org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [myUrl]; nested exception is org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized - Bad Token, failedMessage=GenericMessage [payload={username=[username], password=[password]}, headers={content-length=93387, http_requestMethod=POST, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@14b2c481, cookie=JSESSIONID=633EAA21C082C31AD527CBA0258A7532, pollingFrequency=0, accept=*/*, authorization=Basic aW50Z0xha2V3b29kRnJlZGRpZU1hYzpVdG02dTVLdFQ1c2VYeHBO, replyChannel=org.springframework.messaging.core
- 据我了解,客户端无法读取我作为地图发送的表单参数,返回 401 - Unauthorized - Bad token。我已经检查了所有凭据,它们没有任何问题。只是客户无法阅读它们。
- 我得到的响应是json,无论是成功还是失败。当我的错误通道中出现异常时,我正在尝试读取正文,但它返回 null。这就是我试图阅读异常的方式。
消息异常消息异常 = 消息异常消息.getPayload(); HttpStatusCodeException httpStatusCodeException = (HttpStatusCodeException) messingException.getCause(); 字符串 httpResponseBody = httpStatusCodeException.getResponseBodyAsString();