gpt4 book ai didi

spring-jms - Spring mappingjackson2messageconverter 给出空指针异常

转载 作者:行者123 更新时间:2023-12-04 20:37:39 27 4
gpt4 key购买 nike

我正在尝试使用 mappingjackson2messageconvertor 将 java 对象转换为 json 格式但看起来它没有转换为 json 对象,而是抛出空点异常错误。想知道我做错了什么。

我检查了一个例子 here .但是我没有使用rabbitmq 而不是使用activemq。

来源

@SpringBootApplication
@EnableJms
public class Application {
@Bean
JmsListenerContainerFactory<?> myJmsContainerFactory(ConnectionFactory connectionFactory) {
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(new MappingJackson2MessageConverter());
return factory;
}


public static void main(String[] args) {
// Clean out any ActiveMQ data from a previous run
FileSystemUtils.deleteRecursively(new File("activemq-data"));

// Launch the application
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
AssetApi asset = new AssetApi();
asset.setBroadcasterId("test");
asset.setNotes("test");

// Send a message
MessageCreator messageCreator = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MessageConverter message = new MappingJackson2MessageConverter();
return message.toMessage(asset, session);
}
};
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
System.out.println("Sending a new message.");
jmsTemplate.send("mailbox-destination", messageCreator);
}
}

错误
java.lang.NullPointerException: null
at org.apache.activemq.command.ActiveMQMessage.getStringProperty(ActiveMQMessage.java:676)
at org.springframework.jms.support.converter.MappingJackson2MessageConverter.getJavaTypeForMessage(MappingJackson2MessageConverter.java:377)
at org.springframework.jms.support.converter.MappingJackson2MessageConverter.fromMessage(MappingJackson2MessageConverter.java:195)
at org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:215)
at org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter.extractPayload(AbstractAdaptableMessageListener.java:397)
at org.springframework.jms.support.converter.MessagingMessageConverter.fromMessage(MessagingMessageConverter.java:108)
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.toMessagingMessage(MessagingMessageListenerAdapter.java:77)
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:62)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:678)
at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:638)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:608)
at org.springframework.jms.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:579)
at org.springframework.jms.listener.SimpleMessageListenerContainer.processMessage(SimpleMessageListenerContainer.java:329)
at org.springframework.jms.listener.SimpleMessageListenerContainer$2.onMessage(SimpleMessageListenerContainer.java:305)
at org.apache.activemq.ActiveMQMessageConsumer.dispatch(ActiveMQMessageConsumer.java:1390)
at org.apache.activemq.ActiveMQSessionExecutor.dispatch(ActiveMQSessionExecutor.java:131)
at org.apache.activemq.ActiveMQSessionExecutor.iterate(ActiveMQSessionExecutor.java:202)
at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:133)
at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

最佳答案

使用 JMS 转换器,您需要告诉转换器您要从 JSON 创建什么类型 (id)。

您可以通过告诉它哪个 JMS 属性( header )包含类型 ID 来做到这一点。

然后是 Map从类型 ID 到类名。

或者你可以子类化并覆盖 getJavaTypeForMessage() .

有关方法 getJavaTypeForMessage() 的信息,请参见 javadoc在转换器上。

关于spring-jms - Spring mappingjackson2messageconverter 给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32385062/

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