This is a REST API to shorten URLs. It accepts long URLs and optionally an expiration date in yyyy-MM-dd format to return a shortened URL. It
supports redirection to the original URL if the short URL is valid and not expired. The API includes Swagger
documentation and can be deployed easily using Docker Compose.
Key features:
- Input validation for shortening URL requests.
- Generates the base URL dynamically from the incoming HTTP request.
- Calculates expiration date for shortened URLs, defaulting to 24 hours if none provided.
- Redirects from short URL to the original URL.
- Handles expired and non-existent short URLs properly.
- Swagger UI documentation for all endpoints.
For Docker (Recommended):
- Docker & Docker Compose
For Local Development:
- Java 21+
- Maven 3.9+
- MongoDB
Using Docker, .env file is optional since default values exist in Docker Compose file. For
local development without Docker, you must
set MONGO_USER and MONGO_PASSWORD environment variables for MongoDB authentication. Other parameters have sensible
defaults.
For production or deployments on platforms like Render, AWS, or any service handling the deployment, you should set the
MONGO_URI environment variable in the deployment environment to ensure a proper MongoDB connection.
Rename .env.example to .env and modify variables according to your needs.
| Variable | For Docker | For Local Development | Default | Description |
|---|---|---|---|---|
| PORT | Optional | Optional | 8080 | Server port |
| MONGO_USER | Optional | Required | - | MongoDB username |
| MONGO_PASSWORD | Optional | Required | - | MongoDB password |
| MONGO_URI | Optional | Optional | Auto-built from other variables | Full MongoDB connection URI (set explicitly only in production/deployment) |
For the fastest setup, it is recommended to use Docker Compose to start the app and its services:
# Run docker compose command to start all services
$ docker compose up -d --buildAccess the application at http://localhost:8080/docs (or the port you configured).
| Route | HTTP Method | Params | Description | Auth Method |
|---|---|---|---|---|
/docs |
GET | - | Swagger documentation | None |
/shorten-url |
POST | Body with originalUrl and expirationDate(optional) |
Create a short URL from a long URL | None |
/{shortCode} |
GET | Path: :shortCode |
Redirects to the original URL if valid | None |
POST /shorten-url
Request body:
{
"originalUrl": "https://example.com/a/very/long/url",
"expirationDate": "2025-11-12"// optional, format: yyyy-MM-dd
}