I have put together a java test. It puts a message on a queue and returns it as a string. What Im trying to achieve is for it to it convert into the java object SignUpDto. I have stripped down the code as much as possible for the question.
The question:
How do I modify the test below to convert into a object?
SignUpClass
public class SignUpDto {
private String customerName;
private String isoCountryCode;
... etc
}
Application - Config class
@Configuration
public class Application {
@Bean
public ConnectionFactory connectionFactory() {
return new CachingConnectionFactory("localhost");
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public RabbitTemplate rabbitTemplate() {
// updated with @GaryRussels feedback
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
return rabbitTemplate;
}
@Bean
public Queue myQueue() {
return new Queue("myqueue");
}
}
The Test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Application.class})
public class TestQueue {
@Test
public void convertMessageIntoObject(){
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
AmqpTemplate template = context.getBean(AmqpTemplate.class);
String jsonString = "{ \"customerName\": \"TestName\", \"isoCountryCode\": \"UK\" }";
template.convertAndSend("myqueue", jsonString);
String foo = (String) template.receiveAndConvert("myqueue");
// this works ok
System.out.println(foo);
// How do I make this convert
//SignUpDto objFoo = (SignUpDto) template.receiveAndConvert("myqueue");
// objFoo.toString()
}
}