This is a Spring Boot 3.x application that provides a REST API service to retrieve user details based on email ID. The service implements case-insensitive email lookup, email format validation, and comprehensive error handling.
- ✅ REST API endpoint to retrieve user by email ID
- ✅ Case-insensitive email lookup
- ✅ Email format validation
- ✅ Comprehensive error handling with meaningful error messages
- ✅ Global exception handling using @ControllerAdvice
- ✅ Logging using SLF4J
- ✅ Unit tests for Service and Controller layers
- ✅ Integration tests
- ✅ H2 in-memory database for testing
- ✅ Clean code architecture with proper package structure
- Java: 17
- Spring Boot: 3.2.0
- Spring Data JPA: For database operations
- Spring Validation: For input validation
- H2 Database: In-memory database for testing
- Lombok: To reduce boilerplate code
- JUnit 5: For unit and integration testing
- Mockito: For mocking in tests
- Maven: Build tool
user-service/
├── src/main/java/com/example/userservice/
│ ├── UserServiceApplication.java # Main application class
│ ├── controller/
│ │ └── UserController.java # REST API endpoints
│ ├── service/
│ │ └── UserService.java # Business logic
│ ├── repository/
│ │ └── UserRepository.java # Database operations
│ ├── model/
│ │ └── User.java # User entity
│ └── exception/
│ ├── UserNotFoundException.java # Custom exception
│ ├── InvalidEmailException.java # Custom exception
│ ├── GlobalExceptionHandler.java # Global exception handler
│ └── ErrorResponse.java # Error response model
├── src/main/resources/
│ └── application.properties # Application configuration
└── src/test/java/com/example/userservice/
├── controller/
│ └── UserControllerTest.java # Controller unit tests
├── service/
│ └── UserServiceTest.java # Service unit tests
└── integration/
└── UserServiceIntegrationTest.java # Integration tests
- Java 17 or higher
- Maven 3.6 or higher
git clone <repository-url>
cd user-servicemvn clean installmvn testmvn spring-boot:runAlternatively, you can run the JAR file:
java -jar target/user-service-1.0.0.jarThe application will start on http://localhost:8080
Endpoint: GET /api/v1/users?email={email}
Example Request:
curl -X GET "http://localhost:8080/api/v1/users?email=test@example.com"Success Response (200 OK):
{
"id": 1,
"email": "test@example.com",
"name": "Test User",
"phone": "1234567890",
"address": "123 Test St",
"city": "Test City",
"state": "Test State",
"zipCode": "12345",
"country": "Test Country"
}Endpoint: GET /api/v1/users/{email}
Example Request:
curl -X GET "http://localhost:8080/api/v1/users/test@example.com"Success Response (200 OK):
{
"id": 1,
"email": "test@example.com",
"name": "Test User",
"phone": "1234567890",
"address": "123 Test St",
"city": "Test City",
"state": "Test State",
"zipCode": "12345",
"country": "Test Country"
}{
"timestamp": "2024-01-15T10:30:00",
"status": 404,
"error": "Not Found",
"message": "User not found with email: notfound@example.com",
"path": "/api/v1/users"
}{
"timestamp": "2024-01-15T10:30:00",
"status": 400,
"error": "Bad Request",
"message": "Invalid email format: invalid-email",
"path": "/api/v1/users"
}{
"timestamp": "2024-01-15T10:30:00",
"status": 400,
"error": "Bad Request",
"message": "email: Email should be valid",
"path": "/api/v1/users"
}mvn testmvn test -Dtest=UserServiceTestmvn verifyThe project includes:
- Unit Tests: Testing individual components (Service, Controller)
- Integration Tests: Testing the complete flow from API to database
- Test Cases:
- Valid email lookup
- Case-insensitive email lookup
- User not found scenario
- Invalid email format
- Null/empty email validation
For development and testing, you can access the H2 database console:
URL: http://localhost:8080/h2-console
Connection Details:
- JDBC URL:
jdbc:h2:mem:userdb - Username:
sa - Password: (leave empty)
The application uses H2 in-memory database by default. To use a different database, update application.properties:
# For MySQL
spring.datasource.url=jdbc:mysql://localhost:3306/userdb
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
# For PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/userdb
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialectLogging levels can be adjusted in application.properties:
logging.level.root=INFO
logging.level.com.example.userservice=DEBUG- Email validation is performed using regex pattern
- Input validation using Jakarta Bean Validation
- SQL injection prevention through JPA/Hibernate
- Proper error handling without exposing sensitive information
- ✅ Clean code architecture with separation of concerns
- ✅ Proper package structure (controller, service, repository, model, exception)
- ✅ Use of DTOs and proper layering
- ✅ Comprehensive logging
- ✅ Global exception handling
- ✅ Input validation
- ✅ Unit and integration testing
- ✅ Use of Lombok to reduce boilerplate
- ✅ RESTful API design with proper HTTP status codes
- ✅ Documentation and comments
- Add authentication and authorization (Spring Security)
- Implement pagination for bulk user retrieval
- Add caching (Redis/Caffeine)
- Add API documentation (Swagger/OpenAPI)
- Implement rate limiting
- Add monitoring and metrics (Actuator, Prometheus)
- Implement audit logging
- Add Docker support
If port 8080 is already in use, change it in application.properties:
server.port=8081Ensure you have Java 17 and Maven installed:
java -version
mvn -versionClean and rebuild:
mvn clean install -UThis project is licensed under the MIT License.
For questions or support, please contact the development team.
Issue Key: SCRUM-6
Summary: Create service to retrieve user details by email ID
Status: To Do
Priority: Medium
Components: User Management, Backend Services
Labels: enhancement
Link: https://aravindank.atlassian.net/browse/SCRUM-6