A simple RESTful API based on the Spring Boot web app: a controller responsible for the resource named Users, implemented according to the best practices. It allows to perform various operations related to user management, including user creation, updating user details, deleting users, and searching for users by birth date range.
- Create a user with validation for age (configurable).
- Update user details either partially or for all fields.
- Delete user.
- Search for users within a specified birth date range (with the validation which checks that “From” is less than “To”). Returns a list of objects.
- Comprehensive unit tests for ensuring functionality and reliability.
- Code has error handling for REST.
- JSON format for API responses.
- Use of database is not necessary. The data persistence layer is not required.
To run this project, you need to have the following software installed on your machine:
- Java Development Kit (JDK)
- Maven (for building the project)
Clone this repository to your local machine using the following command:
git clone https://github.com/your-username/user-management-api.git
Navigate to the project directory:
cd users-resource-restful-app
Build the project using Maven:
mvn clean install
API provides various endpoints to interact with the system. Below are examples of how to use the API:
POST /users
Content-Type: application/json
{
"email": "[email protected]",
"firstName": "John",
"lastName": "Doe",
"birthDate": "1990-01-15"
}
HTTP 201 Created
{
"id": 1,
"email": "[email protected]",
"firstName": "John",
"lastName": "Doe",
"birthDate": "1990-01-15",
"address": null,
"phoneNumber": null
}
HTTP 400 Bad Request
{
"title": "BAD_REQUEST",
"status": 400,
"timestamp": "2023-10-04T12:00:00",
"message": "Validation failed",
"detail": "Age must be greater than 18",
"errors": [
"Age must be greater than 18"
],
"instance": "/users"
}
PUT /users/1
Content-Type: application/json
{
"email": "[email protected]",
"firstName": "Updated",
"lastName": "User",
"birthDate": "1990-01-01"
}
HTTP 200 OK
{
"id": 1,
"email": "[email protected]",
"firstName": "Updated",
"lastName": "User",
"birthDate": "1990-01-01",
"address": null,
"phoneNumber": null
}
HTTP 400 Bad Request
{
"title": "BAD_REQUEST",
"status": 400,
"timestamp": "2023-10-04T12:00:00",
"message": "Validation failed",
"detail": "Age must be greater than 18",
"errors": [
"Age must be greater than 18"
],
"instance": "/users/1"
}
DELETE /users/1
HTTP 202 Accepted
"User 1 deleted successfully"
HTTP 404 Not Found
{
"title": "NOT_FOUND",
"status": 404,
"timestamp": "2023-10-04T12:00:00",
"message": "Resource not found",
"detail": "User with ID 1 not found",
"instance": "/users/1"
}
GET /users/search?from=1990-01-01&to=2000-12-31
HTTP 200 OK
[
{
"id": 2,
"email": "[email protected]",
"firstName": "Alice",
"lastName": "Smith",
"birthDate": "1995-06-10",
"address": null,
"phoneNumber": null
},
{
"id": 3,
"email": "[email protected]",
"firstName": "Bob",
"lastName": "Johnson",
"birthDate": "1998-11-20",
"address": null,
"phoneNumber": null
}
]
HTTP 400 Bad Request
{
"title": "BAD_REQUEST",
"status": 400,
"timestamp": "2023-10-04T12:00:00",
"message": "Validation failed",
"detail": "Parameter 'from' must be less than 'to'",
"errors": [
"Parameter 'from' must be less than 'to'"
],
"instance": "/users/search"
}
Unit tests for the application can be executed using Maven. Run the following command from the project root directory:
mvn test