Dakotah North
Dakotah North

Reputation: 1344

Converting Spring Cloud Stream message with JSON to POJO

Spring documentation highlights the ability for Spring Cloud Stream to automatically convert JSON to a POJO.

https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#_sending_arbitrary_data_to_an_output_e_g_foreign_event_driven_sources

By using the below code ...

@SpringBootApplication
public class LoggingConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoggingConsumerApplication.class, args);
    }

    @Bean
    public Consumer<Person> exampleEventConsumer() {
        return person -> {
            System.out.println("Received: " + person);
        };
    }

    public static class Person {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String toString() {
            return this.name;
        }
    }
}

And I am using the corresponding application.yaml file for Solace messaging ...

spring:
  cloud:
    function:
      definition: exampleEventConsumer
    stream:
      bindings:
        exampleEventConsumer-in-0:
          destination: try-me

      binders:
        solace-broker:
          type: solace
          environment:
            solace:
              java:
                host: "tcp://mr-connection-zw285jhb96b.messaging.solace.cloud:55555"
                msgVpn:  "msgvpn-example"
                clientUsername:  "solace-cloud-client"
                clientPassword:  "p2tkutqaru4pss6t9s0"

and when I send a text or binary message with the payload of

{\"name\":\"Miles Archer\"}

from the Solace broker, I get the following error

2024-04-15T22:48:45.493-04:00 ERROR 44014 --- [HelloWorldSpringCloudStream] [pool-3-thread-1] o.s.integration.handler.LoggingHandler   : 
org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.cloud.stream.function.FunctionConfiguration$FunctionToDestinationBinder$1@5fe7e7e5], 
failedMessage=GenericMessage [payload={\"name\":\"Miles Archer\"}, 
headers={solace_expiration=0, solace_destination=try-me, solace_replicationGroupMessageId=rmid1:30704-81ae9045b12-00000000-00000146, deliveryAttempt=3, solace_isReply=false, solace_timeToLive=0, solace_receiveTimestamp=0, acknowledgmentCallback=com.solace.spring.cloud.stream.binder.inbound.acknowledge.JCSMPAcknowledgementCallback@19dc7076, solace_discardIndication=false, solace_dmqEligible=false, solace_priority=-1, solace_redelivered=false, id=61619594-f32e-a2a9-3738-baea97e80635, contentType=application/json, timestamp=1713235722421}]

While the documentation says ...

Doing so also lets you see one of the core features of the framework: It tries to automatically convert incoming message payloads to type Person.

All my attempts to do so have failed. Any guidance into what I am missing is greatly appreciated!

Upvotes: 0

Views: 71

Answers (1)

This is from the source of the GenericMessage:

if (this.payload instanceof byte[] bytes) {
  sb.append("byte[").append(bytes.length).append(']');
}
else {
  sb.append(this.payload);
}

When the payload is String, it writes as is. I think the problem is with the backslashes.

You should send messages to Solace without escaping double quotation marks:

curl -X POST \
  https://your-solace-broker/topic/{topic_name} \
  -H 'Content-Type: application/json' \
  -d '{"name": "Miles Archer"}'
    
}

Upvotes: 0

Related Questions