This is a simple URL shortener application built using Spring Boot. The application allows you to shorten long URLs, store them in memory, and then redirect users to the original URL when they visit the shortened link.
Shorten URLs by providing a long URL. Retrieve the original URL from the shortened one. Base URL is configurable via application.properties. In-memory storage for URL mappings.
Java 11+ (or the latest version of JDK) Maven (for building the project) Spring Boot (for the framework) Setup
Clone the project to your local machine using the following command:
git clone https://github.com/your-username/url-shortener.git
Navigate to the project directory and build the project using Maven:
cd url-shortener
mvn clean install
In src/main/resources/application.properties
, configure the base.url
property. This will define the base URL for all shortened URLs.
base.url=http://localhost:8080/
You can modify this to use any custom URL (e.g., a production URL) later.
To run the Spring Boot application, use the following command:
mvn spring-boot:run
Your application will start on http://localhost:8080/
by default.
To shorten a URL, send a POST request with a parameter originalUrl
:
curl -X POST "http://localhost:8080/url/shorten?originalUrl=https://www.example.com"
The response will be a shortened URL:
http://localhost:8080/abcd12
To get redirected to the original URL, visit the shortened URL in your browser:
http://localhost:8080/abcd12
You will be redirected to https://www.example.com
.
- Shortening URLs: The
UrlService
generates a random alphanumeric string to create the shortened URL. This string is mapped to the original URL in an in-memoryMap
. - Redirecting to Original URL: When a user visits the shortened URL, the application looks up the original URL in the
Map
and redirects the user to it.
- Change the base URL: Modify the
base.url
property inapplication.properties
to change the base URL used for shortened links. - Persist URLs: This version stores URLs in memory. For persistence, you can integrate a database like MySQL or MongoDB.
For running this application in production, consider:
- Deploying it behind a reverse proxy (e.g., Nginx).
- Configuring HTTPS for secure communication.
- Integrating a persistent storage solution (database) for better scalability and reliability.
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Create a new Pull Request.
- Add a database to store the URLs
- Short and meaningful domain name