# Content resolution example with Spring Cloud Stream

**URL:** <https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906>\
**Category:** Solace Event Broker\
**Tags:** spring-cloud-stream\
**Created:** [April 15, 2024, 12:41am UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906 "2024-04-15T00:41:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dakotahnorth](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@dakotahnorth](https://community.solace.com/u/dakotahnorth)\
**Post date:** [April 15, 2024, 12:41am UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/1 "2024-04-15T00:41:28Z")

</div>

The Spring documentation mentions one of the core features of the framework:

It tries to automatically convert incoming message payloads to type

`Person`

with the following code:

```kotlin

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

```

My attempts to have the incoming message automatically convert into a `Person ` type with Solace have failed.

Please point me to an Solace example of sending & receiving messages thru Spring Cloud Stream where the incoming message payload is automatically converted.

---

<div class="post-metadata">

**Author:** ![dakotahnorth](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@dakotahnorth](https://community.solace.com/u/dakotahnorth)\
**Post date:** [April 15, 2024, 1:08am UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/2 "2024-04-15T01:08:44Z")

</div>

Spring Cloud Stream documentation … [Preface](https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#spring-cloud-stream-preface-adding-message-handler)

---

<div class="post-metadata">

**Author:** ![dakotahnorth](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@dakotahnorth](https://community.solace.com/u/dakotahnorth)\
**Post date:** [April 15, 2024, 1:43am UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/3 "2024-04-15T01:43:40Z")

</div>

This is also referenced in the Solace code labs …

By default when coding your Spring Cloud Stream microservice you are writing Spring Cloud Function beans that can be re-used for multiple purposes and can leverage the framework’s

Content Type Negotiation

to pass your POJOs directly into the function while decoupling your business logic from the specific runtime target and triggering mechanism (web endpoint, stream processor, task).

> **[Spring Cloud Stream - Beyond the Basics](https://codelabs.solace.dev/codelabs/spring-cloud-stream-beyond/?_gl=1*155nf2j*_ga*MTMyOTkzNDg4OC4xNzExOTM3MDcw*_ga_XZ3NWMM83E*MTcxMzEwMTgxOC4xMy4wLjE3MTMxMDE4MTguMC4wLjA.#3)**

---

<div class="post-metadata">

**Author:** ![Aaron](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/aaron/32/843_2.png) [@Aaron](https://community.solace.com/u/Aaron)\
**Post date:** [April 15, 2024, 8:05am UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/4 "2024-04-15T08:05:37Z")

</div>

Hi @dakotahnorth , welcome to the Community. You’re going to need some way to deserialize the payload into your POJO. What format is your payload in? Spring can’t just guess how to do this.

For example, if you look at the code generated by [the Spring Cloud Stream generator for AsyncAPI docs](https://github.com/asyncapi/java-spring-cloud-stream-template) , it uses Jackson `@JsonInclude` to build the POJOs from JSON schemas:

```typescript

package com.example.app;

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class PassengerInfo {

	public PassengerInfo () {
	}

	public PassengerInfo (
		String passengerAction, 
		String destination, 
		java.math.BigDecimal passengerId, 
		String tripId, 
		String vipStatus) {
		this.passengerAction = passengerAction;
		this.destination = destination;
		this.passengerId = passengerId;
		this.tripId = tripId;
		this.vipStatus = vipStatus;
	}

	private String passengerAction;
	private String destination;
	private java.math.BigDecimal passengerId;
	private String tripId;
	private String vipStatus;
	public String getPassengerAction() {
		return passengerAction;
	}

	public PassengerInfo setPassengerAction(String passengerAction) {
		this.passengerAction = passengerAction;
		return this;
	}

	public String getDestination() {
		return destination;
	}

	public PassengerInfo setDestination(String destination) {
		this.destination = destination;
		return this;
	}

	public java.math.BigDecimal getPassengerId() {
		return passengerId;
	}

	public PassengerInfo setPassengerId(java.math.BigDecimal passengerId) {
		this.passengerId = passengerId;
		return this;
	}

	public String getTripId() {
		return tripId;
	}

	public PassengerInfo setTripId(String tripId) {
		this.tripId = tripId;
		return this;
	}

	public String getVipStatus() {
		return vipStatus;
	}

	public PassengerInfo setVipStatus(String vipStatus) {
		this.vipStatus = vipStatus;
		return this;
	}

	public String toString() {
		return "PassengerInfo ["
		+ " passengerAction: " + passengerAction
		+ " destination: " + destination
		+ " passengerId: " + passengerId
		+ " tripId: " + tripId
		+ " vipStatus: " + vipStatus
		+ " ]";
	}
}

```

---

<div class="post-metadata">

**Author:** ![dakotahnorth](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@dakotahnorth](https://community.solace.com/u/dakotahnorth)\
**Post date:** [April 15, 2024, 1:47pm UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/5 "2024-04-15T13:47:17Z")

</div>

Well … from the Solace AWS Broker “Try Me!” page I tried sending both binary and text messages.  
The payload in the message text box was

```bash

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

```

Changed my simple `PersonEvent` to be

```typescript

@JsonInclude(JsonInclude.Include.NON_NULL)
public class PersonEvent {

    @JsonProperty("name")
    private String name;

    public String getName() {
        return name;
}

    public void setName(String name) {
        this.name = name;
}

    public PersonEvent(String name) {
        this.name = name;
}

    public PersonEvent() {
    

}

```

Still … I am not able to deserialize the message.  
So was hoping there was some lab or github examples with Solace that you can point me to that has this all working with both publishing and receiving messages.

---

<div class="post-metadata">

**Author:** ![dakotahnorth](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@dakotahnorth](https://community.solace.com/u/dakotahnorth)\
**Post date:** [April 15, 2024, 2:10pm UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/6 "2024-04-15T14:10:14Z")

</div>

Posted the following to Github as an example …

[https://github.com/dakotahNorth/HelloWorldSpringCloudStream](https://github.com/dakotahNorth/HelloWorldSpringCloudStream)

---

<div class="post-metadata">

**Author:** ![Aaron](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/aaron/32/843_2.png) [@Aaron](https://community.solace.com/u/Aaron)\
**Post date:** [April 16, 2024, 12:45pm UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/7 "2024-04-16T12:45:35Z")

</div>

I cloned your code, even installed a JDK21. Got it working by moving the Person event class out of the Application class and into it’s own class.

**Application.java**

```typescript

@SpringBootApplication
public class HelloWorldSpringCloudStreamApplication {

    public static void main(String... args) {
        SpringApplication.run(HelloWorldSpringCloudStreamApplication.class, args);

    }

    @Bean
    public Consumer<PersonEvent> exampleEventConsumer() {

        return personEvent -> {
            System.out.println("Received event: " + personEvent.getName());

        };
    }
}

```

**PersonEvent.java**

```typescript

@JsonInclude(JsonInclude.Include.NON_NULL)
public class PersonEvent {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "PersonEvent, name="+name;
    }
}

```

Also, you were escaping your quotes in your payload…? Here’s a handy command-line tool my colleague wrote that is useful for things like this:

> **[GitHub - SolaceLabs/solace-tryme-cli](https://github.com/SolaceLabs/solace-tryme-cli)**
>
> Contribute to SolaceLabs/solace-tryme-cli development by creating an account on GitHub.

Just download the latest release, that’s what I use:

```applescript

$ ./stm send -t try-me -m '{"name":"Aaron"}'

```

Hope that’s enough to get you going now…! ??

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/marc/32/2191_2.png) [@marc](https://community.solace.com/u/marc)\
**Post date:** [April 16, 2024, 1:14pm UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/8 "2024-04-16T13:14:08Z")

</div>

@dakotahnorth , as you see in the Spring Cloud Stream documentation the framework automatically provides this deserialization for you as long as it can understand the payload.

We have examples of it in our samples repo here:

> **[GitHub - SolaceSamples/solace-samples-spring: Developer Samples for using Solace PubSub+ with...](https://github.com/SolaceSamples/solace-samples-spring)**
>
> Developer Samples for using Solace PubSub+ with Spring Framework

- see the cloud-stream-sink for the simplest of examples: [solace-samples-spring/cloud-stream-sink at master · SolaceSamples/solace-samples-spring · GitHub](https://github.com/SolaceSamples/solace-samples-spring/tree/master/cloud-stream-sink)

Note that the framework provides [these MessageConverters](https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#_provided_messageconverters) and if you need others you can see the “User-defined Message Converters” section right after that one.

FYI @Aaron no need to do the serialization and deserialization yourself!

---

<div class="post-metadata">

**Author:** ![dakotahnorth](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@dakotahnorth](https://community.solace.com/u/dakotahnorth)\
**Post date:** [April 16, 2024, 2:12pm UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/9 "2024-04-16T14:12:25Z")

</div>

> **[Content resolution example with Spring Cloud Stream](https://community.solace.com/discussion/comment/6910#Comment_6910)**
>
> The Spring documentation mentions one of the core features of the framework:

Urgh … so embarrassing! Since the documentation had the Person class as part of the same class, I just figured it was the “correct” way.  
Thank you for point that out, as well as, the quotes!

SUPER, SUPER helpful!

---

<div class="post-metadata">

**Author:** ![dakotahnorth](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@dakotahnorth](https://community.solace.com/u/dakotahnorth)\
**Post date:** [April 16, 2024, 4:17pm UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/10 "2024-04-16T16:17:23Z")

</div>

Thanks Marc!

The sink example is helpful … and for “the simplest of examples”, would be great to see this be even simpler!

Instead of

```css

channel.send(MessageBuilder.withPayload(new SensorReading("test", 50, BaseUnit.FAHRENHEIT)).build());

```

For example … with the help from above, this is what I am now doing with a `StreamBridge`

```ada

streamBridge.send("try-me", new PersonPOJO("Miles Archer"));

```

---

<div class="post-metadata">

**Author:** ![dakotahnorth](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@dakotahnorth](https://community.solace.com/u/dakotahnorth)\
**Post date:** [April 16, 2024, 4:19pm UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/11 "2024-04-16T16:19:38Z")

</div>

Re the `Person` code above …

```java

@JsonInclude(JsonInclude.Include.NON_NULL)

```

Isn’t needed. Works with just a POJO with no special annotations.

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/marc/32/2191_2.png) [@marc](https://community.solace.com/u/marc)\
**Post date:** [April 16, 2024, 7:23pm UTC](https://community.solace.com/t/content-resolution-example-with-spring-cloud-stream/1906/12 "2024-04-16T19:23:49Z")

</div>

Awesome @dakotahnorth , glad you figured it out 🙂

When you commented this which sample are you referring to? It’s been a bit since I looked through these samples, but usually we use MessageBuilder when we are trying to show adding message headers.

The sink example is helpful … and for “the simplest of examples”, would be great to see this be even simpler!
