gpt4 book ai didi

java - Spring Boot RabbitMQ 接收器 Jackson 反序列化为 POJO

转载 作者:行者123 更新时间:2023-11-30 02:25:11 27 4
gpt4 key购买 nike

我有一个 Spring Boot 项目,我正在尝试与 RabbitMQ 服务器集成,以便我可以向队列发布消息并从队列读取消息。

这是我的rabbitmq配置(编辑后仅显示相关详细信息):

@Configuration
@ConfigurationProperties(prefix="rabbit")
public class RabbitConfig {
private String queue;

@Bean
Queue queue() {
return new Queue(queue, durable);
}

@Bean
public MessageConverter jsonMessageConverter(){
return new Jackson2JsonMessageConverter();
}

@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, Queue queue,
MessageListenerAdapter listenerAdapter, MessageConverter messageConverter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
container.setQueues(queue);
container.setMessageListener(listenerAdapter);
container.setMessageConverter(messageConverter);
return container;
}

@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver);
}

public void setQueue(String queue) {
this.queue = queue;
}
}

这是我的接收器类:

public interface Receiver {
void handleMessage(FooA message);
}

@Component
public class RabbitReceiver implements Receiver {
@Override
public void handleMessage(FooA message) {
System.out.println(message);
}
}

还有我的pojo:

public class FooA {}
private double num;
private Map<String, String> map = new HashMap();

public FooA() {
}

public FooA(double num, Map<String, String> map) {
this.num = num;
this.map = map;
}

public int getnum() {
return num;
}

public Map<String, String> getMap() {
return map;
}
}

我成功地将 FooA 消息对象发布到队列。这是队列中的样子:

[
{
"payload_bytes": 41,
"redelivered": false,
"exchange": "amq.fanout",
"routing_key": "",
"message_count": 0,
"properties": {
"priority": 0,
"delivery_mode": 2,
"headers": {
"__TypeId__": "com.test.FooA"
},
"content_encoding": "UTF-8",
"content_type": "application/json"
},
"payload": "{\"num\":1.2,\"map\":{}}",
"payload_encoding": "string"
}
]

但是当我尝试从队列中读取时,我收到此错误:

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Failed to invoke target method 'handleMessage' with argument type = [class [B], value = [{[B@75a7bfc9}]
at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:408) ~[spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:298) ~[spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:822) ~[spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:745) ~[spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:97) [spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:189) ~[spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1276) [spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:726) ~[spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1219) [spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1189) [spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1500(SimpleMessageListenerContainer.java:97) [spring-rabbit-1.7.3.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1421) [spring-rabbit-1.7.3.RELEASE.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
Caused by: java.lang.NoSuchMethodException: com.test.RabbitReceiver.handleMessage([B)
at java.lang.Class.getMethod(Class.java:1786) ~[na:1.8.0_121]
at org.springframework.util.MethodInvoker.prepare(MethodInvoker.java:174) ~[spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:386) ~[spring-rabbit-1.7.3.RELEASE.jar:na]
... 12 common frames omitted

我做错了什么?

编辑1:我将方法更改为:

@Override
public void handleMessage(byte[] message) {
System.out.println(message);
}

这有效,但完全无法使用。它只是显示如下:

enter image description here

如何将其映射到我的 pojo FooA

最佳答案

我刚刚让类实现了 Serialized 接口(interface),并删除了 JsonMessageConverter。 Json 和 Serialized 是冲突的,所以不起作用。

关于java - Spring Boot RabbitMQ 接收器 Jackson 反序列化为 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45827213/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com