|
1 | | -# Spring Boot REST API: A Simple and Robust Microservice Template |
| 1 | +# Spring Boot REST API for Simple Data Management |
2 | 2 |
|
3 | | -This Spring Boot application provides a lightweight REST API template with built-in health monitoring and data management capabilities. It offers a production-ready foundation for building microservices with comprehensive health checks, status monitoring, and data operations. |
| 3 | +This Spring Boot application provides a lightweight REST API for managing data items with a simple key-value storage mechanism. It offers a clean and efficient way to store and retrieve data items through RESTful endpoints with built-in monitoring capabilities via Spring Actuator. |
4 | 4 |
|
5 | | -The application implements a RESTful service with health monitoring endpoints and a flexible data management interface. It features configurable logging levels, actuator endpoints for operational insights, and a clean architecture that separates concerns between controllers and data models. Built with Spring Boot 3.2.0, it leverages modern Java 17 features and includes comprehensive test support through JUnit. |
| 5 | +The application implements a RESTful service with in-memory storage, making it ideal for prototyping, testing, or scenarios requiring temporary data persistence. It features comprehensive logging configuration, health monitoring through Spring Actuator, and follows Spring Boot best practices for configuration management. The service is built using Java 17 and managed with Gradle, ensuring modern Java features and reliable dependency management. |
6 | 6 |
|
7 | 7 | ## Repository Structure |
8 | 8 | ``` |
9 | 9 | . |
10 | | -├── build.gradle # Gradle build configuration with Spring Boot 3.2.0 and dependencies |
| 10 | +├── build.gradle # Gradle build configuration with Spring Boot dependencies |
11 | 11 | ├── config/ |
12 | | -│ └── application-local.yml # Local environment configuration (port, app name, logging) |
| 12 | +│ └── application-local.yml # Local environment configuration (port, logging, app name) |
13 | 13 | └── src/ |
14 | 14 | └── com/example/ |
15 | | - ├── App.java # Main application entry point with Spring Boot configuration |
16 | | - ├── controller/ # REST API endpoint definitions |
17 | | - │ ├── HealthController.java # Health and status monitoring endpoints |
18 | | - │ └── SampleController.java # Data management endpoints |
| 15 | + ├── App.java # Main application entry point with Spring Boot configuration |
| 16 | + ├── controller/ |
| 17 | + │ └── SampleController.java # REST endpoints for data management |
19 | 18 | └── model/ |
20 | | - └── DataItem.java # Data model for API operations |
| 19 | + └── DataItem.java # Data model class for storing items |
21 | 20 | ``` |
22 | 21 |
|
23 | 22 | ## Usage Instructions |
24 | 23 |
|
25 | 24 | ### Prerequisites |
26 | 25 | - Java Development Kit (JDK) 17 or higher |
27 | | -- Gradle 8.x or higher |
28 | | -- Basic understanding of Spring Boot and REST APIs |
| 26 | +- Gradle 8.x (or use the included Gradle wrapper) |
29 | 27 |
|
30 | | - |
31 | | -### Quick Start |
32 | | -1. Start the application: |
33 | | -```bash |
34 | | -./gradlew bootRun |
35 | | -``` |
36 | | - |
37 | | -2. Verify the application is running by accessing the health endpoint: |
38 | | -```bash |
39 | | -curl http://localhost:8080/api/health |
40 | | -``` |
| 28 | +2. The application will be available at `http://localhost:8080` |
41 | 29 |
|
42 | 30 | ### More Detailed Examples |
43 | 31 |
|
44 | | -1. Check Application Status |
| 32 | +#### Storing a Data Item |
45 | 33 | ```bash |
46 | | -curl http://localhost:8080/api/status |
| 34 | +curl -X PUT http://localhost:8080/api/data/123 \ |
| 35 | + -H "Content-Type: application/json" \ |
| 36 | + -d '{"content": "Sample content"}' |
47 | 37 | ``` |
| 38 | + |
48 | 39 | Expected response: |
49 | 40 | ```json |
50 | 41 | { |
51 | | - "timestamp": "2024-01-01T12:00:00", |
52 | | - "service": "sample-rest-app", |
53 | | - "status": "running" |
| 42 | + "id": "123", |
| 43 | + "content": "Sample content" |
54 | 44 | } |
55 | 45 | ``` |
56 | 46 |
|
57 | | -2. Health Check |
| 47 | +#### Retrieving a Data Item |
58 | 48 | ```bash |
59 | | -curl http://localhost:8080/api/health |
| 49 | +curl http://localhost:8080/api/data/123 |
60 | 50 | ``` |
| 51 | + |
61 | 52 | Expected response: |
62 | 53 | ```json |
63 | 54 | { |
64 | | - "status": "UP", |
65 | | - "message": "Service is healthy" |
| 55 | + "id": "123", |
| 56 | + "content": "Sample content" |
66 | 57 | } |
67 | 58 | ``` |
68 | 59 |
|
69 | 60 | ### Troubleshooting |
70 | 61 |
|
71 | | -1. Application Won't Start |
72 | | -- **Problem**: Application fails to start with port binding issues |
73 | | -- **Solution**: |
74 | | - ```bash |
75 | | - # Check if port 8080 is already in use |
76 | | - lsof -i :8080 |
77 | | - # Modify port in config/application-local.yml if needed |
78 | | - ``` |
79 | | - |
80 | | -2. Debugging |
81 | | -- Enable debug logging by modifying `config/application-local.yml`: |
82 | | - ```yaml |
83 | | - logging: |
84 | | - level: |
85 | | - com.example: DEBUG |
86 | | - ``` |
87 | | -- Check logs in console output for detailed information |
88 | | -- Use Spring Boot Actuator endpoints for health monitoring: |
89 | | - ```bash |
90 | | - curl http://localhost:8080/actuator/health |
91 | | - ``` |
| 62 | +#### Common Issues |
| 63 | + |
| 64 | +1. Application fails to start |
| 65 | +- **Problem**: Port 8080 already in use |
| 66 | +- **Solution**: Modify the port in `config/application-local.yml`: |
| 67 | +```yaml |
| 68 | +server: |
| 69 | + port: 8081 |
| 70 | +``` |
| 71 | +
|
| 72 | +2. Logging issues |
| 73 | +- **Problem**: Insufficient logging information |
| 74 | +- **Solution**: Adjust logging levels in `application-local.yml`: |
| 75 | +```yaml |
| 76 | +logging: |
| 77 | + level: |
| 78 | + com.example: DEBUG |
| 79 | +``` |
| 80 | + |
| 81 | +#### Debugging |
| 82 | +- Enable debug logging by adding `--debug` flag: |
| 83 | +```bash |
| 84 | +./gradlew bootRun --debug |
| 85 | +``` |
| 86 | +- View logs in console output |
| 87 | +- Application logs are written to standard output and can be redirected to a file: |
| 88 | +```bash |
| 89 | +./gradlew bootRun > application.log |
| 90 | +``` |
92 | 91 |
|
93 | 92 | ## Data Flow |
94 | 93 |
|
95 | | -The application processes HTTP requests through a layered architecture, transforming REST calls into data operations with proper error handling and response formatting. |
| 94 | +The application implements a simple data flow where REST requests are processed through controllers and stored in an in-memory map structure. |
96 | 95 |
|
97 | 96 | ```ascii |
98 | | -Client Request → Controller Layer → Data Processing → Response |
99 | | - ↑ | | ↓ |
100 | | - └──────────────┴────────Error Handling─────────────┘ |
| 97 | +Client Request → REST Controller → In-Memory Storage |
| 98 | + ↑ | |
| 99 | + └────────────────────────────────────┘ |
| 100 | + Response with stored data |
101 | 101 | ``` |
102 | 102 |
|
103 | 103 | Key component interactions: |
104 | | -1. Controllers receive HTTP requests and validate inputs |
105 | | -2. Request data is mapped to internal data models |
106 | | -3. Business logic processes the data operations |
107 | | -4. Responses are formatted as JSON and returned to the client |
108 | | -5. Error handling is managed across all layers |
109 | | -6. Health monitoring provides real-time system status |
110 | | -7. Logging captures operation details at configurable levels |
| 104 | +1. REST requests are received by `SampleController` |
| 105 | +2. Controller methods handle GET and PUT operations |
| 106 | +3. Data is stored in an in-memory HashMap within the controller |
| 107 | +4. Responses are wrapped in Spring's ResponseEntity for proper HTTP status codes |
| 108 | +5. Spring Boot handles JSON serialization/deserialization automatically |
0 commit comments