[go: nahoru, domu]

Skip to content

Commit

Permalink
add swagger docs to spring boot service
Browse files Browse the repository at this point in the history
  • Loading branch information
csplinter committed Jun 15, 2020
1 parent 4a014a2 commit 04a0502
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ This repository contains sample inventory microservices to demonstrate how to us
- [Frank Moley](https://github.com/fpmoles) - twitter handle [@fpmoles](https://twitter.com/fpmoles)

#### Modules:
- [`microservice-spring-boot`](microservice-spring-boot):
- [`microservice-spring-boot`](microservice-spring-boot): Service for Products
- **Persistence Layer** : uses Cassandra Java driver's `CqlSession` directly for queries to products table
- **Exposition Layer** : uses `spring-web` `@Controller`
- [`microservice-spring-data`](microservice-spring-data): Service for Orders table
- [`microservice-spring-data`](microservice-spring-data): Service for Orders
- **Persistence Layer** : uses Spring Data Cassandra for data access to orders table
- **Exposition Layer** : uses Spring Data Rest for API generation

Expand Down Expand Up @@ -69,8 +69,8 @@ kubectl -n cass-operator apply -f deploy/storage-class.yml
# apply the operator manifest
kubectl -n cass-operator apply -f https://raw.githubusercontent.com/DataStax-Academy/kubernetes-workshop-online/master/1-cassandra/11-install-cass-operator-v1.1.yaml
# start a single C* 4.0 pod
kubectl -n cass-operator apply -f deploy/cassandra-4.0.0-1node.yml
# start a single C* 4.0 node
kubectl -n cass-operator apply -f deploy/cassandra-4.0.0-1node.yml
```

Create the Kubernetes Secrets for database username and password
Expand Down Expand Up @@ -137,6 +137,8 @@ kubectl -n spring-data-service port-forward <spring-data-pod> 8081:8081
```

#### Spring Boot service endpoints
Explore the endpoints with Swagger: http://localhost:8083/swagger-ui.html

Add products
```
curl -X POST -H "Content-Type: application/json" -d '{"name": "mobile", "id":"123e4567-e89b-12d3-a456-556642440000", "description":"iPhone", "price":"500.00"}' http://localhost:8083/api/products/add
Expand Down Expand Up @@ -189,4 +191,4 @@ curl -X DELETE "http://localhost:8081/api/orders/delete/product-from-order?order
Delete order with order_id = 123e4567-e89b-12d3-a456-556642440000
```
curl -X DELETE "http://localhost:8081/api/orders/delete/order?orderId=123e4567-e89b-12d3-a456-556642440000"```
```
```
Binary file added doc/pics/spring-k8s-cassandra-small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/pics/swagger-spring-boot-service.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions microservice-spring-boot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ kubectl -n spring-boot-service get pods
kubectl -n spring-boot-service port-forward <pod-name> 8083:8083
```

Explore the endpoints with Swagger: http://localhost:8083/swagger-ui.html

Test the endpoints with curl

Add products
Expand Down
12 changes: 12 additions & 0 deletions microservice-spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-core</artifactId>
</dependency>

<!-- Swagger docs -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import java.net.InetSocketAddress;
import java.nio.file.Paths;

/* Configuration class for the Spring Boot microservice
Note: We could have used CassandraProperties here like we do in the Spring Data microservice.
Instead, we show a different technique of having application defined settings for the
database connection settings.
*/
@Configuration
public class SpringBootCassandraConfiguration {

Expand Down Expand Up @@ -50,6 +55,9 @@ public String getLocalDataCenter() {

@Bean
public CqlSessionBuilderCustomizer sessionBuilderCustomizer() {
/* When using DataStax Astra, we must pass the secure connect bundle to the CqlSession
See documentation: https://docs.datastax.com/en/astra/aws/doc/dscloud/astra/dscloudUsingDrivers.html
*/
if (!astraSecureConnectBundle.equals("none")) {
return builder -> builder
.withCloudSecureConnectBundle(Paths.get(this.astraSecureConnectBundle))
Expand All @@ -65,6 +73,9 @@ public CqlSessionBuilderCustomizer sessionBuilderCustomizer() {

@Bean
public DriverConfigLoaderBuilderCustomizer driverConfigLoaderBuilderCustomizer() {
/* When using DataStax Astra, we do not have to pass contact points like we normally would because
this metadata is contained in the secure connect bundle.
*/
if (!astraSecureConnectBundle.equals("none")) {
return builder -> builder.without(DefaultDriverOption.CONTACT_POINTS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.datastax.examples.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import java.util.List;
import java.util.UUID;

/* Data Access Object for Products
Note: We could have optionally used the Object Mapper distributed with the Cassandra Java driver
See documentation: https://docs.datastax.com/en/developer/java-driver/latest/manual/mapper/
*/
public class ProductDao {

private PreparedStatement insertProduct;
Expand Down

0 comments on commit 04a0502

Please sign in to comment.