Building a Java-Based Link Shortener with Database Integration

Photo by JJ Ying on Unsplash

Building a Java-Based Link Shortener with Database Integration


2 min read

Creating a link shortener with Java involves several components: a web application to handle requests, a database to store original links and keywords, and a mechanism to redirect users based on the provided short link. Below is a simplified explanation along with some code snippets and diagrams.


  1. Database Structure:

    Create a table to store mappings between short keywords and original URLs.

     CREATE TABLE url_mappings (
         keyword VARCHAR(255) UNIQUE,
         original_url VARCHAR(1024)
  2. Java Web Application (Using Spring Boot):

    You can use Spring Boot to create a simple web application.

     // Main Application Class
     public class LinkShortenerApplication {
         public static void main(String[] args) {
   , args);
     // Controller Class
     public class LinkController {
         private LinkService linkService;
         public ResponseEntity<Void> redirectToOriginalLink(@PathVariable String keyword, HttpServletResponse response) {
             String originalUrl = linkService.getOriginalUrl(keyword);
             if (originalUrl != null) {
                 response.setHeader("Location", originalUrl);
                 return new ResponseEntity<>(HttpStatus.FOUND);
             } else {
                 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
     // Service Class
     public class LinkService {
         private LinkRepository linkRepository;
         public String getOriginalUrl(String keyword) {
             LinkEntity linkEntity = linkRepository.findByKeyword(keyword);
             return (linkEntity != null) ? linkEntity.getOriginalUrl() : null;
     // Entity Class
     public class LinkEntity {
         @GeneratedValue(strategy = GenerationType.IDENTITY)
         private Long id;
         @Column(unique = true)
         private String keyword;
         @Column(length = 1024)
         private String originalUrl;
         // Getters and Setters
     // Repository Interface
     public interface LinkRepository extends JpaRepository<LinkEntity, Long> {
         LinkEntity findByKeyword(String keyword);


  1. Database Structure:

    • The url_mappings table has columns for the ID (primary key), keyword (unique), and original URL.
  2. Java Web Application:

    • The Spring Boot application has a controller (LinkController) with a method to handle requests starting with /short/{keyword}.

    • The LinkService retrieves the original URL based on the provided keyword using the LinkRepository.

    • If the original URL is found, a 302 Found HTTP response is returned, redirecting the user to the original URL. Otherwise, a 404 Not Found response is returned.

  3. Diagrams:

    • The database diagram illustrates the structure of the url_mappings table.

    • The request flow diagram shows how the web application handles requests, checks the database for the original URL based on the provided keyword, and responds accordingly.

Note: These diagrams are simplified and may not cover all aspects of a production-ready system. You may need to implement additional features such as link expiration, analytics, security measures, etc., based on your specific requirements.