Build a Markdown-based Blog with Spring Boot — Part 4

Build a Markdown-based Blog with Spring Boot — Part 4

Adding jsoup dependency

Remember how we have a synopsis attribute in the Post entity? We will use that attribute (or field, in the database) and set it to the first 150 characters of the actual post’s body. However, after parsing the Markdown and rendering HTML from it, the post’s body would look like, say,

<h1>Hello World!</h1><br /><p>This is the post's actual body, rendered in HTML!</p>
pom.xml file after adding jsoup dependency
Loading Maven changes after adding jsoup dependency
Loading Maven changes after adding jsoup dependency

ContextEventListener class

Begin by creating the class that implements ApplicationListener interface that takes in as type parameter the ContextReferencedEvent type. With this, our class would need to override the onApplicationEvent method.

Skeleton of the ContextEventListener class
Implementation of onApplicationEvent method

PostUtil and AuthorUtil classes

PostUtil class
AuthorUtil class

Get it running

Now, when we run our Spring Boot application, or whenever we cause the Spring Context to refresh, a ContextRefreshedEvent is fired. This looks for a new blog post in the resources/posts/ directory and persists it.

# Hello World! This is my *first* blog post. <br> Be sure to read future parts of this blog post series, <br> titled **Build a Markdown-based Blog with Spring Boot**.

Code

We have now created an application that persists new blog posts to our database. Now, we will work towards displaying blog posts using the Thymeleaf template engine. But that’s for the next post.

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store