This project is a URL Shortener service built with Spring Boot and Java 17. It provides endpoints for generating shortened URLs and redirecting users to the original URLs.
- Shorten URLs with an optional expiration date.
- Allow users to specify a custom short URL (up to 20 characters).
- Automatically generate a unique short URL if no custom URL is provided.
- Redirect users to the original URL when they visit the shortened link.
- Java 17
- Maven
- MySQL Database
git clone https://github.com/PX201/url-shortener-api.git
cd url-shortener-api
Update the application.properties
file with your MySQL database configuration:
spring.datasource.url=jdbc:mysql://localhost:3306/your-database
spring.datasource.username=your-username
spring.datasource.password=your-password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
mvn clean install
mvn spring-boot:run
The application will start on http://localhost:8080
.
Endpoint: /shorten
Method: POST
Request Body: JSON
{
"originUrl": "https://www.example.com",
"expDate": "2024-12-31",
"customShortUrl": "mycustomurl"
}
Parameters:
originUrl
(required): The original URL to be shortened.expDate
(optional): Expiration date for the short URL. The default expiration date is two days after the creation.ShortUrl
(optional): Custom short URL (up to 20 characters). If not provided, a unique short URL will be generated.
Response: JSON
{
"id": 101,
"shortUrl": "http://localhost:8080/r/mycustomurl",
"originUrl": "https://www.example.com",
"expirationDate": "2024-12-31",
"createdAt": "2024-12-30",
"clickCount": 0
}
Endpoint: /r/{shortlink}
Method: GET
Description: Redirects to the original URL associated with the short link.
The project uses the following dependencies:
- Spring Boot Starter Data JPA
- Spring Boot Starter Web
- MySQL Connector/J
- Spring Boot Starter Test
- Spring Boot Starter Validation
- Apache Commons Codec
This project is licensed under the MIT License.