URL Shortener

About

This simple project can shorten long URLs into a minified version of the URL so that it is easier to share. All shortened URLs are stored in an in-memory database (H2 Database).

How it works

A user submits a post request containing the URL to be shortened, if the request is valid, the application will generate a hash (with 5 characters) for that URL and save it in the database, then it will return the hash that can be used to resolve the token into the long URL again.

How is the hash generated?

See RandomWordService.

The algorithm is to use generate a word where each character is randomly selected from a sequence of alphanumeric characters. The total amount of unique hash is given by:

number of unique hash = t ^ p; where
t = number of characters available in the alphanumeric sequence
p = number of characters in the hash

For this application, t = 62 and p = 5, so:

62 ^ 5 = 916132832 

In case of a hash collision (i.e. a randomly generated hash is already in use), the application will try to generate a new hash up to 5 times. If the application doesn't succeed it will return a 500 HTTP error (See ShortenerService).

Built with

  • Apache Maven
  • Java 17
  • Spring Boot
  • Spring Webflux
  • Spring Retry
  • Spring Data JPA
  • H2 Database
  • Apache Commons Validator

How to build

mvn clean verify

# or 
# mvn clean install
# if you want the artefact to be installed in your local maven repository

How to run

mvn spring-boot:run

After running the above command, the application will be available on http://localhost:8080. The API can be browsed via swagger, it is available at http://localhost:8080/swagger-ui/index.html

API

The application exposes two endpoints:

Controller Operation HTTP Method Path Parameters Response
ShortenerController Create a new short URL POST /shortener CreateShortURLRequest CreateShortURLResponse
ShortenerController Resolve an existing short URL into the long version GET /shortener/{token} String ResolveShortURLResponse

Examples

Shorten a URL:

curl -X POST \
  http://localhost:8080/shortener \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
    "url": "www.google.com"
}'

Response:

{
    "originalUrl": "www.google.com",
    "shortUrl": "5myjt"
}

Resolve a short URL

curl -X GET \
  http://localhost:8080/shortener/5myjt \
  -H 'cache-control: no-cache'

Response:

{
    "originalUrl": "www.google.com",
    "shortUrl": "5myjt"
}