Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable sending String payloads with JSON content type in SqsTemplate #1144

Open
tomazfernandes opened this issue May 2, 2024 · 1 comment
Labels
component: sqs SQS integration related issue status: ideal-for-contribution We agree it's nice to have but it is not team priority type: enhancement Smaller enhancement in existing integration

Comments

@tomazfernandes
Copy link
Contributor

Currently due to Spring Messaging internals if we send a String message containing a JSON without specifying a Content-Type it'll default to text/plain in the StringMessageConverter.

If we set .header(MessageHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON), it will be mapped by MappingJackson2MessageConverter which will double serialize the payload, which won't be readable.

We should look into a way of enabling users to send a String json with APPLICATION_JSON content type so it can be properly deserialized by the consumer.

@tomazfernandes tomazfernandes added component: sqs SQS integration related issue status: ideal-for-contribution We agree it's nice to have but it is not team priority type: enhancement Smaller enhancement in existing integration labels May 2, 2024
@imsosleepy
Copy link

imsosleepy commented May 11, 2024

@tomazfernandes
I'm looking into this issue, but my sample code doesn't seem to be checking for this issue properly.

Below is the code I used, with some modifications from the SQS sample code in this project.

@SpringBootApplication
public class SpringSqsSample {

	private static final Logger LOGGER = LoggerFactory.getLogger(SpringSqsSample.class);

	private static final String QUEUE_NAME = "test-queue";

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

	@SqsListener(queueNames = QUEUE_NAME, acknowledgementMode = "ON_SUCCESS")
	void listen(Object message) {
		LOGGER.info("Received message : " + message);
	}

	@Bean
	public ApplicationRunner sendMessageToQueue2(SqsTemplate sqsTemplate) {
		String jsonString = """
			{
			  "message1": "hello",
			  "message2": "sqs!"
			}
			""";

		return args -> sqsTemplate.send(sqsTemplate.send(to -> to.queue(QUEUE_NAME).payload(jsonString).delaySeconds(30).header(MessageHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)));
	}
}

Can you tell me if there's something wrong with my code or give me an example of a situation where it would be double serialize?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: sqs SQS integration related issue status: ideal-for-contribution We agree it's nice to have but it is not team priority type: enhancement Smaller enhancement in existing integration
Projects
None yet
Development

No branches or pull requests

2 participants