【问题标题】:Spring Integration: http converter issue using application/octet-stream content typeSpring Integration:使用 application/octet-stream 内容类型的 http 转换器问题
【发布时间】:2019-04-08 22:31:31
【问题描述】:

我有这个流程:

@Bean
public IntegrationFlow flow(){
    return IntegrationFlows.from(QueueMessageSink.INPUT)
        .transform(Transformers.fromJson(QueueMessage.class))
        .<QueueMessage, DTOMessage> transform(queueMessage -> new DTOMessage(/* transform logic */))
        .handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST))
        .channel("nullChannel")
        .get();
}

基本上,我正在从队列中读取消息并将其发布到 REST 端点。

队列中的消息以 Content-type application/octet-stream 到达,因此我已按以下方式配置队列:

spring.cloud.stream.bindings.my-queue.contentType=application/octet-stream

消息已正确地从 JSON 转换为 QueueMessage 以及从 QueueMessage 转换为 DTOMessage,但在尝试发布 DTOMessage 时出现以下异常:

Caused by: org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI; nested exception is org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.example.DTOMessage] and content type [application/octet-stream]

我认为(也许我错了)Http.outboundGateway 正在从 Spring 集成消息的标头中读取内容类型 application/octet-stream。

解决此问题的正确方法是什么?我想一个可能的解决方案是将内容类型更改为 application/json,但我不确定这是否是最简单的方法(我也不知道该怎么做)。

提前致谢。

【问题讨论】:

    标签: spring spring-integration spring-cloud-stream


    【解决方案1】:

    我想一个可能的解决方案是将内容类型更改为 application/json

    这确实是一个正确的方法,但你不应该在bindings 上这样做,而是在你的流程中这样做。我认为这样的事情应该对你有用:

    .enrichHeaders(h -> h
                    .defaultOverwrite(true)
                    .header(MessageHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
    .handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST))
    

    【讨论】:

    • 这似乎可行,谢谢。我有另一个问题。从 JSON 到 QueueMessage 的转换是否正确?我的意思是,在enrichHeaders 步骤之前,我收到一条不一致的消息,其中有效负载是Java 对象,内容类型是应用程序/八位字节,我必须手动覆盖它,这对我来说听起来很奇怪。
    • 因为您将 from JSON to POJO 转换,反之亦然。这不是因为您将 JSON 转换为 QueueMessage,然后将其转换为 DTOMessage,因此,看起来我们不能直接将您的字节传递给 Http.outboundGateway(),假设它是 JSON。无论如何,您似乎必须根据您的 HTTP 要求覆盖它...
    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 2020-08-01
    • 2019-08-01
    • 2019-10-16
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多