Spring Boot Open Graph Meta Tags: Add OG to Your Java App
How to add og:title, og:image, og:description, and Twitter card meta tags in a Spring Boot application using Thymeleaf templates or a headless REST API with a frontend.
Spring MVC + Thymeleaf
If your Spring Boot app renders HTML with Thymeleaf, pass OG metadata from your controller to the model and render it in the layout fragment:
// BlogController.java
@Controller
@RequestMapping("/blog")
public class BlogController {
@Autowired
private PostService postService;
@GetMapping("/{slug}")
public String blogPost(@PathVariable String slug, Model model) {
Post post = postService.findBySlug(slug);
if (post == null) return "error/404";
model.addAttribute("pageTitle", post.getTitle());
model.addAttribute("pageDescription", post.getExcerpt());
model.addAttribute("ogImage", "https://yourdomain.com/og/" + slug + ".png");
model.addAttribute("canonicalUrl", "https://yourdomain.com/blog/" + slug);
model.addAttribute("post", post);
return "blog/post";
}
}<!-- templates/fragments/head.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="head">
<meta charset="UTF-8" />
<title th:text="${pageTitle} + ' | MySite'">MySite</title>
<meta name="description" th:content="${pageDescription}">
<meta property="og:title" th:content="${pageTitle}">
<meta property="og:description" th:content="${pageDescription}">
<meta property="og:image" th:content="${ogImage}">
<meta property="og:url" th:content="${canonicalUrl}">
<meta property="og:type" content="article">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" th:content="${pageTitle}">
<meta name="twitter:description" th:content="${pageDescription}">
<meta name="twitter:image" th:content="${ogImage}">
<link rel="canonical" th:href="${canonicalUrl}">
</head>
</html><!-- templates/blog/post.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/head :: head}"></head>
<body>
<h1 th:text="${post.title}">Post Title</h1>
<p th:text="${post.excerpt}">Excerpt</p>
</body>
</html>Spring Boot as headless REST API
If Spring Boot is your backend REST API and you have a JavaScript frontend (React, Vue, Next.js), OG meta tags must live in the frontend's SSR layer — not in Spring Boot itself. Spring Boot's @RestController returns JSON which social scrapers cannot parse for OG tags.
For shareable links that must have OG tags from the Java layer, create a dedicated Spring MVC controller (not a @RestController) that returns a minimal HTML page with OG tags:
// ShareController.java — minimal HTML page with OG tags for social crawlers
@Controller
@RequestMapping("/share")
public class ShareController {
@Autowired
private PostService postService;
@GetMapping("/blog/{slug}")
@ResponseBody
public ResponseEntity<String> shareBlogPost(@PathVariable String slug) {
Post post = postService.findBySlug(slug);
if (post == null) return ResponseEntity.notFound().build();
String html = """
<!DOCTYPE html>
<html>
<head>
<meta property="og:title" content="%s" />
<meta property="og:description" content="%s" />
<meta property="og:image" content="https://yourdomain.com/og/%s.png" />
<meta property="og:url" content="https://yourdomain.com/blog/%s" />
<meta http-equiv="refresh" content="0;url=https://yourdomain.com/blog/%s" />
</head>
<body>Redirecting...</body>
</html>
""".formatted(post.getTitle(), post.getExcerpt(), slug, slug, slug);
return ResponseEntity.ok()
.contentType(MediaType.TEXT_HTML)
.body(html);
}
}Common Spring Boot OG mistakes
- Using @RestController for pages that need OG tags — REST controllers return JSON. Switch to
@Controllerwith Thymeleaf for HTML pages. - Relative og:image URLs — always use absolute URLs. The scraper won't have your app's base URL context to resolve relative paths.
- Authentication on OG image endpoints — social scrapers don't send session cookies or auth headers. Ensure your
og:imageURL is publicly accessible.
Verify your Spring Boot OG tags
After deploying, paste your URL into OGFixer to preview how your Spring Boot app's links appear on Twitter, LinkedIn, Slack, and Discord.