The Green Shadow Backend project is a robust and scalable server-side application developed to support the Green Shadow system. It provides APIs and services to manage dashboards for Manager, Administrator, and Scientist roles. The system allows for CRUD operations and detailed management of crops, equipment, vehicles, staff, fields, monitoring logs, and related entities.
This backend uses modern frameworks and tools, ensuring security, scalability, and efficient data handling.
- Role-Based Access Control: Secure endpoints for Manager, Administrator, and Scientist roles.
- CRUD Operations: Manage entities like crops, equipment, vehicles, staff, and logs.
- Validation: Ensures data integrity using Spring Validation.
- Secure Authentication: JWT-based authentication for user login and role management.
- Monitoring and Logging: Log requests and responses using Logback for detailed monitoring.
- Efficient Mapping: Model mapping with ModelMapper for DTO and entity conversions.
- Technologies Used
- Project Structure
- Setup and Installation
- API Endpoints
- API Testing
- Usage
- Contributing
- License
- Contact
The backend is built using the following tools and frameworks:
- Spring Boot: Backend framework for microservice development.
- Spring Data JPA: For seamless interaction with the MySQL database.
- Spring Web MVC: To handle RESTful web services.
- Spring Validation: Data validation at the API level.
- Spring Security: For securing APIs with JWT.
- Lombok: Simplifies boilerplate code with annotations.
- ModelMapper: For DTO to entity and entity to DTO conversions.
- Jackson: For JSON serialization and deserialization.
- MySQL: Database management system.
- JWT: JSON Web Tokens for secure authentication.
- Logback: For application logging.
The backend follows a modular structure for clarity and maintainability. Key packages include:
-
config
:
Contains configuration classes such as security filters, JWT configurations, and logging filters. These classes are responsible for setting up security mechanisms (e.g., JWT filters) and application-wide logging functionality. -
controller
:
Houses API endpoints to handle HTTP requests and responses. These endpoints manage user interactions and perform CRUD operations for various entities. -
service
:
Implements the business logic of the application. These classes act as intermediaries between controllers and repositories to ensure clean separation of concerns. -
repository
:
Previously referred to asdao
(Data Access Objects). This package interacts directly with the database to fetch, save, update, or delete data. -
entity
:
Contains JPA entity classes that map to database tables. Each entity represents a specific table and its attributes correspond to table columns. -
dto
:
Data Transfer Objects used for exchanging data between layers. DTOs ensure that only required data is shared between the frontend and backend. -
exception
:
Includes custom exception classes and handlers to manage errors gracefully across the application, ensuring a better user experience and easier debugging. -
util
:
Utility classes offering common functionalities, such as data format conversions, validation utilities, and helper methods used across the application. -
customobject
:
Custom objects designed for specific features or unique utility purposes that don’t fit into other predefined categories.
- Java 17 (OpenJDK 17)
- Maven
- MySQL 8.0.33
- Apache Tomcat 10
-
Clone the repository - Backend
git clone https://github.com/ApsaraWitharana/AAD-CW-Green-Shadow-Backend.git
Clone the repository - Frontend
git clone https://github.com/ApsaraWitharana/AAD-CW-Green-Shadow-Frontend.git
-
Configure the database
- Create a MySQL database named
green_shadow_system
. - Update the
application.properties
file with your MySQL credentials:
spring.datasource.url = jdbc:mysql://localhost:3306/green_shadow_system?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false spring.datasource.username = root spring.datasource.password = password spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
- Create a MySQL database named
-
Build the project
mvn clean install
-
Deploy to Tomcat
- Ensure Tomcat is installed and running.
- Copy the generated WAR file from the
target
directory to the Tomcatwebapps
directory. - Restart Tomcat.
After installation, run the server by starting Tomcat. The API will be available at http://localhost:8080/
.
Here are the key API endpoints for the Green Shadow Backend:
- POST /api/auth/login: User login (JWT-based authentication).
- GET /api/crops: Retrieve all crops.
- POST /api/crops: Add a new crop.
- PUT /api/crops/{id}: Update crop details.
- DELETE /api/crops/{id}: Delete a crop.
- GET /api/equipment: Retrieve all equipment.
- POST /api/equipment: Add new equipment.
For a full list of available endpoints, please refer to the API Documentation.
You can test the Green Shadow Backend API using Postman. Here’s how you can get started:
-
Import Postman Collection
- Download the Postman collection from this link.
- Import the collection into Postman for easy access to all API requests.
-
Test Authentication
- To authenticate, send a POST request to
/api/auth/login
with the following body:{ "username": "your_username", "password": "your_password" }
- Copy the JWT token from the response and use it for authentication in subsequent requests by including it in the Authorization header as
Bearer {JWT_TOKEN}
.
- To authenticate, send a POST request to
-
Test CRUD Operations
- Use the available endpoints (e.g.,
/api/crops
,/api/equipment
) to test the various CRUD operations for managing resources in the system.
- Use the available endpoints (e.g.,
-
Check Logs
- You can view detailed logs for your requests in the backend application using Logback, which will help you debug any issues.
This project is licensed under the MIT License. See the LICENSE file for details.