Creating REST service using spring-data-cassandra and spring-data-rest-webmvc

6,099 views
Skip to first unread message

Jega A

unread,
May 13, 2014, 12:03:13 PM5/13/14
to spring-dat...@googlegroups.com
I am working on a POC -- creating a REST service to perform basic CRUD operations into Cassandra.
I am using spring-data-cassandra and spring-data-rest-web-mvc

I have created three Java files 
Contract.java -> Entity description of the Cassandra column family (Table)
ContractRepository.java -> Repository information
Application.java -> Contains the main

I am trying to follow some of the samples outlined in String Data website. Specifically the tutorials that show how to use REST/MongoDB  (http://spring.io/guides/gs/accessing-mongodb-data-rest/) 
Since there are no specific tutorial for Cassandra (as yet) I am trying to replicate the MongoDb description with Cassandra.  
I am trying to run the application without any configuration files and also using spring-boot -- similar to how the tutorials are done with Mongo.
I am using maven to do the compile/build and run. I am starting the run using "mvn spring-boot:run".
I am getting a run time exception while trying to run this.

The key error is the following:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contractRepository': 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 cass_test3.Application: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null

The cassandraTemplate bean is not being created!


Here is the code 

Application.java

package cass_test3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;



@Configuration
@EnableCassandraRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    }

}


Contract.java 

package cass_test3;


import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.Table;

@Table(value="contracts")
public class Contract {

@PrimaryKey(value="id")
@Column(value="id")
private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}

ContractRepository.java

package cass_test3;


import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "contract", path = "contract")
public interface ContractRepository extends CassandraRepository<Contract> {
Contract findById(@Param("id") String id);
}


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<modelVersion>4.0.0</modelVersion>

<groupId>cass-test3</groupId>
<artifactId>cass-test3</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.2.RELEASE</version>
</parent>

<repositories>
<repository>
<id>spring-libs-snapshot</id>
<snapshots>
<enabled>true</enabled>
</snapshots>

</repository>
<repository>
<id>spring-milestones</id>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>2.1.0.RC1</version>

</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>1.0.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
</dependencies>
</project>

I understand that a default cassandraTemplate bean should be created at run time using the standard defaults for the connection to ContractRepository.
Somehow that is not being created. Do I need to explicitly initialize the cassandraTemplate? If so how do I do it?

Thanks

Jega




Matthew Adams

unread,
May 13, 2014, 12:49:46 PM5/13/14
to Jega A, spring-dat...@googlegroups.com
I'd say that you at minimum need to configure a Cassandra Cluster, then a Session to give to the CassandraTemplate.  See org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration for details.

I'm sorry to say that I'm not too familiar with Spring Boot yet.  If you find that we could automatically create a Cluster & Session with reasonable defaults (localhost, 9042) for use with Spring Boot, please enter an issue for that.


--
You received this message because you are subscribed to the Google Groups "Spring Data Cassandra" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spring-data-cass...@googlegroups.com.
To post to this group, send email to spring-dat...@googlegroups.com.
Visit this group at http://groups.google.com/group/spring-data-cassandra.
To view this discussion on the web visit https://groups.google.com/d/msgid/spring-data-cassandra/dd5b65de-d6d5-41bb-918b-95d2df6aa6c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
mailto:mat...@matthewadams.me 

Jega A

unread,
May 13, 2014, 4:14:28 PM5/13/14
to Matthew Adams, spring-dat...@googlegroups.com
Thanks for your tip. I was able to get the code running by adding some initialization for the CassandraTemplate.
I think it would be nice to have a default implementation that uses localhost:9042 (similar to the spring-data-mongo and other implementations).

===============

Modified Application.java

public class Application extends AbstractCassandraConfiguration{
private static final String KEYSPACE_NAME = "contracts_ks";
private static final String CONTACT_POINTS = "192.168.72.100";
private static final int PORT = 9042;
private static final int MAX_POOL_CONNECTION = 50;

@Override
protected String getKeyspaceName() {

return KEYSPACE_NAME;
}

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

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


@Bean(name="casops")
public CassandraOperations operations() throws Exception {

return new CassandraTemplate(session().getObject(), new MappingCassandraConverter(new BasicCassandraMappingContext()));
--
Jega

Matthew Adams

unread,
May 13, 2014, 4:17:20 PM5/13/14
to Jega A, spring-dat...@googlegroups.com
Glad to hear!  Please enter an issue for us to track this.
Reply all
Reply to author
Forward
0 new messages