3

I have a springboot app, in which I am connecting to cassandra DB.

My pom.xml:

parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-cassandra</artifactId>
    </dependency>

The cassandraConfig definitition:

@Configuration
@PropertySource("file:///Users/s1b03vf/Work/carrierhub/cass.properties")
@ConfigurationProperties()
@EnableCassandraRepositories(basePackageClasses = {MCSEDistributorRepository.class})
public class MSCECassandraConfig {
protected String contactPoints;

protected int port;

protected String username;

protected String password;

protected String keyspace;


@Override
protected AuthProvider getAuthProvider() {
    return new PlainTextAuthProvider(username, password);
}


@Override
protected String getKeyspaceName() {
    return keyspace;
}


@Override
protected String getContactPoints() {
    return contactPoints;
}


@Override
protected int getPort() {
    return port;
}


@Override
public String[] getEntityBasePackages() {
    return new String[]{"com.example.demo.entity.cassandra"};
}
}

Repository class:

@Repository
public interface MCSEDistributorRepository extends CassandraRepository<MCSEDistributor, String> {

}

Entity class:

    @Table("mcse_offer")
    public class MCSEDistributor {
        @Column
        @PrimaryKey
        private int id;

        @Column
        private String name;
}

Now when I start my application I am running into the below error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/Users/s1b03vf/.m2/repository/io/springfox/springfox-spring-web/2.9.2/springfox-spring-web-2.9.2.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/Users/s1b03vf/.m2/repository/io/springfox/springfox-spring-web/2.9.2/springfox-spring-web-2.9.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerMapping' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MCSEDistributorRepository': Cannot resolve reference to bean 'cassandraTemplate' while setting bean property 'cassandraTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraTemplate' defined in class path resource [com/example/demo/config/MSCECassandraConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.core.CassandraAdminTemplate]: Factory method 'cassandraTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/example/demo/config/MSCECassandraConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'session' defined in class path resource [com/example/demo/config/MSCECassandraConfig.class]: Invocation of init method failed; nested exception is com.datastax.driver.core.exceptions.DriverInternalError: Unexpected exception thrown

It says dependency and bean creation issue for cassandraTemplate and sessionFactory. I am new to using spring-data, so not sure what I am missing here.

From startup log, I can see that it is using the below driver version:

2020-06-08 22:44:57.782  INFO 42129 --- [  restartedMain] com.datastax.driver.core                 : DataStax Java driver 3.7.2 for Apache Cassandra
7
  • Hey. This error is not related to spring data. You have a problem initializing the cassandra driver. Can you share the driver properties file? I also suggest you take a look at this awesome toturial, very eaay to understand and follow step by step baeldung.com/cassandra-datastax-java-driver Commented Jun 8, 2020 at 18:17
  • "driver properties file" ? Where is that stored? Commented Jun 8, 2020 at 18:19
  • It seems like you are missing the session part. Look at the tutorial above, it will solve your issue. Commented Jun 8, 2020 at 18:20
  • You already shared it, i missed it... Its ok Commented Jun 8, 2020 at 18:21
  • But isn't that already taken care by the spring-data-cassandra? Commented Jun 8, 2020 at 18:31

3 Answers 3

2

Try calling super and then set the required properties

@Bean
public CassandraClusterFactoryBean cluster() {
    CassandraClusterFactoryBean cluster = super.cluster();
    
    cluster.setContactPoints("127.0.0.1");
    cluster.setPort(9142);

    return cluster;
}
Sign up to request clarification or add additional context in comments.

1 Comment

1

Figured out the issue. Issue was that the CassandraConfig class was missing this:

@Override
    protected boolean getMetricsEnabled() {
        return false;
    } 

Added this and it started running fine.

Comments

0

There is something wrong with your @configuration class. it doesn't have @bean annotations. look at this example cassandra @configuration class from: https://www.baeldung.com/spring-data-cassandra-tutorial

@Configuration
public class CassandraConfig extends AbstractCassandraConfiguration {

    @Override
    protected String getKeyspaceName() {
        return "testKeySpace";
    }

    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster = 
          new CassandraClusterFactoryBean();
        cluster.setContactPoints("127.0.0.1");
        cluster.setPort(9142);
        return cluster;
    }

    @Bean
    public CassandraMappingContext cassandraMapping() 
      throws ClassNotFoundException {
        return new BasicCassandraMappingContext();
    }
}

There is also explanation on how to configure the repository and table.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.