Create simple url shortener. In first version it will be unauthenticated service which means that anyone can use API to shorted URL without registration.
Given a full URL, returns a short URL. Currently, the API only supports shortening a single URL per API call.
Method: GET
Request Params: longUrl
Response: Returns short url data on success
Example Call:
http://localhost/url/shorten?longUrl=https://www.imdb.com/title/tt8690918/episodes
Example Response:
{
"longUrl": "https://www.imdb.com/title/tt8690918/episodes",
"shortUrl": "http://localhost/u/924"
}
Given a short URL, returns the original full URL.
Method: GET
Request Params: shortUrl
Response: Returns original url data on success
Example Call:
http://localhost/url/expand?shortUrl=http://localhost/u/924
Example Response:
{
"longUrl": "https://www.imdb.com/title/tt8690918/episodes",
"shortUrl": "http://localhost/u/924"
}
Service must handle GET requests to /u/XXX
endpoint and redirect client to long url using HTTP 301 code. There are several redirection codes, but we should use 301 which is permanent redirect. It means user's browser will remember where to redirect next time user opens short url without calling our service.
- Build system: Gradle (how to create new project: https://docs.gradle.org/current/samples/sample_building_java_applications.html)
- RESTful API framework: Spring Boot. Looks like it's easier to create sample project using https://start.spring.io/ like this
- Use H2 local database to store information about URLs (it will be dependency in your Gradle script)
- Use Jdbi framework to connect and work with H2 DB (https://jdbi.org/).
- Use JUnit for unit-tests.
- Create skeleton Spring Boot project;
- Add two endpoints with hardcoded responses;
- Change endpoint handlers to generate shortened URLs but store them in local HashMap<String, String> object. Create such storage in separate class that implement your
UrlStore
interface; - For simplicity use increasing number for short URL (contrary to using hash values such as
8PSW
); - Implement H2 storage class that implements
UrlStore
interface from item 3 so that data can survive restart of your service;