Skip to content

Mqttv5PahoMessageHandler fails due to incorrect MessageConverter.fromMessage usage #11011

@d0v0c

Description

@d0v0c

In what version(s) of Spring Integration are you seeing this issue?

7.0.4

Describe the bug

This is the outbound mirror of Issue #10990.

Mqttv5PahoMessageHandler#buildMqttMessage calls MessageConverter.fromMessage(message, byte[].class) at
Mqttv5PahoMessageHandler.java:225 to serialize a non-byte[] / non-String payload into bytes. According to the Spring Messaging contract, fromMessage is for inbound deserialization (bytes → object) while toMessage is for outbound serialization (object → bytes). The outbound publish path is using the wrong direction.

To Reproduce

Typical outbound gateway setup with contentType=application/json:

@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel",
        defaultHeaders = @GatewayHeader(name = MessageHeaders.CONTENT_TYPE,
                value = MediaType.APPLICATION_JSON_VALUE))
public interface MqttGateway {
    void send(@Header(MqttHeaders.TOPIC) String topic, Object payload);
}

@Bean
IntegrationFlow mqttOutFlow(Mqttv5PahoMessageHandler messageHandler) {
    return IntegrationFlow.from("mqttOutboundChannel")
        .handle(messageHandler)
        .get();
}

Calling gateway.send("topic", new TestPojo(...)) triggers the issue, resulting in the following exception:

JsonParseException: Unrecognized token '': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

The submitted PR includes unit tests in Mqttv5HandlerTests that reproduce the bug and verify the fix.

Expected behavior

The handler should invoke converter.toMessage(payload, headers). With the default Jackson-backed converter and contentType=application/json, this produces valid JSON bytes.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions