A comprehensive Spring Boot microservice template with modern best practices, including JPA, Security, Actuator, OpenAPI documentation, and Eureka client integration.
- Spring Boot 3.2.0 with Java 17
- Spring Data JPA for database operations
- Spring Security for authentication and authorization
- Spring Cloud with Eureka client for service discovery
- H2 Database for development (in-memory)
- Spring Boot Actuator for monitoring and metrics
- OpenAPI 3 (Swagger) for API documentation
- Validation with Bean Validation
- Global Exception Handling with consistent error responses
- Comprehensive Testing with JUnit 5 and MockMvc
- Maven for dependency management
- Java 17 or higher
- Maven 3.6 or higher
- IDE (IntelliJ IDEA, Eclipse, VS Code)
git clone <repository-url>
cd microservice
mvn clean installmvn spring-boot:runThe application will start on http://localhost:8080
- Main Application: http://localhost:8080
- Swagger UI: http://localhost:8080/swagger-ui.html
- API Documentation: http://localhost:8080/api-docs
- H2 Console: http://localhost:8080/h2-console
- Actuator Health: http://localhost:8080/actuator/health
| Method | Endpoint | Description | 
|---|---|---|
| GET | /api/users | Get all users | 
| GET | /api/users/{id} | Get user by ID | 
| GET | /api/users/username/{username} | Get user by username | 
| GET | /api/users/email/{email} | Get user by email | 
| GET | /api/users/search?name={name} | Search users by name | 
| GET | /api/users/created-after?startDate={date} | Get users created after date | 
| POST | /api/users | Create new user | 
| PUT | /api/users/{id} | Update user | 
| DELETE | /api/users/{id} | Delete user | 
| GET | /api/users/check/username/{username} | Check if username exists | 
| GET | /api/users/check/email/{email} | Check if email exists | 
curl -X POST http://localhost:8080/api/users \
  -H "Content-Type: application/json" \
  -d '{
    "username": "john_doe",
    "email": "john@example.com",
    "firstName": "John",
    "lastName": "Doe"
  }'curl -X GET http://localhost:8080/api/userscurl -X GET http://localhost:8080/api/users/1The main configuration is in src/main/resources/application.yml:
- Server Port: 8080
- Database: H2 in-memory database
- JPA: Hibernate with auto DDL
- Security: Basic authentication (admin/admin123)
- Actuator: Health, info, metrics endpoints exposed
- Eureka: Client configuration for service discovery
The application uses H2 in-memory database by default. To use a different database:
- Update the spring.datasourceproperties inapplication.yml
- Add the appropriate database driver dependency to pom.xml
- Update the JPA dialect configuration
The application includes basic security configuration:
- CSRF disabled for API endpoints
- H2 console access allowed
- Swagger UI and API docs access allowed
- Actuator endpoints access allowed
- All other endpoints require authentication
src/
├── main/
│   ├── java/com/example/microservice/
│   │   ├── MicroserviceApplication.java
│   │   ├── config/
│   │   │   └── SecurityConfig.java
│   │   ├── controller/
│   │   │   └── UserController.java
│   │   ├── dto/
│   │   │   └── UserDto.java
│   │   ├── entity/
│   │   │   └── User.java
│   │   ├── exception/
│   │   │   └── GlobalExceptionHandler.java
│   │   ├── repository/
│   │   │   └── UserRepository.java
│   │   └── service/
│   │       └── UserService.java
│   └── resources/
│       └── application.yml
└── test/
    └── java/com/example/microservice/
        ├── MicroserviceApplicationTests.java
        └── controller/
            └── UserControllerTest.java
mvn testmvn verifymvn jacoco:reportThe application includes Spring Boot Actuator for monitoring:
- Health Check: /actuator/health
- Application Info: /actuator/info
- Metrics: /actuator/metrics
- Prometheus Metrics: /actuator/prometheus
- Entity: Create JPA entity in entitypackage
- Repository: Create repository interface in repositorypackage
- DTO: Create DTO class in dtopackage
- Service: Create service class in servicepackage
- Controller: Create REST controller in controllerpackage
- Tests: Add corresponding test classes
- Follow Java naming conventions
- Use meaningful variable and method names
- Add proper JavaDoc comments
- Include unit tests for all business logic
- Use validation annotations for DTOs
Create a Dockerfile:
FROM openjdk:17-jdk-slim
COPY target/microservice-1.0.0.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]Build and run:
docker build -t microservice .
docker run -p 8080:8080 microserviceFor production deployment:
- Use external database (PostgreSQL, MySQL, etc.)
- Configure proper security settings
- Set up logging configuration
- Configure monitoring and alerting
- Use environment-specific configuration files
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License.
For questions and support, please open an issue in the repository.