Home > database >  Home made Spring Boot starter is failing by NoClassDefFoundError
Home made Spring Boot starter is failing by NoClassDefFoundError

Time:08-09

I'm creating my own Spring Boot Messaging start that depends on spring-kafka 2.8.6 and it provides very user friendly use case specific methods to send and receive events.

I want to allow users to override the erro handler providing it as a @Bean if they want to.

In the starter config I have it declared like this:

starter config

@Configuration
@EnableKafka
public class StarterApplicationConfig
{   
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnClass(CommonErrorHandler.class)
    public CommonErrorHandler commonErrorHandler()
    {
        return new DefaultCommonErrorHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory(
               ConcurrentKafkaListenerContainerFactoryConfigurer configurer, 
               ConsumerFactory<Object, Object> kafkaConsumerFactory, 
               CommonErrorHandler commonErrorHandler)
    {
        ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new 
        ConcurrentKafkaListenerContainerFactory<>();
        configurer.configure(factory, kafkaConsumerFactory);

        // ***********
        // here is where I set the error handler
        // ***********
        factory.setCommonErrorHandler(commonErrorHandler);
        return factory;
    }

    ...
}

In the main Spring Boot application users can define their own error handles following the same principle:

@Configuration 
public class MainApplicationConfig
{   
    @Bean
    public CommonErrorHandler commonErrorHandler()
    {
        return new CustomCommonErrorHandler();
    }

    ...
}

Doing that I'm facing two problems:

  1. When there is no custom error handler defined I get a ClassDefNotFoundError (org.springframework.kafka.listener.CommonErrorHandler) from the starter class
  2. I cannot define a custom error handler in the main application because it doesn't find the org.springframework.kafka.listener.CommonErrorHandler class in the main application (compile time), but it finds other classes form the same kafka library.

What am I missing here?

CodePudding user response:

It is not clear why would one implement his/her own solution for definitely existing features in Spring Boot for Apache Kafka auto-configuration: https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaAnnotationDrivenConfiguration.java#L79. Either way it looks like you have a problem with versions mismatch. You said that you use spring-kafka-2.8.6, but what Spring Boot version do you use? And seeing that ClassDefNotFoundError, I assume that you don't override a Spring Boot dependency management properly and don't rely on its versions either.

See more info in Spring for Apache Kafka docs how to override those versions for Spring Boot: https://docs.spring.io/spring-kafka/docs/2.8.8/reference/html/#update-deps

  • Related