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.

Components:

  1. Database Structure:

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

     CREATE TABLE url_mappings (
         id INT AUTO_INCREMENT PRIMARY KEY,
         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
     @SpringBootApplication
     public class LinkShortenerApplication {
         public static void main(String[] args) {
             SpringApplication.run(LinkShortenerApplication.class, args);
         }
     }
    
     // Controller Class
     @RestController
     public class LinkController {
         @Autowired
         private LinkService linkService;
    
         @GetMapping("/short/{keyword}")
         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
     @Service
     public class LinkService {
         @Autowired
         private LinkRepository linkRepository;
    
         public String getOriginalUrl(String keyword) {
             LinkEntity linkEntity = linkRepository.findByKeyword(keyword);
             return (linkEntity != null) ? linkEntity.getOriginalUrl() : null;
         }
     }
    
     // Entity Class
     @Entity
     public class LinkEntity {
         @Id
         @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);
     }
    

Explanation:

  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.