Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.springbootdev.samples.rabbitmq.consumer;
import com.springbootdev.samples.rabbitmq.consumer.model.Item;
import com.springbootdev.samples.rabbitmq.consumer.model.Order;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class RabbitMqConfig {
@Bean
public MessageConverter jsonMessageConverter()
{
Jackson2JsonMessageConverter jsonMessageConverter = new Jackson2JsonMessageConverter();
jsonMessageConverter.setClassMapper(classMapper());
return jsonMessageConverter;
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory)
{
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(jsonMessageConverter());
return template;
}
@Bean
public DefaultClassMapper classMapper()
{
DefaultClassMapper classMapper = new DefaultClassMapper();
Map<String, Class<?>> idClassMapping = new HashMap<>();
idClassMapping.put("com.springbootdev.samples.producer.model.Order", Order.class);
classMapper.setIdClassMapping(idClassMapping);
return classMapper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment