Build a Markdown-based Blog with Spring Boot - Part 1
Roshan Adhikari
Posted on May 19, 2021
In this piece, we will be building a personal blog containing articles written in Markdown.
We will use Spring Boot to develop the project and few other tools and libraries. We will push our project to a GitHub repository and use Heroku's Automatic Deployment feature to publish posts to our blog.
Additionally, we will use the RemoteMySQL service to host our remote MySQL database. To communicate with the database, we will need MySQL connector for Java and Spring Data JPA.
For rendering our view, we will use Thymeleaf.
This article is one part of a series of posts that I intend to publish on this topic.
For the entirety of this tutorial, I will use IntelliJ IDEA Community Edition as the IDE.
There may be some IntelliJ-specific shortcuts and hotkeys here and there, but you can follow along using your IDE of choice.
Now let's get going!
Generating Project
Just like how every fun Spring Boot adventure begins, go to start.spring.io (Spring Initializr) and configure our project.
Select the Maven Project option and Java as the language.
Choose a non-snapshot version of Spring Boot (I chose 2.4.5).
Then, fill up your Project Metadata -- including the Group, Artifact, Name, Description, and Package name.
We will be using Jar packaging, so select Jar. Then, choose 11 as the Java version.
For dependencies, we will need the following:
- Spring Data JPA
- MySQL Driver
- Thymeleaf
- Spring Web
- Lombok
Extract the resulting .zip file and you're ready to go!
Opening the project
Once the .zip file has been extracted, the project can be imported into the IDE.
Using IntelliJ IDEA, you can open the project by pointing IntelliJ to the root folder of the project (one that contains the pom.xml file as an immediate child).
After you've opened the project, take a look at the directory and file structure generated by Spring Intializr.
The pom.xml file is crucial for a Maven project.
It is filled with the dependencies that we selected from our little configuration session with Spring Initialzr.
Since Spring Boot is an opinionated framework, within its starter dependencies are a collection of other dependencies that have been curated by Spring Boot itself.
Defining entities
Our blog needs two entities -- posts and authors.
The author entity describes an author of a post with their name, email, and website.
The post entity describes a post, with its title, content, timestamp of creation, synopsis (first 150 characters of the post), and the ID of the author.
Both entities are identified by a unique, auto-generating ID.
Defining POJOs
The Plain Old Java Object (POJO) definitions for the entities are as described below.
We use JPA annotations to specify a one-to-many relationship between authors and posts.
Furthermore, to map Java's LocalDateTime typed attribtue to MySQL's datetime typed column, we need to convert between the two types (from LocalDateTime to TimeStamp and vice-versa).
For that, we define a new LocalDateTimeConverter class as following.
Code
I will end things at this much for now. We will continue the project from the next part of this series.
The project has been pushed to a GitHub repository, and you can find the code here.
Posted on May 19, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.