How to Generate Rich Link Preview for Website Link / URL based on the Meta Tags Present in the Web Page with Spring Boot and Jsoup

What you’ll build

Home page

Enter URL for Rich Link Preview

Link Preview

Rich Link Preview

API for fetching link preview info by URL

API for fetching Rich Link Preview

What you’ll need

  • Spring Tool Suite 4
  • JDK 11

Tech Stack

  • Spring Boot 2.2
  • JSP and Bootstrap 4.1.3
  • JQuery 3.4.1
  • Jsoup 1.12.1

What is Rich Link Preview ?

Rich Link Preview allows you to see a peek of the link location and usually generated realtime by visiting the website, and reading the meta tags present in the web page.

Bootstrap Your Application

You can create your spring boot application with the web & devtools dependencies and download it from here

Project Structure

        |   \---com
        |       \---javachinna
        |           \---linkpreview
        |               |
        |               |   
        |               +---controller
        |               |
        |               |       
        |               \---model

Project Dependencies


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath /> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot</description>



		<!-- To compile JSP files -->
		<!-- -->

			<!-- <scope>runtime</scope> -->



Create Controller

public class PagesController {

	private final Logger logger = LogManager.getLogger(getClass());

	@GetMapping({ "/", "/home" })
	public ModelAndView home(@RequestParam(value = "view", required = false) String view) {"Entering home page");
		ModelAndView model = new ModelAndView("previewLink");
		model.addObject("title", "Home");
		model.addObject("view", view);
		return model;

	 * Fetches rich link preview info for the given URL based on the meta tags
	 * present in the web page
	 * @param url
	 * @return
	public Link getLinkPreviewInfo(@RequestParam(value = "url", required = true) String url) {
		Link link = null;
		try {
			link = extractLinkPreviewInfo(url);
		} catch (IOException e) {
			logger.error("Unable to connect to : {}", url);
		return link;

	 * Generates rich link preview for the given URL based on the meta tags present
	 * in the web page
	 * @param url
	 * @return
	public ModelAndView linkPreview(@RequestParam(value = "url", required = true) String url) {
		ModelAndView model = new ModelAndView("link");
		try {
			model.addObject("link", extractLinkPreviewInfo(url));
		} catch (IOException e) {
			logger.error("Unable to connect to : {}", url);
			model.addObject("css", "danger");
			model.addObject("msg", "Unable to connect to '" + url + "': " + e.getMessage());
		return model;

	 * Parses the web page and extracts the info from meta tags required for preview
	 * @param url
	 * @return
	 * @throws IOException
	private Link extractLinkPreviewInfo(String url) throws IOException {
		if (!url.startsWith("http")) {
			url = "http://" + url;
		Document document = Jsoup.connect(url).get();
		String title = getMetaTagContent(document, "meta[name=title]");
		String desc = getMetaTagContent(document, "meta[name=description]");
		String ogUrl = StringUtils.defaultIfBlank(getMetaTagContent(document, "meta[property=og:url]"), url);
		String ogTitle = getMetaTagContent(document, "meta[property=og:title]");
		String ogDesc = getMetaTagContent(document, "meta[property=og:description]");
		String ogImage = getMetaTagContent(document, "meta[property=og:image]");
		String ogImageAlt = getMetaTagContent(document, "meta[property=og:image:alt]");
		String domain = ogUrl;
		try {
			domain = InternetDomainName.from(new URL(ogUrl).getHost()).topPrivateDomain().toString();
		} catch (Exception e) {
			logger.warn("Unable to connect to extract domain name from : {}", url);
		return new Link(domain, url, StringUtils.defaultIfBlank(ogTitle, title), StringUtils.defaultIfBlank(ogDesc, desc), ogImage, ogImageAlt);

	 * Returns the given meta tag content
	 * @param document
	 * @return
	private String getMetaTagContent(Document document, String cssQuery) {
		Element elm =;
		if (elm != null) {
			return elm.attr("content");
		return "";

For simplicity, I’ve added the link preview info fetching logic in the controller itself instead of putting them into the service layer.

@RestController annotation is a convenience annotation that is itself annotated with @Controller and @ResponseBody . This annotation is applied to a class to mark it as a request handler. Spring RestController annotation is used to create RESTful web services using Spring MVC

@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. GET)

Note: The getLinkPreviewInfo() method is the REST service mapped with path /api/link/preview. So If you are just looking for a REST service which will return the preview info for the given URL as a JSON response, then you can get it by htting http://localhost:8080/api/link/preview?url=<website url>

Create Model Class

public class Link {

	private static final long serialVersionUID = -706243242873257798L;

	public Link() {

	public Link(String domain, String url, String title, String desc, String image, String imageAlt) {
		this.domain = domain;
		this.url = url;
		this.title = title;
		this.desc = desc;
		this.image = image;
		this.imageAlt = imageAlt;

	private String domain;

	private String url;

	private String title;

	private String desc;

	private String image;

	private String imageAlt;

	// Setters and Getters goes here

Create Spring Boot Application Class

@SpringBootApplication Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration@EnableAutoConfiguration and @ComponentScan

public class SpringBootLinkPreviewApplication {

	public static void main(String[] args) {, args);

Create View Pages


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri=""%>
<%@ taglib prefix="spring" uri=""%>
<%@ taglib uri="" prefix="form"%>
<!doctype html>
<html lang="en">
<link rel="stylesheet" href="" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
	<div class="container-fluid mt-2">
		<div class="row">
			<div class="col-12 col-md-5">
				<c:url value="/postLink" var="actionUrl" />
				<div class="form-group">
					<label for="title">Link </label> <input type="text" class="form-control" id="url" aria-describedby="url" placeholder="Enter URL here" />
				<div class="mb-2">
					<button class="btn btn-sm btn-dark" value="preview" onclick="preview()">Preview Link</button>
				<div class="form-group" id="link-preview"></div>
			<div class="col-md-7 d-none d-md-block"></div>
	<script >
		function preview() {
			var url = $('#url').val();
			if (url) {
				$.get('/link/preview?url=' + url, function(data, status) {


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri=""%>
	<c:when test="${empty msg}">
		<div class="card shadow mb-2">
			<div class="card-body">
				<h6 class="text-muted pb-2 m-0">
					<i class="fas fa-link"></i> Link Preview
				<a href="${link.url}" class="custom-card" target="_blank" data-image="${link.image}" data-image-alt="${link.imageAlt}" data-title="${link.title}"
					data-desc="${link.desc}" data-domain="${link.domain}">
					<div class="card">
						<img src="${link.image}" class="card-img-top" alt="${link.imageAlt}" style="max-height: 300px; object-fit: fill;">
						<div class="card-footer">
							<h5 class="card-title">${link.title}</h5>
							<p class="card-text">${link.desc}</p>
							<p class="card-text">
								<small class="text-muted">${link.domain}</small>
		<div class="alert alert-${css} alert-dismissible" role="alert">
			<button type="button" class="close" data-dismiss="alert" aria-label="Close">
				<span aria-hidden="true">×</span>

Create file


Run with Maven

You can run the application using mvn clean spring-boot:run and visit to http://localhost:8080

Source Code


That’s all folks! In this tutorial, you’ve learned how to get link preview info for a given url and generate the preview using spring boot application.

I hope you enjoyed this tutorial. Thank you for reading.

Leave a Reply