简体   繁体   中英

Spring Integration: how to send a Byte Array as POST parameter with an http:outbound-gateway?

I want to send an Object instance serialized as a byte[] to an int-http:outbound-gateway as a HTTP POST parameter.

Here is my Spring integration configuration:

<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>

I have setted the contentType to "application/octet-stream" because I noticed that is the contentType indicated in the API documentation of ByteArrayHttpMessageConverter :

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/ByteArrayHttpMessageConverter.html

The POST request is correctly generated, but it fails with the error:

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]

Here is the full LOG of Spring, where there is the complete Error message:

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]

The request payload is builded as following:

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();
    }
}

Any idea to how to solve this issue?

You don't POST by adding ?payload={myObject} to the query string - the message payload is the POST body .

Add a transformer to the chain before the gateway to transform the body to a byte[] and it will work as desired; remove the uri variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM