This is a simple project that utilizes Spring and Casquatch to provide a Rest API for a given schema
In this tutorial we are going to step through the creation of a simple project from start to finish.
The following prerequisites are required:
Spring offers the Spring Initializer to quick start a project. We will use this to build out the basic template.
unzip springrest.zip
rm -rf .mvn
rm -rf src/main/resources/static
rm -rf src/main/resources/templates
rm mvnw*
rm HELP.md
If you don’t already have a running Cassandra instance to connect to, you can spin one up quickly using Docker.
docker run --rm -p 9042:9042 --name springrest -d cassandra:latest
docker exec -i springrest cqlsh <<EOF
CREATE KEYSPACE springrest WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
CREATE TABLE springrest.table_name (
key_one int,
key_two int,
col_one text,
col_two text,
PRIMARY KEY (key_one, key_two)
);
EOF
Now the project is ready for Casquatch Integration by adding dependencies, entity, and required annotations.
<casquatch.version>2.0-RELEASE</casquatch.version>
<dependency>
<groupId>com.tmobile.opensource.casquatch</groupId>
<artifactId>casquatch-driver-spring</artifactId>
<version>${casquatch.version}</version>
</dependency>
<dependency>
<groupId>com.tmobile.opensource.casquatch</groupId>
<artifactId>casquatch-driver-tests</artifactId>
<version>${casquatch.version}</version>
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>com.tmobile.opensource.casquatch</groupId>
<artifactId>casquatch-driver-processor</artifactId>
<version>${casquatch.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
casquatch.basic.contact-points.0="127.0.0.1:9042"
casquatch.basic.session-keyspace = springrest
casquatch.basic.load-balancing-policy.local-datacenter=datacenter1
@CasquatchEntity
@Getter @Setter @NoArgsConstructor
public class TableName extends AbstractCasquatchEntity {
@PartitionKey
private Integer keyOne;
@ClusteringColumn(1)
private Integer keyTwo;
private String colOne;
private String colTwo;
}
@CasquatchSpring(generateRestDao = true)
mvn spring-boot:run
Now that the application is running, it is ready to start serving queries.
curl -X POST "http://localhost:8080/TableName/save" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"payload\": { \"keyOne\": 1, \"keyTwo\": 1,\"colOne\":\"test\",\"colTwo\":\"test2\" }}"
curl -X POST "http://localhost:8080/TableName/get" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"payload\": { \"keyOne\": 1, \"keyTwo\": 1 }}"
In order to easily interact and see the APIs, you can optionally add Swagger UI.
<swagger.version>2.9.2</swagger.version>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
@SpringBootApplication
@CasquatchSpring(generateRestDao = true)
@EnableSwagger2
public class SpringRestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringRestApplication.class, args);
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.tmobile.opensource.casquatch.examples.springrest"))
.paths(PathSelectors.any())
.build();
}
}