An intelligent, full-stack crop recommendation platform that analyzes soil nutrients and climate data to deliver data-driven recommendations for optimal crop selection.
- Overview
- Features
- Architecture
- Tech Stack
- Project Structure
- Quick Start
- API Reference
- Default Users
- Screenshots
- Roadmap
- Contributing
- License
SmartCrop is a production-ready crop recommendation system built with Spring Boot that helps farmers make data-driven decisions. It analyzes soil parameters (Nitrogen, Phosphorus, Potassium, pH) along with real-time weather data to recommend the most suitable crop with a confidence score.
Key Highlights:
- π§ͺ Rule-based recommendation engine matching soil profiles to ideal crop ranges
- π€οΈ Real-time weather integration via OpenWeather API
- π Interactive dashboards with Chart.js visualizations
- π JWT-based authentication with role-based access control (Admin, Farmer, Officer)
- π³ Docker-ready for one-command deployment
| Feature | Description |
|---|---|
| Soil Analysis | Submit NPK, pH, and moisture data for intelligent crop matching |
| Weather Integration | Live weather data from OpenWeather API for climate-aware recommendations |
| Smart Recommendations | Algorithm matches soil + climate to crop ideal ranges with confidence scoring |
| Role-Based Dashboards | Farmer (NPK trends), Admin (crop analytics), Officer (weather trends) |
| JWT Authentication | Secure login with BCrypt passwords and HTTP-only cookie sessions |
| Modern Landing Page | Beautiful hero section with feature cards and gradient design |
| Responsive UI | Mobile-friendly dashboard built with Bootstrap 5 + custom CSS |
| RESTful API | Complete API for programmatic access to all features |
| Docker Support | One-command deployment with MySQL via Docker Compose |
graph TB
subgraph Client["π₯οΈ Client Layer"]
LP[Landing Page]
UI[Dashboard UI<br/>Thymeleaf + Bootstrap 5 + Chart.js]
API_CLIENT[REST API Clients]
end
subgraph Security["π Security Layer"]
JWT[JWT Authentication Filter]
RBAC[Role-Based Access Control]
end
subgraph Application["βοΈ Application Layer"]
WC[Web Controller]
AC[Auth Controller]
RC[Recommendation Controller]
SC[Soil Data Controller]
WEC[Weather Controller]
DC[Dashboard API Controller]
end
subgraph Service["π§ Service Layer"]
RS[Recommendation Service<br/>Rule-Based Engine]
US[User Service]
SS[Soil Data Service]
WS[Weather Service]
DS[Dashboard Service]
end
subgraph Data["πΎ Data Layer"]
REPO[JPA Repositories]
DB[(MySQL / H2)]
OW[OpenWeather API]
end
LP --> WC
UI --> DC
API_CLIENT --> AC
API_CLIENT --> RC
API_CLIENT --> SC
Client --> JWT --> RBAC --> Application
Application --> Service
RS --> REPO
SS --> REPO
DS --> REPO
WS --> OW
WS --> REPO
REPO --> DB
Data Flow:
- User submits soil data (NPK, pH, moisture) + optional location
- System fetches real-time weather if location provided
- Recommendation engine scores all crops against soil/climate parameters
- Best match returned with confidence score and persisted to database
- Dashboard visualizations update with new data
| Layer | Technology |
|---|---|
| Backend | Spring Boot 3.2, Spring Web, Spring Data JPA, Spring Security |
| Frontend | Thymeleaf, Bootstrap 5, Chart.js, Custom CSS |
| Database | MySQL 8.x (production) Β· H2 (demo mode) |
| Authentication | JWT + BCrypt, HTTP-only cookies |
| External API | OpenWeather API |
| Build | Maven 3.8+, Maven Wrapper |
| DevOps | Docker, Docker Compose |
| Language | Java 17 |
crop-recommendation-system/
βββ π README.md
βββ π LICENSE # MIT License
βββ π CONTRIBUTING.md # Contribution guidelines
βββ π CHANGELOG.md # Version history
βββ π .env.example # Environment template
βββ π .gitignore
βββ π³ Dockerfile
βββ π³ docker-compose.yml
βββ π pom.xml
βββ π run.ps1 # Quick start script
β
βββ src/main/java/com/crop/
β βββ π CropRecommendationApplication.java
β β
β βββ config/ # Configuration
β β βββ SecurityConfig.java # Spring Security + JWT setup
β β βββ WebClientConfig.java # WebClient for external APIs
β β βββ DataLoader.java # Database seeding
β β
β βββ controller/ # Request Handlers
β β βββ WebController.java # Thymeleaf page routes
β β βββ AuthController.java # Login/Register REST API
β β βββ RecommendationController.java
β β βββ SoilDataController.java
β β βββ WeatherController.java
β β βββ CropController.java
β β βββ DashboardApiController.java
β β
β βββ service/ # Business Logic
β β βββ RecommendationService.java # π§ Core recommendation engine
β β βββ UserService.java
β β βββ SoilDataService.java
β β βββ WeatherService.java
β β βββ DashboardService.java
β β
β βββ repository/ # Data Access
β β βββ CropRepository.java
β β βββ RecommendationRepository.java
β β βββ SoilDataRepository.java
β β βββ UserRepository.java
β β βββ WeatherDataRepository.java
β β
β βββ entity/ # Database Models
β β βββ User.java
β β βββ Crop.java
β β βββ Recommendation.java
β β βββ SoilData.java
β β βββ WeatherData.java
β β βββ enums/Role.java
β β
β βββ dto/ # Data Transfer Objects
β β βββ LoginRequest.java
β β βββ LoginResponse.java
β β βββ RegisterRequest.java
β β βββ RecommendationRequest.java
β β βββ RecommendationResponse.java
β β βββ SoilDataRequest.java
β β βββ WeatherResponse.java
β β βββ ApiError.java
β β
β βββ security/ # Authentication
β β βββ JwtAuthenticationFilter.java
β β βββ JwtUtil.java
β β βββ CustomUserDetailsService.java
β β
β βββ exception/ # Error Handling
β βββ GlobalExceptionHandler.java
β βββ BadRequestException.java
β βββ ResourceNotFoundException.java
β
βββ src/main/resources/
β βββ templates/ # Thymeleaf Views
β β βββ home.html # Landing page
β β βββ login.html
β β βββ register.html
β β βββ dashboard.html # Main dashboard
β βββ static/css/
β β βββ style.css # Custom styles
β βββ application.properties # MySQL config
β βββ application-demo.properties # H2 demo config
β
βββ database/
β βββ schema.sql # DDL + sample data
β
βββ docs/
βββ CNDC_Justification.md # Architecture decisions
# Production (MySQL required)
.\run.ps1
# Demo mode (no MySQL needed β uses H2 in-memory database)
.\run.ps1 -Demo# With MySQL
mvn spring-boot:run
# With H2 (demo)
mvn spring-boot:run -Dspring-boot.run.profiles=demomvn clean package -DskipTests
docker compose up -d --buildAccess: http://localhost:8080
| Requirement | For Production | For Demo |
|---|---|---|
| JDK 17+ | β Required | β Required |
| MySQL 8.x | β Required | β Not needed |
| Maven | β
Or use mvnw |
β
Or use mvnw |
| Docker | β Optional | β Optional |
-- Database is auto-created if URL has createDatabaseIfNotExist=true
-- Or create manually:
CREATE DATABASE IF NOT EXISTS crop_recommendation_db;Update src/main/resources/application.properties with your MySQL password:
spring.datasource.username=root
spring.datasource.password=YOUR_PASSWORDAll API endpoints require JWT authentication (except auth endpoints).
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/auth/login |
Login, returns JWT |
POST |
/api/auth/register |
Register new user |
| Method | Endpoint | Role | Description |
|---|---|---|---|
POST |
/api/recommendation |
FARMER+ | Get crop recommendation |
POST |
/api/soil |
FARMER+ | Submit soil data |
GET |
/api/soil |
FARMER+ | List user's soil data |
GET |
/api/weather/current?location=X |
ALL | Fetch & store weather |
| Method | Endpoint | Role | Description |
|---|---|---|---|
GET |
/api/dashboard/farmer |
FARMER | NPK trends + recommendations |
GET |
/api/dashboard/admin |
ADMIN | Most recommended crops |
GET |
/api/dashboard/officer?location=X |
OFFICER | Weather trends |
# 1. Login
TOKEN=$(curl -s -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"farmer@crop.com","password":"password123"}' | jq -r '.token')
# 2. Get recommendation
curl -X POST http://localhost:8080/api/recommendation \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"soilData": {
"nitrogen": 90, "phosphorus": 42,
"potassium": 43, "ph": 6.5, "moisture": 50
},
"location": "London"
}'Created automatically by DataLoader on first run (password: password123):
| Role | Dashboard | |
|---|---|---|
admin@crop.com |
ADMIN | Most recommended crops chart |
farmer@crop.com |
FARMER | NPK trends + recommendation history |
officer@crop.com |
OFFICER | Rainfall & temperature trends |
Landing Page β Modern hero section with feature cards
Dashboard β NPK input with validation, recommendation result with confidence bar, trend charts
Login β Clean authentication page
Screenshots coming soon β run the app to see the full UI!
- Core recommendation engine (rule-based)
- JWT authentication with role-based access
- Interactive dashboards with Chart.js
- OpenWeather API integration
- Modern landing page with feature cards
- Docker support
- H2 demo mode
- ML-based recommendation model (scikit-learn / TensorFlow)
- Crop disease detection module
- Multi-language support (i18n)
- Email notifications for recommendations
- AI recommendation module with historical data learning
- Analytics & reporting module
- Carbon credit tracking
- Admin panel with user management UI
- IoT sensor integration (MQTT)
- Mobile app (React Native / Flutter)
com.crop/
βββ recommendation/ # AI + rule-based engines
βββ analytics/ # Reports, trends, exports
βββ carbon/ # Carbon credit tracking
βββ admin/ # Admin panel controllers
βββ notification/ # Email, SMS, push
βββ iot/ # Sensor data ingestion
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.
This project is licensed under the MIT License β see the LICENSE file for details.
- Spring Boot β Backend framework
- OpenWeather API β Weather data provider
- Chart.js β Dashboard visualizations
- Bootstrap 5 β UI components
Made with β€οΈ for smarter agriculture