Skip to content

A comprehensive Spring Boot microservice template with JPA, Security, Actuator, and OpenAPI documentation

Notifications You must be signed in to change notification settings

techbinos/spring-boot-microservice

Repository files navigation

Spring Boot Microservice

A comprehensive Spring Boot microservice template with modern best practices, including JPA, Security, Actuator, OpenAPI documentation, and Eureka client integration.

Features

  • 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

Prerequisites

  • Java 17 or higher
  • Maven 3.6 or higher
  • IDE (IntelliJ IDEA, Eclipse, VS Code)

Quick Start

1. Clone and Build

git clone <repository-url>
cd microservice
mvn clean install

2. Run the Application

mvn spring-boot:run

The application will start on http://localhost:8080

3. Access the Application

API Endpoints

User Management

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

Example API Usage

Create a User

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"
  }'

Get All Users

curl -X GET http://localhost:8080/api/users

Get User by ID

curl -X GET http://localhost:8080/api/users/1

Configuration

Application Properties

The 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

Database Configuration

The application uses H2 in-memory database by default. To use a different database:

  1. Update the spring.datasource properties in application.yml
  2. Add the appropriate database driver dependency to pom.xml
  3. Update the JPA dialect configuration

Security 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

Project Structure

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

Testing

Run Tests

mvn test

Run Integration Tests

mvn verify

Test Coverage

mvn jacoco:report

Monitoring and Health Checks

The application includes Spring Boot Actuator for monitoring:

  • Health Check: /actuator/health
  • Application Info: /actuator/info
  • Metrics: /actuator/metrics
  • Prometheus Metrics: /actuator/prometheus

Development

Adding New Features

  1. Entity: Create JPA entity in entity package
  2. Repository: Create repository interface in repository package
  3. DTO: Create DTO class in dto package
  4. Service: Create service class in service package
  5. Controller: Create REST controller in controller package
  6. Tests: Add corresponding test classes

Code Style

  • 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

Deployment

Docker

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 microservice

Production Configuration

For production deployment:

  1. Use external database (PostgreSQL, MySQL, etc.)
  2. Configure proper security settings
  3. Set up logging configuration
  4. Configure monitoring and alerting
  5. Use environment-specific configuration files

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

License

This project is licensed under the MIT License.

Support

For questions and support, please open an issue in the repository.

About

A comprehensive Spring Boot microservice template with JPA, Security, Actuator, and OpenAPI documentation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published