rabbitmq 消费 json 消息并转换为 Java 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32330909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rabbitmq consume json message and convert into Java object
提问by Robbo_UK
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.
我已经整理了一个 java 测试。它将消息放入队列并将其作为字符串返回。我试图实现的是将它转换为 java 对象 SignUpDto。我已经尽可能地精简了这个问题的代码。
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 Hymanson2JsonMessageConverter());
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()
}
}
回答by Gary Russell
Configure the RabbitTemplate
with a Hymanson2JsonMessageConverter
.
配置RabbitTemplate
了Hymanson2JsonMessageConverter
。
Then use
然后使用
template.convertAndSend("myqueue", myDto);
...
SignUpDto out = (SignUpDto) template.receiveAndConvert("myQueue");
Note that the outbound conversion sets up the content type (application/json) and headers with type information that tells the receiving converter what object type to create.
请注意,出站转换设置内容类型 (application/json) 和带有类型信息的标头,这些信息告诉接收转换器要创建的对象类型。
If you really do want to send a simple String of JSON, you need to set the content type to application/json
. To help the inbound conversion, you can either set the type headers (look at the converter source for information), or you can configure the converter with a ClassMapper
to determine the type.
如果您确实想发送一个简单的 JSON 字符串,则需要将内容类型设置为application/json
. 为了帮助入站转换,您可以设置类型标头(查看转换器源以获取信息),或者您可以使用 a 配置转换器ClassMapper
以确定类型。
EDIT
编辑
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
message-converter="json" />
<bean id="json"
class="org.springframework.amqp.support.converter.Hymanson2JsonMessageConverter" />
Or, since you are using Java Config; simply inject one into your template definition.
或者,由于您使用的是 Java Config;只需将一个注入到您的模板定义中。
EDIT2
编辑2
If you want to send a plain JSON string; you need to help the inbound converter via headers.
如果你想发送一个普通的 JSON 字符串;您需要通过标头帮助入站转换器。
To set the headers...
要设置标题...
template.convertAndSend("", "myQueue", jsonString, new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
message.getMessageProperties().setContentType("application/json");
message.getMessageProperties().getHeaders()
.put(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME, "foo.SignUpDto");
return message;
}
});
Bear in mind, though, that this sendingtemplate must NOThave a JSON message converter (let it default to the SimpleMessageConverter
). Otherwise, the JSON will be double-encoded.
请记住,虽然,这个发送模板必须不是有一个JSON消息变换(让它默认为SimpleMessageConverter
)。否则,JSON 将被双重编码。