Spring Integration: как отправить байтовый массив как параметр POST с помощью http:outbound-gateway?
Я хочу отправить экземпляр объекта, сериализованный как byte[]
для int-http:outbound-gateway
в качестве параметра HTTP POST.
Вот моя конфигурация интеграции Spring:
<int:chain input-channel="requestChannel" output-channel="replyChannel">
<int:header-enricher>
<int:header name="contentType" value="application/octet-stream" overwrite="true"/>
</int:header-enricher>
<int-http:outbound-gateway id="gateway"
encode-uri="true" url="http://localhost:8080/myApp?payload={myObject}"
http-method="POST" header-mapper="headerMapper" extract-request-payload="true">
<int-http:uri-variable name="myObject" expression="payload.getSerializedObject()"/>
</int-http:outbound-gateway>
</int:chain>
Я установил contentType
в "application/octet-stream"
потому что я заметил, что это contentType, указанный в документации API ByteArrayHttpMessageConverter
:
Запрос POST генерируется правильно, но он завершается ошибкой:
org.springframework.web.client.RestClientException: не удалось записать запрос: не найден подходящий HttpMessageConverter для типа запроса [test.app.RequestPayload] и типа контента [application/octet-stream]
Вот полный журнал Spring, где есть полное сообщение об ошибке:
2016-07-21 10:30:37 DEBUG org.springframework.http.client.support.HttpAccessor.createRequest:79 (executor-3) -
Created POST request for "http://localhost:8080/myApp?payload=-84,-19,0,5,115,114,0,16,116,101,115,116,46,67,111,110,116,101,110,105,116,111,114,101,-80,-61,4,81,-17,-9,122,-124,2,0,2,76,0,4,110,111,109,101,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,76,0,4,116,105,112,111,116,0,11,76,116,101,115,116,47,84,105,112,111,59,120,112,116,0,11,99,111,110,116,101,110,105,116,111,114,101,115,114,0,9,116,101,115,116,46,84,105,112,111,-85,110,71,71,--84,-19,0,5,115,114,0,16,116,101,115,116,46,67,111,110,116,101,110,105,116,111,114,101,-80,-61,4,81,-17,-9,122,-124,2,0,2,76,0,4,110,111,109,101,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,76,0,4,116,105,112,111,116,0,11,76,116,101,115,116,47,84,105,112,111,59,120,112,116,0,11,99,111,110,116,101,110,105,116,111,114,101,115,114,0,9,116,101,115,116,46,84,105,112,111,-85,110,71,71,-3,12,58,41,2,0,2,76,0,9,109,97,116,101,114,105,97,108,101,113,0,126,0,1,76,0,4,110,111,109,101,113,0,126,0,1,120,112,116,0,9,109,97,116,101,114,105,97,108,101,116,0,4,116,105,112,1118,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,120,112,116,0,14,82,79,76,69,95,65,78,79,78,89,77,79,85,83,120,113,0,126,0,10,115,114,0,72,111,114,103,46,115,112,114,105,110,103,102,114,97,109,101,119,111,114,107,46,115,101,99,117,114,105,116,121,46,119,101,98,46,97,117,116,104,101,110,116,105,99,97,116,105,111,110,46,87,101,98,65,117,116,104,101,110,116,105,99,97,116,105,111,110,68,101,116,97,105,108,115,0,0,0,0,0,0,1,-102,2,0,2,76,0,13,114,101,109,111,116,101,65,100,100,114,101,115,115,113,0,126,0,12,76,0,9,115,101,115,115,105,111,110,73,100,113,0,126,0,12,120,112,116,0,15,48,58,48,58,48,58,48,58,48,58,48,58,48,58,49,112,118,91,63,-111,116,0,13,97,110,111,110,121,109,111,117,115,85,115,101,114"
2016-07-21 10:30:37 DEBUG org.springframework.integration.channel.AbstractMessageChannel.send:411 (executor-3) -
preSend on channel 'errorChannel', message: ErrorMessage [payload=org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://localhost:8080/myApp?payload=-84,-19,0,5,115,114,0,16,116,101,115,116,46,67,111,110,116,101,110,105,116,111,114,101,-80,-61,4,81,-17,-9,122,-124,2,0,2,76,0,4,110,111,109,101,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,76,0,4,116,105,112,111,116,0,11,76,116,101,115,116,47,84,105,112,111,59,120,112,116,0,11,99,111,110,116,101,110,105,116,111,114,101,115,114,0,9,116,101,115,116,46,84,105,112,111,-85,110,71,71,--84,-19,0,5,115,114,0,16,116,101,115,116,46,67,111,110,116,101,110,105,116,111,114,101,-80,-61,4,81,-17,-9,122,-124,2,0,2,76,0,4,110,111,109,101,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,76,0,4,116,105,112,111,116,0,11,76,116,101,115,116,47,84,105,112,111,59,120,112,116,0,11,99,111,110,116,101,110,105,116,111,114,101,115,114,0,9,116,101,115,116,46,84,105,112,111,-85,110,71,71,-3,12,58,41,2,0,2,76,0,9,109,97,116,101,114,105,97,108,101,113,0,126,0,1,76,0,4,110,111,109,101,113,0,126,0,1,120,112,116,0,9,109,97,116,101,114,105,97,108,101,116,0,4,116,105,112,1118,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,120,112,116,0,14,82,79,76,69,95,65,78,79,78,89,77,79,85,83,120,113,0,126,0,10,115,114,0,72,111,114,103,46,115,112,114,105,110,103,102,114,97,109,101,119,111,114,107,46,115,101,99,117,114,105,116,121,46,119,101,98,46,97,117,116,104,101,110,116,105,99,97,116,105,111,110,46,87,101,98,65,117,116,104,101,110,116,105,99,97,116,105,111,110,68,101,116,97,105,108,115,0,0,0,0,0,0,1,-102,2,0,2,76,0,13,114,101,109,111,116,101,65,100,100,114,101,115,115,113,0,126,0,12,76,0,9,115,101,115,115,105,111,110,73,100,113,0,126,0,12,120,112,116,0,15,48,58,48,58,48,58,48,58,48,58,48,58,48,58,49,112,118,91,63,-111,116,0,13,97,110,111,110,121,109,111,117,115,85,115,101,114]; nested exception is org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [test.app.RequestPayload] and content type [application/octet-stream], headers={id=0b397968-8a99-2d59-8f9e-636bd172c3d1, timestamp=1469089837071}]
2016-07-21 10:30:37 DEBUG org.springframework.integration.handler.AbstractMessageHandler.handleMessage:115 (executor-3) -
_org.springframework.integration.errorLogger.handler received message: ErrorMessage [payload=org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://localhost:8080/myApp?payload=-84,-19,0,5,115,114,0,16,116,101,115,116,46,67,111,110,116,101,110,105,116,111,114,101,-80,-61,4,81,-17,-9,122,-124,2,0,2,76,0,4,110,111,109,101,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,76,0,4,116,105,112,111,116,0,11,76,116,101,115,116,47,84,105,112,111,59,120,112,116,0,11,99,111,110,116,101,110,105,116,111,114,101,115,114,0,9,116,101,115,116,46,84,105,112,111,-85,110,71,71,--84,-19,0,5,115,114,0,16,116,101,115,116,46,67,111,110,116,101,110,105,116,111,114,101,-80,-61,4,81,-17,-9,122,-124,2,0,2,76,0,4,110,111,109,101,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,76,0,4,116,105,112,111,116,0,11,76,116,101,115,116,47,84,105,112,111,59,120,112,116,0,11,99,111,110,116,101,110,105,116,111,114,101,115,114,0,9,116,101,115,116,46,84,105,112,111,-85,110,71,71,-3,12,58,41,2,0,2,76,0,9,109,97,116,101,114,105,97,108,101,113,0,126,0,1,76,0,4,110,111,109,101,113,0,126,0,1,120,112,116,0,9,109,97,116,101,114,105,97,108,101,116,0,4,116,105,112,1118,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,120,112,116,0,14,82,79,76,69,95,65,78,79,78,89,77,79,85,83,120,113,0,126,0,10,115,114,0,72,111,114,103,46,115,112,114,105,110,103,102,114,97,109,101,119,111,114,107,46,115,101,99,117,114,105,116,121,46,119,101,98,46,97,117,116,104,101,110,116,105,99,97,116,105,111,110,46,87,101,98,65,117,116,104,101,110,116,105,99,97,116,105,111,110,68,101,116,97,105,108,115,0,0,0,0,0,0,1,-102,2,0,2,76,0,13,114,101,109,111,116,101,65,100,100,114,101,115,115,113,0,126,0,12,76,0,9,115,101,115,115,105,111,110,73,100,113,0,126,0,12,120,112,116,0,15,48,58,48,58,48,58,48,58,48,58,48,58,48,58,49,112,118,91,63,-111,116,0,13,97,110,111,110,121,109,111,117,115,85,115,101,114]; nested exception is org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [test.app.RequestPayload] and content type [application/octet-stream], headers={id=0b397968-8a99-2d59-8f9e-636bd172c3d1, timestamp=1469089837071}]
2016-07-21 10:30:37 ERROR org.springframework.integration.handler.LoggingHandler.handleMessageInternal:184 (executor-3) -
org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://localhost:8080/myApp?payload=-84,-19,0,5,115,114,0,16,116,101,115,116,46,67,111,110,116,101,110,105,116,111,114,101,-80,-61,4,81,-17,-9,122,-124,2,0,2,76,0,4,110,111,109,101,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,76,0,4,116,105,112,111,116,0,11,76,116,101,115,116,47,84,105,112,111,59,120,112,116,0,11,99,111,110,116,101,110,105,116,111,114,101,115,114,0,9,116,101,115,116,46,84,105,112,111,-85,110,71,71,--84,-19,0,5,115,114,0,16,116,101,115,116,46,67,111,110,116,101,110,105,116,111,114,101,-80,-61,4,81,-17,-9,122,-124,2,0,2,76,0,4,110,111,109,101,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,76,0,4,116,105,112,111,116,0,11,76,116,101,115,116,47,84,105,112,111,59,120,112,116,0,11,99,111,110,116,101,110,105,116,111,114,101,115,114,0,9,116,101,115,116,46,84,105,112,111,-85,110,71,71,-3,12,58,41,2,0,2,76,0,9,109,97,116,101,114,105,97,108,101,113,0,126,0,1,76,0,4,110,111,109,101,113,0,126,0,1,120,112,116,0,9,109,97,116,101,114,105,97,108,101,116,0,4,116,105,112,1118,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,120,112,116,0,14,82,79,76,69,95,65,78,79,78,89,77,79,85,83,120,113,0,126,0,10,115,114,0,72,111,114,103,46,115,112,114,105,110,103,102,114,97,109,101,119,111,114,107,46,115,101,99,117,114,105,116,121,46,119,101,98,46,97,117,116,104,101,110,116,105,99,97,116,105,111,110,46,87,101,98,65,117,116,104,101,110,116,105,99,97,116,105,111,110,68,101,116,97,105,108,115,0,0,0,0,0,0,1,-102,2,0,2,76,0,13,114,101,109,111,116,101,65,100,100,114,101,115,115,113,0,126,0,12,76,0,9,115,101,115,115,105,111,110,73,100,113,0,126,0,12,120,112,116,0,15,48,58,48,58,48,58,48,58,48,58,48,58,48,58,49,112,118,91,63,-111,116,0,13,97,110,111,110,121,109,111,117,115,85,115,101,114]; nested exception is org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [test.app.RequestPayload] and content type [application/octet-stream]
Полезная нагрузка запроса строится следующим образом:
public void sendMessage(Object myObject) throws Exception {
MessagingChannel messagingChannel = (MessagingChannel)appContext.getBean("requestChannelBean");
RequestPayload payload = new RequestPayload(serialize(myObject));
Message<RequestPayload> message = MessageBuilder.withPayload(payload).build();
MessageChannel requestChannel = messagingChannel.getRequestChannel();
requestChannel.send(message);
}
public byte[] serialize(Object obj) throws IOException {
try(ByteArrayOutputStream b = new ByteArrayOutputStream()){
try(ObjectOutputStream o = new ObjectOutputStream(b)){
o.writeObject(obj);
}
return b.toByteArray();
}
}
Есть идеи, как решить эту проблему?
1 ответ
Вы не POST, добавив ?payload={myObject}
к строке запроса - полезная нагрузка сообщения является телом POST.
Добавьте трансформатор в цепь перед шлюзом, чтобы преобразовать тело в byte[]
и это будет работать по желанию; удалите переменную uri.