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

标签 java spring spring-integration

我想将序列化为 byte[] 的对象实例作为 HTTP POST 参数发送到 int-http:outbound-gateway

这是我的 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”,因为我注意到这是 ByteArrayHttpMessageConverter 的 API 文档中指示的 contentType >:

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

POST 请求已正确生成,但失败并出现错误:

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]

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

知道如何解决这个问题吗?

最佳答案

您不能通过将 ?payload={myObject} 添加到查询字符串来进行 POST - 消息负载是 POST 正文

在网关之前向链中添加一个转换器,将主体转换为 byte[],它将按需要工作;删除 uri 变量。

关于java - Spring 集成: how to send a Byte Array as POST parameter with an http:outbound-gateway?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38499618/

相关文章:

java - 使用 SWIG 将 char * 类型结构的成员转换为 Java 中的 java.lang.Object

java - Railo Java 打印

java - 无法使用 @Valid 在 Spring Boot 中验证请求正文

java - 抛出异常后如何处理 SFTP 入站适配器轮询中的剩余文件?

Java DTO 到 TypeScript

Java Graphics - 像 Zelle 的 Python Graphics 一样简单

java - 如何在spring-mvc中将参数传递给重定向页面

java - 如何在 CXF 客户端中动态添加 HTTP header ?

spring - 在 Spring JMS 集成中配置基于时间间隔的 cron

java - Headerenricher Spring Integration 和 java dsl