Skip to content

Commit d5cc0e5

Browse files
committed
Add product based sample of SpringBoot Redis
Signed-off-by: slashexx <[email protected]>
1 parent 6c22ff7 commit d5cc0e5

File tree

11 files changed

+373
-0
lines changed

11 files changed

+373
-0
lines changed

spring-boot-redis/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
Here’s a README for the Product application you developed using Spring Boot and Redis:
2+
3+
```markdown
4+
# Product Spring Application
5+
6+
This is a simple Product application built with Spring Boot and Redis. The application provides RESTful APIs to manage Product items.
7+
8+
## Prerequisites
9+
10+
- Java 8
11+
- Maven
12+
- Docker
13+
14+
## Installation
15+
16+
### 1. Clone the Repository
17+
18+
```sh
19+
git clone https://github.com/yourusername/product-spring-app.git
20+
cd product-spring-app
21+
```
22+
23+
### 2. Build the Application
24+
25+
```sh
26+
mvn clean install
27+
```
28+
29+
### 3. Set Up Docker Containers
30+
31+
#### Redis Container
32+
33+
1. Pull the Redis Docker Image:
34+
35+
```sh
36+
docker pull redis:latest
37+
```
38+
39+
2. Run the Redis Container:
40+
41+
```sh
42+
docker run --name redis-product -p 6379:6379 -d redis:latest
43+
```
44+
45+
### 4. Configure Application Properties
46+
47+
Update the `src/main/resources/application.properties` file with your Redis settings:
48+
49+
```properties
50+
spring.redis.host=localhost
51+
spring.redis.port=6379
52+
```
53+
54+
### 5. Run the Application
55+
56+
```sh
57+
mvn spring-boot:run
58+
```
59+
60+
## Usage
61+
62+
### API Endpoints
63+
64+
#### Create a Product Item
65+
66+
```sh
67+
curl -X POST http://localhost:8080/api/products \
68+
-H "Content-Type: application/json" \
69+
-d '{
70+
"name": "Sample Product",
71+
"description": "This is a sample product description",
72+
"price": 29.99
73+
}'
74+
```
75+
76+
#### Get All Product Items
77+
78+
```sh
79+
curl -X GET http://localhost:8080/api/products
80+
```
81+
82+
#### Get a Product Item by ID
83+
84+
```sh
85+
curl -X GET http://localhost:8080/api/products/{id}
86+
```
87+
Replace `{id}` with the actual ID of the Product item you want to retrieve.
88+
89+
#### Update a Product Item
90+
91+
```sh
92+
curl -X PUT http://localhost:8080/api/products/{id} \
93+
-H "Content-Type: application/json" \
94+
-d '{
95+
"name": "Updated Product",
96+
"description": "This is an updated product description",
97+
"price": 39.99
98+
}'
99+
```
100+
Replace `{id}` with the actual ID of the Product item you want to update.
101+
102+
#### Delete a Product Item
103+
104+
```sh
105+
curl -X DELETE http://localhost:8080/api/products/{id}
106+
```
107+
Replace `{id}` with the actual ID of the Product item you want to delete.
108+
109+
## License
110+
111+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
112+
```
113+
114+
Feel free to adjust any details specific to your project, such as the repository link, product attributes, or additional instructions!
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: api.keploy.io/v1beta1
2+
kind: Mocks
3+
name: product-mocks
4+
spec:
5+
mocks:
6+
- req:
7+
method: POST
8+
url: http://localhost:8080/products
9+
resp:
10+
status_code: 201
11+
header:
12+
Content-Type: application/json
13+
body: '{"id":"1","name":"Product A","price":10.99}'
14+
15+
- req:
16+
method: GET
17+
url: http://localhost:8080/products/1
18+
resp:
19+
status_code: 200
20+
header:
21+
Content-Type: application/json
22+
body: '{"id":"1","name":"Product A","price":10.99}'
23+
24+
- req:
25+
method: DELETE
26+
url: http://localhost:8080/products/1
27+
resp:
28+
status_code: 204
29+
header:
30+
Content-Type: application/json
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
version: api.keploy.io/v1beta1
2+
kind: Http
3+
name: test-1
4+
spec:
5+
metadata: {}
6+
req:
7+
method: POST
8+
proto_major: 1
9+
proto_minor: 1
10+
url: http://localhost:8080/products
11+
header:
12+
Accept: '*/*'
13+
Content-Length: "160"
14+
Content-Type: application/json
15+
Host: localhost:8080
16+
User-Agent: curl/7.88.1
17+
body: |-
18+
{
19+
"id": "1",
20+
"name": "Product A",
21+
"price": 10.99
22+
}
23+
timestamp: 2024-08-14T11:58:49.994804119+05:30
24+
resp:
25+
status_code: 201
26+
header:
27+
Content-Type: application/json
28+
Date: Wed, 14 Aug 2024 06:28:51 GMT
29+
body: '{"id":"1","name":"Product A","price":10.99}'
30+
status_message: Created
31+
proto_major: 0
32+
proto_minor: 0
33+
timestamp: 2024-08-14T11:58:53.957634007+05:30
34+
objects: []
35+
assertions:
36+
noise:
37+
header.Date: []
38+
created: 1723616933
39+
curl: |-
40+
curl --request POST \
41+
--url http://localhost:8080/products \
42+
--header 'User-Agent: curl/7.88.1' \
43+
--header 'Accept: */*' \
44+
--header 'Content-Type: application/json' \
45+
--header 'Host: localhost:8080' \
46+
--data '{
47+
"id": "1",
48+
"name": "Product A",
49+
"price": 10.99
50+
}'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
version: api.keploy.io/v1beta1
2+
kind: Http
3+
name: test-2
4+
spec:
5+
metadata: {}
6+
req:
7+
method: GET
8+
proto_major: 1
9+
proto_minor: 1
10+
url: http://localhost:8080/products/1
11+
header:
12+
Accept: '*/*'
13+
Host: localhost:8080
14+
User-Agent: curl/7.88.1
15+
timestamp: 2024-08-14T11:58:49.994804119+05:30
16+
resp:
17+
status_code: 200
18+
header:
19+
Content-Type: application/json
20+
Date: Wed, 14 Aug 2024 06:28:51 GMT
21+
body: '{"id":"1","name":"Product A","price":10.99}'
22+
status_message: OK
23+
proto_major: 0
24+
proto_minor: 0
25+
timestamp: 2024-08-14T11:58:53.957634007+05:30
26+
objects: []
27+
assertions:
28+
noise:
29+
header.Date: []
30+
created: 1723616933
31+
curl: |-
32+
curl --request GET \
33+
--url http://localhost:8080/products/1 \
34+
--header 'User-Agent: curl/7.88.1' \
35+
--header 'Accept: */*' \
36+
--header 'Host: localhost:8080'

spring-boot-redis/keploy/test-set-0/tests/test-3.yaml

Whitespace-only changes.

spring-boot-redis/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.example</groupId>
6+
<artifactId>spring-boot-redis</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-boot-redis</name>
11+
<description>Sample application using Spring Boot and Redis</description>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>2.6.7</version>
17+
<relativePath/>
18+
</parent>
19+
20+
<properties>
21+
<java.version>11</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-data-redis</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-devtools</artifactId>
36+
<scope>runtime</scope>
37+
<optional>true</optional>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.projectlombok</groupId>
41+
<artifactId>lombok</artifactId>
42+
<optional>true</optional>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.redis;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootRedisApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(SpringBootRedisApplication.class, args);
10+
}
11+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.redis.controller;
2+
3+
import com.example.redis.model.Product;
4+
import com.example.redis.repository.ProductRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.Optional;
9+
10+
@RestController
11+
@RequestMapping("/products")
12+
public class ProductController {
13+
14+
@Autowired
15+
private ProductRepository productRepository;
16+
17+
@PostMapping
18+
public Product createProduct(@RequestBody Product product) {
19+
return productRepository.save(product);
20+
}
21+
22+
@GetMapping("/{id}")
23+
public Optional<Product> getProduct(@PathVariable String id) {
24+
return productRepository.findById(id);
25+
}
26+
27+
@GetMapping
28+
public Iterable<Product> getAllProducts() {
29+
return productRepository.findAll();
30+
}
31+
32+
@PutMapping("/{id}")
33+
public Product updateProduct(@PathVariable String id, @RequestBody Product product) {
34+
if (!productRepository.existsById(id)) {
35+
return null;
36+
}
37+
product.setId(id);
38+
return productRepository.save(product);
39+
}
40+
41+
@DeleteMapping("/{id}")
42+
public void deleteProduct(@PathVariable String id) {
43+
productRepository.deleteById(id);
44+
}
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example.redis.model;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.redis.core.RedisHash;
5+
6+
@RedisHash("products")
7+
public class Product {
8+
@Id
9+
private String id;
10+
private String name;
11+
private double price;
12+
13+
public String getId() { return id; }
14+
public void setId(String id) { this.id = id; }
15+
public String getName() { return name; }
16+
public void setName(String name) { this.name = name; }
17+
public double getPrice() { return price; }
18+
public void setPrice(double price) { this.price = price; }
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.redis.repository;
2+
3+
import com.example.redis.model.Product;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
public interface ProductRepository extends CrudRepository<Product, String> {
7+
}

0 commit comments

Comments
 (0)