Skip to content

Add Spring Boot Redis Sample Application #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions spring-boot-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
Here’s a README for the Product application you developed using Spring Boot and Redis:

```markdown
# Product Spring Application

This is a simple Product application built with Spring Boot and Redis. The application provides RESTful APIs to manage Product items.

## Prerequisites

- Java 8
- Maven
- Docker

## Installation

### 1. Clone the Repository

```sh
git clone https://github.com/yourusername/product-spring-app.git
cd product-spring-app
```

### 2. Build the Application

```sh
mvn clean install
```

### 3. Set Up Docker Containers

#### Redis Container

1. Pull the Redis Docker Image:

```sh
docker pull redis:latest
```

2. Run the Redis Container:

```sh
docker run --name redis-product -p 6379:6379 -d redis:latest
```

### 4. Configure Application Properties

Update the `src/main/resources/application.properties` file with your Redis settings:

```properties
spring.redis.host=localhost
spring.redis.port=6379
```

### 5. Run the Application

```sh
mvn spring-boot:run
```

## Usage

### API Endpoints

#### Create a Product Item

```sh
curl -X POST http://localhost:8080/api/products \
-H "Content-Type: application/json" \
-d '{
"name": "Sample Product",
"description": "This is a sample product description",
"price": 29.99
}'
```

#### Get All Product Items

```sh
curl -X GET http://localhost:8080/api/products
```

#### Get a Product Item by ID

```sh
curl -X GET http://localhost:8080/api/products/{id}
```
Replace `{id}` with the actual ID of the Product item you want to retrieve.

#### Update a Product Item

```sh
curl -X PUT http://localhost:8080/api/products/{id} \
-H "Content-Type: application/json" \
-d '{
"name": "Updated Product",
"description": "This is an updated product description",
"price": 39.99
}'
```
Replace `{id}` with the actual ID of the Product item you want to update.

#### Delete a Product Item

```sh
curl -X DELETE http://localhost:8080/api/products/{id}
```
Replace `{id}` with the actual ID of the Product item you want to delete.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
```

Feel free to adjust any details specific to your project, such as the repository link, product attributes, or additional instructions!
30 changes: 30 additions & 0 deletions spring-boot-redis/keploy/test-set-0/mocks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: api.keploy.io/v1beta1
kind: Mocks
name: product-mocks
spec:
mocks:
- req:
method: POST
url: http://localhost:8080/products
resp:
status_code: 201
header:
Content-Type: application/json
body: '{"id":"1","name":"Product A","price":10.99}'

- req:
method: GET
url: http://localhost:8080/products/1
resp:
status_code: 200
header:
Content-Type: application/json
body: '{"id":"1","name":"Product A","price":10.99}'

- req:
method: DELETE
url: http://localhost:8080/products/1
resp:
status_code: 204
header:
Content-Type: application/json
50 changes: 50 additions & 0 deletions spring-boot-redis/keploy/test-set-0/tests/test-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: api.keploy.io/v1beta1
kind: Http
name: test-1
spec:
metadata: {}
req:
method: POST
proto_major: 1
proto_minor: 1
url: http://localhost:8080/products
header:
Accept: '*/*'
Content-Length: "160"
Content-Type: application/json
Host: localhost:8080
User-Agent: curl/7.88.1
body: |-
{
"id": "1",
"name": "Product A",
"price": 10.99
}
timestamp: 2024-08-14T11:58:49.994804119+05:30
resp:
status_code: 201
header:
Content-Type: application/json
Date: Wed, 14 Aug 2024 06:28:51 GMT
body: '{"id":"1","name":"Product A","price":10.99}'
status_message: Created
proto_major: 0
proto_minor: 0
timestamp: 2024-08-14T11:58:53.957634007+05:30
objects: []
assertions:
noise:
header.Date: []
created: 1723616933
curl: |-
curl --request POST \
--url http://localhost:8080/products \
--header 'User-Agent: curl/7.88.1' \
--header 'Accept: */*' \
--header 'Content-Type: application/json' \
--header 'Host: localhost:8080' \
--data '{
"id": "1",
"name": "Product A",
"price": 10.99
}'
36 changes: 36 additions & 0 deletions spring-boot-redis/keploy/test-set-0/tests/test-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: api.keploy.io/v1beta1
kind: Http
name: test-2
spec:
metadata: {}
req:
method: GET
proto_major: 1
proto_minor: 1
url: http://localhost:8080/products/1
header:
Accept: '*/*'
Host: localhost:8080
User-Agent: curl/7.88.1
timestamp: 2024-08-14T11:58:49.994804119+05:30
resp:
status_code: 200
header:
Content-Type: application/json
Date: Wed, 14 Aug 2024 06:28:51 GMT
body: '{"id":"1","name":"Product A","price":10.99}'
status_message: OK
proto_major: 0
proto_minor: 0
timestamp: 2024-08-14T11:58:53.957634007+05:30
objects: []
assertions:
noise:
header.Date: []
created: 1723616933
curl: |-
curl --request GET \
--url http://localhost:8080/products/1 \
--header 'User-Agent: curl/7.88.1' \
--header 'Accept: */*' \
--header 'Host: localhost:8080'
Empty file.
59 changes: 59 additions & 0 deletions spring-boot-redis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>spring-boot-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-redis</name>
<description>Sample application using Spring Boot and Redis</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/>
</parent>

<properties>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.redis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootRedisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRedisApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.redis.controller;

import com.example.redis.model.Product;
import com.example.redis.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/products")
public class ProductController {

@Autowired
private ProductRepository productRepository;

@PostMapping
public Product createProduct(@RequestBody Product product) {
return productRepository.save(product);
}

@GetMapping("/{id}")
public Optional<Product> getProduct(@PathVariable String id) {
return productRepository.findById(id);
}

@GetMapping
public Iterable<Product> getAllProducts() {
return productRepository.findAll();
}

@PutMapping("/{id}")
public Product updateProduct(@PathVariable String id, @RequestBody Product product) {
if (!productRepository.existsById(id)) {
return null;
}
product.setId(id);
return productRepository.save(product);
}

@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable String id) {
productRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.redis.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

@RedisHash("products")
public class Product {
@Id
private String id;
private String name;
private double price;

public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.redis.repository;

import com.example.redis.model.Product;
import org.springframework.data.repository.CrudRepository;

public interface ProductRepository extends CrudRepository<Product, String> {
}
2 changes: 2 additions & 0 deletions spring-boot-redis/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spring.redis.host=localhost
spring.redis.port=6379