如何使用Spring Boot配置Jackson转换器?

时间:2017-06-09 12:59:15

标签: java spring-boot jackson jms activemq

我想使用JMS和ActiveMQ将对象从应用程序发送到另一个。

 org.springframework.jms.support.converter.MessageConversionException: Failed to resolve type id [com.wajdi.act.Message]; nested exception is java.lang.ClassNotFoundException: com.wajdi.act.Message
    at org.springframework.jms.support.converter.MappingJackson2MessageConverter.getJavaTypeForMessage(MappingJackson2MessageConverter.java:507)
    at org.springframework.jms.support.converter.MappingJackson2MessageConverter.fromMessage(MappingJackson2MessageConverter.java:228)
    at org.springframework.jms.core.JmsTemplate.doConvertFromMessage(JmsTemplate.java:857)
    at org.springframework.jms.core.JmsTemplate.receiveAndConvert(JmsTemplate.java:831)
    at com.wajdi.jmsrece.JmsReceiverApplication.main(JmsReceiverApplication.java:53)
Caused by: java.lang.ClassNotFoundException: com.wajdi.act.Message
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
    at org.springframework.jms.support.converter.MappingJackson2MessageConverter.getJavaTypeForMessage(MappingJackson2MessageConverter.java:503)
    ... 4 more
2017-06-09 14:46:46.405  INFO 1732 --- [ActiveMQ Task-1] o.a.a.t.failover.FailoverTransport       : Successfully connected to tcp://localhost:61616

发送对象的类:

public class JmsAndActivitiApplication {
    @Bean
    public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        // This provides all boot's default to this factory, including the message converter
        configurer.configure(factory, connectionFactory);
        // You could still override some of Boot's default if necessary.
        return factory;
    }

    @Bean // Serialize message content to json using TextMessage
        public MessageConverter jacksonJmsMessageConverter() {
            MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
            converter.setTargetType(MessageType.BYTES);
            converter.setTypeIdPropertyName("_type");

            return converter;
        }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(JmsAndActivitiApplication.class, args);
        JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
        System.out.println("*******SEND**********");
        jmsTemplate.convertAndSend("DEMO-JMS-QUEUE", new Message("1", "Hello"));
        System.out.println("----------------------");

    }
}

和收到的课程:

@SpringBootApplication
@EnableJms
public class JmsReceiverApplication {

    @Bean
    public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        // This provides all boot's default to this factory, including the message converter
        configurer.configure(factory, connectionFactory);
        // You could still override some of Boot's default if necessary.
        return factory;
    }

    @Bean // Serialize message content to json using TextMessage
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.BYTES);
        converter.setTypeIdPropertyName("_type");

        return converter;
    }


    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(JmsReceiverApplication.class, args);

        JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

        System.out.println("*******receiving**********");
        while(true){
            try {
                System.out.println(jmsTemplate.receiveAndConvert("DEMO-JMS-QUEUE"));
            } catch (JmsException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

我总是得到一个异常,告诉我它无法解析类型。我认为我必须改变Jackson转换器的配置,但我不知道如何。

1 个答案:

答案 0 :(得分:0)

@SpringBootApplication

@EnableJms 公共类JmsReceiverApplication {

@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    // This provides all boot's default to this factory, including the message converter
    configurer.configure(factory, connectionFactory);
    // You could still override some of Boot's default if necessary.
    return factory;
}

@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setTargetType(MessageType.TEXT);
    converter.setTypeIdPropertyName("_type");

    return converter;
}


public static void main(String[] args) {
    ConfigurableApplicationContext context = SpringApplication.run(JmsReceiverApplication.class, args);

    JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

    System.out.println("*******receiving**********");
    while(true){
        try {
            TextMessage textMessage = (TextMessage) jmsTemplate.receive("DEMO-JMS-QUEUE");
        /*  System.out.println(textMessage.getText());
            System.out.println(textMessage.getText());*/
            Gson gson=new Gson();
            String json=gson.toJson(textMessage.getText());
            Message msg = gson.fromJson(textMessage.getText(), Message.class);
            System.out.println("Object: "+msg);
    //  System.out.println(json);



        } catch (JmsException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
}