A ride-sharing backend built with Spring Boot 3 and Java 21 — stateless JWT authentication, versioned REST controllers, MongoDB persistence and centralised error handling.
A rider registers, logs in and requests a ride. A driver accepts it and marks it
complete. Every state change goes through a service layer that validates the
transition, and every failure comes back as a structured ApiError rather than a
stack trace.
| Method | Endpoint | Purpose |
|---|---|---|
POST |
/api/auth/register |
Create an account |
POST |
/api/auth/login |
Exchange credentials for a JWT |
POST |
/api/v1/rides |
Request a ride |
POST |
/api/v1/rides/{rideId}/accept |
Driver accepts a pending ride |
POST |
/api/v1/rides/{rideId}/complete |
Mark a ride complete |
GET |
/api/v1/user/rides |
The caller's ride history |
GET |
/api/v1/driver/rides/requests |
Open ride requests, drivers only |
All /api/v1/** routes need an Authorization: Bearer <token> header, and
access is role-scoped: /api/v1/rides/** is open to USER and DRIVER, while
/api/v1/driver/** requires DRIVER. Only /api/auth/** is public.
JwtFilter+SecurityConfig— a once-per-request filter validates the token and populates the security context; the app holds no session state, and authority checks are declared per route rather than scattered through handlers.- Versioned controllers (
controller/v1) — the URL carries the version so a v2 can land beside v1 without breaking existing clients. GlobalExceptionHandler—@RestControllerAdvicemapsNotFoundExceptionandBadRequestExceptiononto consistent JSON bodies.- DTOs at the boundary —
CreateRideDTOkeeps request shapes separate from the persistedRideandUserdocuments.
Java 21 · Spring Boot · Spring Security · MongoDB · jjwt · Maven
# MongoDB must be reachable (default: mongodb://localhost:27017)
./mvnw spring-boot:runThe API starts on http://localhost:8080.
Configure the datastore and signing key through the usual Spring properties —
spring.data.mongodb.uri and the JWT secret used by JwtUtil.
src/main/java/com/kunal/rideSharingApp/
├── config/ JwtFilter, SecurityConfig
├── controller/ AuthController + v1/{Ride,User,Driver}
├── dto/ request shapes
├── exception/ ApiError, GlobalExceptionHandler, typed exceptions
├── model/ Ride, User
├── repository/ Mongo repositories
├── service/ RideService, UserService
└── util/ JwtUtil