2016-07-21 5 views
0

Ich möchte eine Objektinstanz, die als byte[] serialisiert wurde, an einen int-http:outbound-gateway als HTTP-POST-Parameter senden.Spring Integration: Wie ein Byte-Array als POST-Parameter mit einem http: Outbound-Gateway zu senden?

Hier ist meine Frühling Integration Konfiguration:

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

ich die contentType-"application/octet-stream" gefassten, weil ich das ist die content angegeben in der API-Dokumentation von ByteArrayHttpMessageConverter bemerkt:

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

Die POST-Anforderung wird korrekt generiert, schlägt jedoch mit dem Fehler fehl:

Hier ist die vollständige LOG des Frühlings, wo es die komplette Fehlermeldung:

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] 

Die Anfrage Nutzlast builded wie folgt:

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

Jede Idee, wie dieses Problem zu lösen Problem?

Antwort

1

Sie werden nicht POST, indem Sie ?payload={myObject} der Abfragezeichenfolge hinzufügen - die Nachrichtennutzlast ist der POST-Nachrichtentext.

Fügen Sie der Kette vor dem Gateway einen Transformator hinzu, um den Körper in eine byte[] umzuwandeln, und es funktioniert wie gewünscht; entferne die uri-Variable.

+0

Danke, Gary, das ist die richtige Lösung und es funktioniert perfekt. –

+0

@Gary, könntest du mir bitte sagen, wenn wir einen Objekttyp an 'http: outbound-gateway' übergeben müssen, sollte das in Payload sein. Ritus?? In meinem Fall erwartet meine POST-API Eingaben in '@ RequestBody'. – Jaikrat

+0

Es ist nicht angemessen, auf eine Frage zu pendeln; Sie sollten eine neue Frage stellen. Aber ja, die Nachrichtennutzlast wird zum Körper. –

Verwandte Themen