|
1 | | -# Spring Boot REST API for Simple Data Management |
| 1 | +# Spring Boot REST API Service with Health Monitoring and Data Management |
2 | 2 |
|
3 | | -This Spring Boot application provides a RESTful API for managing data items with in-memory storage. It offers a lightweight and efficient way to store and retrieve data items through HTTP endpoints, making it ideal for prototyping and development environments. |
| 3 | +This Spring Boot application provides a RESTful API service with health monitoring capabilities and simple data management functionality. It offers a robust foundation for building microservices with built-in health checks and a flexible data storage interface. |
4 | 4 |
|
5 | | -The application is built using Spring Boot 3.2.0 and Java 17, featuring a clean architecture with separate controller and model layers. It includes built-in monitoring capabilities through Spring Actuator and comprehensive logging configuration. The REST API supports basic CRUD operations with JSON payloads, making it easy to integrate with various client applications. |
| 5 | +The service implements health monitoring endpoints for infrastructure integration and a data management API for storing and retrieving items. Built with Spring Boot 3.2.0 and Java 17, it includes production-ready features through Spring Actuator and comprehensive testing support. The application uses an in-memory data store for demonstration purposes and can be easily extended for production use cases. |
6 | 6 |
|
7 | 7 | ## Repository Structure |
8 | 8 | ``` |
9 | | -. |
10 | | -├── build.gradle # Gradle build configuration with Spring Boot dependencies |
| 9 | +ui-tests-starter/tstData/qdoc/updateFlow/ |
| 10 | +├── build.gradle # Gradle build configuration with Spring Boot 3.2.0 dependencies |
11 | 11 | ├── config/ |
12 | | -│ └── application-local.yml # Local environment configuration (port, app name, logging) |
13 | | -└── src/ |
14 | | - └── com/example/ |
15 | | - ├── App.java # Main application entry point with Spring Boot configuration |
16 | | - ├── controller/ |
17 | | - │ └── SampleController.java # REST endpoints for data management |
18 | | - └── model/ |
19 | | - └── DataItem.java # Data model class for storing items |
| 12 | +│ └── application-local.yml # Local environment configuration with server and logging settings |
| 13 | +└── src/com/zetcode/tancode/ |
| 14 | + ├── App.java # Main Spring Boot application entry point |
| 15 | + ├── controller/ |
| 16 | + │ ├── HealthController.java # Health and status monitoring endpoints |
| 17 | + │ └── SampleController.java # Data management REST endpoints |
| 18 | + └── model/ |
| 19 | + └── DataItem.java # Data model for item storage |
20 | 20 | ``` |
21 | 21 |
|
22 | 22 | ## Usage Instructions |
23 | | - |
24 | 23 | ### Prerequisites |
25 | | -- Java Development Kit (JDK) 17 or higher |
26 | | -- Gradle 8.x (or use the included Gradle wrapper) |
27 | | - |
28 | | -### Installation |
29 | | -1. Clone the repository: |
30 | | -```bash |
31 | | -git clone <repository-url> |
32 | | -cd <repository-name> |
33 | | -``` |
| 24 | +- Java Development Kit (JDK) 17 or later |
| 25 | +- Gradle 7.x or later |
| 26 | +- Port 8080 available on the host machine |
34 | 27 |
|
35 | | -2. Build the application: |
36 | | -```bash |
37 | | -./gradlew build |
38 | | -``` |
39 | 28 |
|
40 | 29 | ### Quick Start |
41 | 30 | 1. Start the application: |
42 | 31 | ```bash |
43 | 32 | ./gradlew bootRun |
44 | 33 | ``` |
45 | 34 |
|
46 | | -2. The application will be available at `http://localhost:8080` |
| 35 | +2. Verify the service is running: |
| 36 | +```bash |
| 37 | +curl http://localhost:8080/api/health |
| 38 | +``` |
47 | 39 |
|
48 | 40 | ### More Detailed Examples |
49 | | - |
50 | | -#### Creating or Updating a Data Item |
| 41 | +1. Check service status: |
51 | 42 | ```bash |
52 | | -curl -X PUT http://localhost:8080/api/data/1 \ |
53 | | - -H "Content-Type: application/json" \ |
54 | | - -d '{"content": "Sample content"}' |
| 43 | +curl http://localhost:8080/api/status |
55 | 44 | ``` |
56 | | - |
57 | | -Response: |
| 45 | +Expected response: |
58 | 46 | ```json |
59 | 47 | { |
60 | | - "id": "1", |
61 | | - "content": "Sample content" |
| 48 | + "timestamp": "2024-01-01T12:00:00", |
| 49 | + "service": "sample-rest-app", |
| 50 | + "status": "running" |
62 | 51 | } |
63 | 52 | ``` |
64 | 53 |
|
65 | | -#### Retrieving a Data Item |
| 54 | +2. Store a data item: |
66 | 55 | ```bash |
67 | | -curl http://localhost:8080/api/data/1 |
| 56 | +curl -X PUT http://localhost:8080/api/data/1 \ |
| 57 | + -H "Content-Type: application/json" \ |
| 58 | + -d '{"content": "Sample content"}' |
68 | 59 | ``` |
69 | 60 |
|
70 | | -Response: |
71 | | -```json |
72 | | -{ |
73 | | - "id": "1", |
74 | | - "content": "Sample content" |
75 | | -} |
| 61 | +3. Retrieve a data item: |
| 62 | +```bash |
| 63 | +curl http://localhost:8080/api/data/1 |
76 | 64 | ``` |
77 | 65 |
|
78 | 66 | ### Troubleshooting |
79 | | - |
80 | | -#### Common Issues |
81 | | - |
82 | | -1. Application Fails to Start |
83 | | -- Problem: Port 8080 is already in use |
84 | | -- Error message: `Web server failed to start. Port 8080 was already in use.` |
85 | | -- Solution: Modify the port in `config/application-local.yml`: |
86 | | -```yaml |
87 | | -server: |
88 | | - port: 8081 |
89 | | -``` |
90 | | -
|
91 | | -2. Debugging |
92 | | -- Enable debug logging by modifying `config/application-local.yml`: |
93 | | -```yaml |
94 | | -logging: |
95 | | - level: |
96 | | - com.example: DEBUG |
| 67 | +1. Service Not Starting |
| 68 | +- Problem: Application fails to start |
| 69 | +- Diagnosis: |
| 70 | + * Check if port 8080 is already in use |
| 71 | + * Verify Java version with `java -version` |
| 72 | +- Solution: |
| 73 | + * Change port in `config/application-local.yml` |
| 74 | + * Update Java installation if needed |
| 75 | + |
| 76 | +2. Debug Mode |
| 77 | +- Enable debug logging: |
| 78 | + * Set `logging.level.com.example=DEBUG` in application-local.yml |
| 79 | +- Log location: Standard output when running locally |
| 80 | +- Monitor application logs: |
| 81 | +```bash |
| 82 | +tail -f logs/application.log |
97 | 83 | ``` |
98 | | -- Debug logs will be available in the console output |
99 | | -- Monitor actuator endpoints at `http://localhost:8080/actuator` for application health |
100 | 84 |
|
101 | 85 | ## Data Flow |
102 | | - |
103 | | -The application implements a simple data flow where REST requests are processed through controllers and stored in an in-memory map structure. |
| 86 | +The application processes REST requests through controllers, managing data items in an in-memory store while providing health monitoring capabilities. |
104 | 87 |
|
105 | 88 | ```ascii |
106 | | -Client Request → REST Controller → In-Memory Storage |
107 | | - ↑ | |
108 | | - └────────────────────────────────────┘ |
109 | | - Response with stored data |
| 89 | +Client Request |
| 90 | + │ |
| 91 | + ▼ |
| 92 | +[Spring Boot Server :8080] |
| 93 | + │ |
| 94 | + ├─── /api/health, /api/status |
| 95 | + │ │ |
| 96 | + │ HealthController |
| 97 | + │ |
| 98 | + └─── /api/data/{id} |
| 99 | + │ |
| 100 | + SampleController |
| 101 | + │ |
| 102 | + In-Memory Store |
110 | 103 | ``` |
111 | 104 |
|
112 | | -Component interactions: |
113 | | -1. Client sends HTTP requests to REST endpoints |
114 | | -2. SampleController handles request validation and routing |
115 | | -3. Data is stored in an in-memory HashMap within the controller |
116 | | -4. Responses are returned as JSON using Spring's ResponseEntity |
117 | | -5. Error handling is managed through HTTP status codes (200 OK, 404 Not Found) |
| 105 | +Component Interactions: |
| 106 | +1. REST endpoints receive HTTP requests on port 8080 |
| 107 | +2. HealthController provides system status and health information |
| 108 | +3. SampleController manages data items through GET and PUT operations |
| 109 | +4. DataItem objects are stored in an in-memory HashMap |
| 110 | +5. All responses are returned as JSON with appropriate HTTP status codes |
| 111 | +6. Health checks return UP status when service is operational |
| 112 | +7. Data operations are synchronized through Spring's request handling |
0 commit comments