Spring mappingjackson2messageconverter给出空指针异常

时间:2015-09-03 20:42:14

标签: spring-jms spring-json

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

我检查过一个示例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)

4 个答案:

答案 0 :(得分:2)

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

通过告诉它哪个JMS属性(标题)包含类型id来实现这一点。

然后从类型ID到类名称有Map

或者您可以继承并覆盖getJavaTypeForMessage()

请参阅转换器上方法getJavaTypeForMessage()的javadoc。

答案 1 :(得分:1)

我按照Gary Russell的说法,从我的通话班中指出了

Map<String, Class<?>> typeIdMappings = new HashMap<String, Class<?>>();
typeIdMappings.put("JMSType",Audit.class);
mappingJackson2MessageConverter.setTypeIdMappings(typeIdMappings);
mappingJackson2MessageConverter.setTypeIdPropertyName("JMSType"); 
Audit audit = (Audit) mappingJackson2MessageConverter.fromMessage(message);

然后我覆盖了MappingJackson2MessageConverter的方法:

@Component
public class MecMappingJackson2MessageConverter extends MappingJackson2MessageConverter {

    private Map<String, Class<?>> idClassMappings = new HashMap<String, Class<?>>();

    private Map<Class<?>, String> classIdMappings = new HashMap<Class<?>, String>();

    private ObjectMapper objectMapper = new ObjectMapper();

    private ClassLoader beanClassLoader;

    @Override
    public JavaType getJavaTypeForMessage(Message message){

        Class<?> mappedClass = this.idClassMappings.get("JMSType");
    if (mappedClass != null) {
        return this.objectMapper.getTypeFactory().constructType(mappedClass);
    }
    try {
        Class<?> typeClass = ClassUtils.forName("JMSType", this.beanClassLoader);
            return this.objectMapper.getTypeFactory().constructType(typeClass);
        }
        catch (Throwable ex) {
            throw new MessageConversionException("Failed to resolve type id [" + "JMSType" + "]", ex);
        }

    }

    @Override
    public void setTypeIdMappings(Map<String, Class<?>> typeIdMappings) {
        this.idClassMappings = new HashMap<String, Class<?>>();
        for (Map.Entry<String, Class<?>> entry : typeIdMappings.entrySet()) {
            String id = entry.getKey();
            Class<?> clazz = entry.getValue();
        this.idClassMappings.put(id, clazz);
        this.classIdMappings.put(clazz, id);
    }
}

}

答案 2 :(得分:0)

我创建了自己的反序列化器,并在我的MappingJackson2MessageConverter对象中设置相同的内容,如下所示

ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(WebResponse.class,new WebResponseDeserializer());
mapper.registerModule(module);
new MappingJackson2MessageConverter().setObjectMapper(mapper);

WebResponseDeserializer扩展com.fasterxml.jackson.databind.JsonDeserializer以提供自定义的json对象解组方式

答案 3 :(得分:0)

您只需要在消息转换器上调用setTypeIdPropertyName并传递消息标题所需的名称,其中消息转换器将保存您的pojo的类名。