Skip to content

Commit

Permalink
Updated springrest example to full tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
tdl-jturner committed Aug 20, 2019
1 parent c07877b commit 2029248
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 44 deletions.
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ This project is designed to provide a java abstraction layer for the Cassandra d
The full manual is available at http://tmobile.github.io/casquatch

## Quick Start
* Download Generator from releases
* Run Code Generator - [Code Generator](https://tmobile.github.io/casquatch/features/codegenerator/)
```
java -Dcasquatch.generator.outputFolder=src/main/java/com/demo/mykeyspace -Dcasquatch.generator.packageName=com.demo.mykeyspace -Dcasquatch.generator.keyspace=myKeyspace -Dcasquatch.generator.datacenter=datacenter1 -jar CassandraGenerator.jar
```
* Configure Maven - [Maven Configuration](https://tmobile.github.io/casquatch/features/mavenconfiguration/)
* Optionally configure Spring - [Configure Spring](https://tmobile.github.io/casquatch/features/configurespring/)
* Enjoy! - [API](https://tmobile.github.io/casquatch/features/api/)
* Check out [Examples](https://tmobile.github.io/casquatch/examples/) for sample projects
See [Spring Rest Tutorial](https://tmobile.github.io/casquatch/examples/springrest) for a quick start example getting up and running with a Rest DAO backed by Casquatch.

## Release Notes
Release notes are now maintained in [Manual](https://tmobile.github.io/casquatch/releasenotes/)
211 changes: 176 additions & 35 deletions manual/content/examples/springrest.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,192 @@
---
title: "Spring Rest"
title: "Spring Rest - Tutorial"
---

## Overview
This is a simple project that utilizes Spring and Casquatch to provide a Rest API for a given schema. Entity was generated via the [Code Generator]({{< ref "features/codegenerator.md" >}})
This is a simple project that utilizes Spring and Casquatch to provide a Rest API for a given schema

## Github: [springrest](https://github.com/tmobile/casquatch/tree/master/casquatch-examples/springrest)
## Working Example: [springrest](https://github.com/tmobile/casquatch/tree/master/casquatch-examples/springrest)

## Schema
```
## Tutorial
In this tutorial we are going to step through the creation of a simple project from start to finish.

### Prerequisites
The following prerequisites are required:

1. JDK 8
2. Maven
3. Docker (or a running Cassandra database)

### Spring Initializer
Spring offers the Spring Initializer to quick start a project. We will use this to build out the basic template.

1. Go to https://start.spring.io/
2. Fill out form as follows
* Project: Maven Project
* Language: Java
* Spring Boot: 2.1.7
* Project Metadata
* Group: com.tmobile.opensource.casquatch.examples
* Artifact: springrest
* Dependencies
* Spring Web Starter
3. Generate the project
4. Extract and cleanup
{{< highlight bash >}}
unzip springrest.zip
rm -rf .mvn
rm -rf src/main/resources/static
rm -rf src/main/resources/templates
rm mvnw*
rm HELP.md
{{< /highlight >}}

### Setup Database in Docker
If you don't already have a running Cassandra instance to connect to, you can spin one up quickly using Docker.

1. Start Docker
{{< highlight bash >}}
docker run --rm -p 9042:9042 --name springrest -d cassandra:latest
{{< /highlight >}}
2. Import Schema
{{< highlight bash >}}
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)
key_one int,
key_two int,
col_one text,
col_two text,
PRIMARY KEY (key_one, key_two)
);
EOF
```

## Configuration
```
casquatch {
basic {
contact-points = [
"127.0.0.1:9071"
]
session-keyspace = springrest
load-balancing-policy.local-datacenter=datacenter1
}
generator {
console=false
file=true
overwrite=true
outputFolder=.
packageName=com.tmobile.opensource.casquatch.examples
createPackage=false
contactPoints=${casquatch.basic.contact-points}
keyspace=${casquatch.basic.session-keyspace}
datacenter=${casquatch.basic.load-balancing-policy.local-datacenter}
}
{{< /highlight >}}

### Casquatch Configuration
Now the project is ready for Casquatch Integration by adding dependencies, entity, and required annotations.

1. Add Properties to pom.xml
{{< highlight xml >}}
<casquatch.version>2.0-SNAPSHOT</casquatch.version>
{{< /highlight >}}
2. Add Dependencies to pom.xml
{{< highlight xml >}}
<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>
{{< /highlight >}}
3. Configure Compiler Plugin
{{< highlight xml >}}
<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>
{{< /highlight >}}
4. Add Configuration to src/main/resources/application.properties
{{< highlight ini >}}
casquatch.basic.contact-points.0="127.0.0.1:9042"
casquatch.basic.session-keyspace = springrest
casquatch.basic.load-balancing-policy.local-datacenter=datacenter1
{{< /highlight >}}
5. Create Entity as src/main/java/com/tmobile/opensource/casquatch/examples/springrest/TableName.java.
{{< highlight java >}}
@CasquatchEntity
@Getter @Setter @NoArgsConstructor
public class TableName extends AbstractCasquatchEntity {
@PartitionKey
private Integer keyOne;

@ClusteringColumn(1)
private Integer keyTwo;

private String colOne;
private String colTwo;
}
{{< /highlight >}}
Alternatively, entities can be [generated] ({{< ref "codegenerator.md" >}})
6. Add Annotation to SpringRestApplication.java
{{< highlight java >}}
@CasquatchSpring(generateRestDao = true)
{{< /highlight >}}
7. Run Application
{{< highlight bash >}}
mvn spring-boot:run
{{< /highlight >}}

### Test it out
Now that the application is running, it is ready to start serving queries.

1. Insert Data
{{< highlight bash >}}
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\" }}"
{{< /highlight >}}
1. Get Data
{{< highlight bash >}}
curl -X POST "http://localhost:8080/TableName/get" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"payload\": { \"keyOne\": 1, \"keyTwo\": 1 }}"
{{< /highlight >}}

server.servlet.context-path=/${casquatch.basic.session-keyspace}
### Optional Swagger UI
In order to easily interact and see the APIs, you can optionally add Swagger UI.

1. Add property in pom.xml
{{< highlight xml >}}
<swagger.version>2.9.2</swagger.version>
{{< /highlight >}}
2. Add Dependencies to pom.xml
{{< highlight xml >}}
<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>
{{< /highlight >}}
3. Update SpringRestApplication.java
{{< highlight java >}}
@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();
}

}
{{< /highlight >}}
4. Go to http://localhost:8080/swagger-ui.html#/table-name-_-rest-dao


```

0 comments on commit 2029248

Please sign in to comment.