From 2029248271c6e3385afca308dc663739509cc8b6 Mon Sep 17 00:00:00 2001 From: Josh Turner Date: Tue, 20 Aug 2019 10:53:47 -0700 Subject: [PATCH] Updated springrest example to full tutorial --- README.md | 10 +- manual/content/examples/springrest.md | 211 +++++++++++++++++++++----- 2 files changed, 177 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 07fc459..580e993 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/manual/content/examples/springrest.md b/manual/content/examples/springrest.md index 27195b6..550fd48 100644 --- a/manual/content/examples/springrest.md +++ b/manual/content/examples/springrest.md @@ -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 <}} + +### 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 >}} +2.0-SNAPSHOT +{{< /highlight >}} +2. Add Dependencies to pom.xml +{{< highlight xml >}} + + com.tmobile.opensource.casquatch + casquatch-driver-spring + ${casquatch.version} + + + com.tmobile.opensource.casquatch + casquatch-driver-tests + ${casquatch.version} + test + +{{< /highlight >}} +3. Configure Compiler Plugin +{{< highlight xml >}} + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + + + com.tmobile.opensource.casquatch + casquatch-driver-processor + ${casquatch.version} + + + + +{{< /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 >}} +2.9.2 +{{< /highlight >}} +2. Add Dependencies to pom.xml +{{< highlight xml >}} + + io.springfox + springfox-swagger2 + ${swagger.version} + + + io.springfox + springfox-swagger-ui + ${swagger.version} + +{{< /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 -```