【问题标题】:How to configure Jackson converter with Spring Boot?如何使用 Spring Boot 配置 Jackson 转换器?
【发布时间】:2017-11-11 12:19:13
【问题描述】:

我想使用 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();
            }
        }
    }
}

我总是收到一个异常,告诉我它无法解析类型。我以为我必须更改杰克逊转换器的配置,但我不知道如何。

【问题讨论】:

  • 显示com.wajdi.act.Message的代码
  • 类消息包含两个字段一个Id:String和contenu:String并实现Serializable
  • 您的根本原因是java.lang.ClassNotFoundException: com.wajdi.act.Message,对于这种异常,如果不访问您的项目,似乎无法调试或建议。
  • 感谢您的关心,我解决了这个问题。
  • 很高兴听到这个消息。如果找到解决方案,您可以随时回答您的问题,以帮助其他面临类似问题的人。

标签: java spring-boot jackson jms activemq


【解决方案1】:
@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.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();
        }
}
}

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 2019-06-07
    • 1970-01-01
    • 2016-01-28
    • 2018-01-25
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多