A Spring Boot application that provides URL shortening functionality with RESTful APIs.
- Create short URLs from long URLs
- Custom short URL support
- URL expiration functionality
- Click tracking
- User-based URL management
- Automatic cleanup of expired URLs
- H2 database for development
- RESTful API endpoints
- Java 17 or higher
- Maven 3.6 or higher
- IDE (IntelliJ IDEA, Eclipse, or VS Code)
-
Go to https://start.spring.io/
-
Configure your project:
- Project: Maven
- Language: Java
- Spring Boot: 3.2.x
- Group: com.example
- Artifact: urlshortener
- Name: urlshortener
- Description: URL Shortening Service
- Package name: com.example.urlshortener
- Packaging: Jar
- Java: 17
-
Dependencies to add:
- Spring Web
- Spring Data JPA
- H2 Database
- Spring Boot DevTools
- Spring Boot Validation
- Spring Boot Actuator
-
Click "Generate" and download the project
-
Extract and open in your IDE
The project structure is already created in this workspace. You can run it directly.
# Navigate to project directory
cd urlshortener
# Clean and compile
mvn clean compile
# Run the application
mvn spring-boot:run- Open the project in your IDE
- Run the
UrlShortenerApplication.javaclass - The application will start on
http://localhost:8080
POST /api/v1/urls
Request Body:
{
"originalUrl": "https://www.example.com/very-long-url",
"customShortUrl": "custom123", // Optional
"expiresAt": "2024-12-31T23:59:59", // Optional
"createdBy": "user123" // Optional
}Response:
{
"shortUrl": "abc12345",
"originalUrl": "https://www.example.com/very-long-url",
"fullShortUrl": "http://localhost:8080/abc12345",
"createdAt": "2024-01-15T10:30:00",
"expiresAt": null,
"clickCount": 0,
"createdBy": "user123"
}GET /{shortUrl}
Redirects to the original URL and increments click count.
GET /api/v1/urls/{shortUrl}
Returns detailed information about a short URL.
GET /api/v1/urls
Returns all active URLs.
GET /api/v1/urls/user/{createdBy}
Returns all URLs created by a specific user.
DELETE /api/v1/urls/{shortUrl}
Deletes a specific short URL.
POST /api/v1/urls/cleanup
Removes all expired URLs from the database.
- URL:
http://localhost:8080/h2-console - JDBC URL:
jdbc:h2:mem:testdb - Username:
sa - Password:
password
To use PostgreSQL or MySQL in production, update application.properties:
# PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/urlshortener
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
# MySQL
spring.datasource.url=jdbc:mysql://localhost:3306/urlshortener
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialectKey configuration options in application.properties:
app.base-url: Base URL for generating short URLsapp.short-url-length: Length of generated short URLs (default: 8)server.port: Server port (default: 8080)
- Create a short URL:
curl -X POST http://localhost:8080/api/v1/urls \
-H "Content-Type: application/json" \
-d '{
"originalUrl": "https://www.google.com",
"createdBy": "testuser"
}'- Get URL information:
curl http://localhost:8080/api/v1/urls/abc12345- Get all URLs:
curl http://localhost:8080/api/v1/urls- Import the following collection or create requests manually
- Test each endpoint with appropriate request bodies
src/
├── main/
│ ├── java/
│ │ └── com/example/urlshortener/
│ │ ├── controller/
│ │ │ ├── UrlShortenerController.java
│ │ │ └── RedirectController.java
│ │ ├── dto/
│ │ │ ├── CreateUrlRequest.java
│ │ │ └── UrlResponse.java
│ │ ├── entity/
│ │ │ └── UrlMapping.java
│ │ ├── exception/
│ │ │ └── GlobalExceptionHandler.java
│ │ ├── repository/
│ │ │ └── UrlMappingRepository.java
│ │ ├── service/
│ │ │ └── UrlShortenerService.java
│ │ └── UrlShortenerApplication.java
│ └── resources/
│ └── application.properties
└── test/
└── java/
└── com/example/urlshortener/
└── UrlShortenerApplicationTests.java
- Health check:
http://localhost:8080/actuator/health - Application info:
http://localhost:8080/actuator/info - Metrics:
http://localhost:8080/actuator/metrics
For production deployment, consider adding:
-
Authentication & Authorization
- Spring Security
- JWT tokens
- OAuth2
-
Rate Limiting
- Implement rate limiting per IP/user
-
Input Validation
- URL validation
- Malicious URL detection
-
HTTPS
- Enable HTTPS in production
Create a Dockerfile:
FROM openjdk:17-jdk-slim
COPY target/urlshortener-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]Build and run:
mvn clean package
docker build -t urlshortener .
docker run -p 8080:8080 urlshortener- AWS: Deploy to EC2 or use AWS Elastic Beanstalk
- Google Cloud: Deploy to App Engine or Cloud Run
- Azure: Deploy to Azure App Service
- Port already in use: Change
server.portinapplication.properties - Database connection issues: Check database credentials and connectivity
- Validation errors: Ensure request body matches expected format
Check application logs for detailed error information:
tail -f logs/application.log- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License.