https://github.com/thanthtooaung-coding/url-shortener
Url Shortener with Spring Boot
https://github.com/thanthtooaung-coding/url-shortener
Last synced: about 1 year ago
JSON representation
Url Shortener with Spring Boot
- Host: GitHub
- URL: https://github.com/thanthtooaung-coding/url-shortener
- Owner: thanthtooaung-coding
- License: mit
- Created: 2025-01-26T02:56:43.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-26T03:34:22.000Z (over 1 year ago)
- Last Synced: 2025-02-02T05:28:21.500Z (over 1 year ago)
- Language: Java
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# URL Shortener with Spring Boot
## Overview
This is a URL Shortener application built using **Spring Boot**, following a clean software architecture with best practices such as Layered Architecture, SOLID principles, and proper exception handling.
## Features
- Shorten long URLs into unique short URLs.
- Retrieve original URLs using shortened versions.
- Basic validation and error handling.
- H2 in-memory database for development.
- RESTful API with structured responses.
---
## Technologies Used
- **Java 21**
- **Spring Boot 3.4.2**
- Spring Web
- Spring Data JPA
- H2 Database
- Spring Security
- **Maven**
- **Lombok**
---
## Project Structure
```
url.shortener/
│-- src/main/java/com/url/shortener
│ ├── UrlShortenerApplication.java
│ ├── config/
│ │ ├── SecurityConfig.java
│ ├── controller/
│ │ ├── UrlShortenerController.java
│ ├── service/
│ │ ├── UrlShortenerService.java
│ │ ├── impl/
│ │ │ ├── UrlShortenerServiceImpl.java
│ ├── repository/
│ │ ├── UrlRepository.java
│ ├── domain/
│ │ ├── Url.java
│ ├── dto/
│ │ ├── UrlRequestDto.java
│ │ ├── UrlResponseDto.java
│ ├── exception/
│ │ ├── UrlNotFoundException.java
│ │ ├── GlobalExceptionHandler.java
│ ├── util/
│ │ ├── HashingUtil.java
│-- src/main/resources
│ ├── application.yml
└── pom.xml
```
---
## Installation and Setup
### Prerequisites
- Install **Java 21**
- Install **Maven**
### Steps to Run the Application
1. Clone the repository:
```bash
git clone https://github.com/thanthtooaung-coding/url-shortener.git
cd url-shortener
```
2. Build the project:
```bash
mvn clean install
```
3. Run the application:
```bash
mvn spring-boot:run
```
4. Access the API via:
- H2 Console: `http://localhost:8080/h2-console` (JDBC URL: `jdbc:h2:mem:urlshortenerdb`)
---
## API Endpoints
### 1. Shorten URL
**Endpoint:**
```
POST /api/urls/shorten
```
**Request Body:**
```json
{
"originalUrl": "https://www.example.com"
}
```
**Response:**
```json
{
"shortUrl": "abc123"
}
```
### 2. Retrieve Original URL
**Endpoint:**
```
GET /api/urls/{shortUrl}
```
**Response:**
```json
"https://www.example.com"
```
### 3. Invalid URL Handling
**Response:**
```json
{
"message": "Shortened URL not found"
}
```
---
## Configuration
**`application.yml` Example:**
```yaml
server:
port: 8080
spring:
datasource:
url: jdbc:h2:mem:urlshortenerdb
driver-class-name: org.h2.Driver
username: sa
password:
jpa:
hibernate:
ddl-auto: update
show-sql: true
h2:
console:
enabled: true
path: /h2-console
logging:
level:
root: INFO
org.springframework: DEBUG
shortener:
base-url: http://localhost:8080/
hash-length: 8
```
---
## Testing
To test the application, you can use:
- **Postman**: Create API requests to shorten and retrieve URLs.
- **cURL**: Example usage:
```bash
curl -X POST http://localhost:8080/api/urls/shorten -H "Content-Type: application/json" -d '{"originalUrl":"https://www.example.com"}'
```
---
## Future Improvements
- Integrate with Swagger UI for API documentation.
- Add Redis caching for better performance.
- Implement custom expiration for shortened URLs.
- Develop a frontend using Vue.js or React.
- Add user authentication for personalized URL tracking.
---
## License
This project is licensed under the [MIT License](LICENSE).
---
## Contact
For any queries, please contact [thanthtoo1285@gmail.com](mailto:thanthtoo1285@gmail.com).