问题
Error :
java.lang.NoClassDefFoundError: org/springframework/amqp/support/converter/MessageConverter at java.lang.Class.getDeclaredMethods0(Native Method)
RabbitConfig.java
package com.example.demo;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class RabbitMQConfig {
@Value("${javainuse.rabbitmq.queue}")
String queueName;
@Value("${javainuse.rabbitmq.exchange}")
String exchange;
@Value("${javainuse.rabbitmq.routingkey}")
private String routingkey;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
ConnectionFactory connectionFactory(){
CachingConnectionFactory connectionFactory=new CachingConnectionFactory("127.0.0.1");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setPort(5672);
connectionFactory.setPublisherReturns(true);
return connectionFactory;
}
@Bean
DirectExchange exchange() {
return new DirectExchange(exchange);
}
@Bean
Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(routingkey);
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
}
Pom.xml:: Dependency
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>queue-service</artifactId>
<groupId>com.hp.oo.unifiedcommons</groupId>
<version>2020.05.0.47-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>queue-service-impls</artifactId>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>2.2.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.hp.oo.unifiedcommons</groupId>
<artifactId>oo-outbox-mapper-api</artifactId>
<version>2020.05.0.47-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
I am not sure but is it because of spring version ??. I mean sometimes i am getting error in ConnectionFactory function or sometimes i got error in MessageConvertor function that I am using in my RabbitMQConfig.java .
Any suggestion how to fix this ?
来源:https://stackoverflow.com/questions/62365086/getting-errors-while-providing-rabbitmq-configuration-in-spring-boot-application