DEV Community

Cover image for Using LiveReload with Spring boot devtools.
Ratul sharker
Ratul sharker

Posted on

1

Using LiveReload with Spring boot devtools.

Background

Spring boot is a versatile framework to develop web applications. During development time, iteration of code change is a common thing. It is a plus point to see the effect of the code changes as fast as posible. Spring boot devtools enables developer to see the changed output most effectively.

Introducing the livereload into the picture, favors the development cycle. Livereload will automatically detect when to reload the browser intelligently.

What is LiveReload ?

LiveReload is a mechanism, which automatically detects any changes in the source code or any configuration (i.e environment) and then reload the browsers automatically.

To facilitate this feature, two part is necessary in the development system:

  1. Some script in the browser end, which will listen to any changes and reload the browser.
  2. A separate server in spring boot end, to notify the browser whenever code or any configuration has changed or not.

The script in the browser end is already developed as browser extension by developers:

Spring boot devtools ships with live reload server. Look for the log while running a project with spring-boot-devtools included:

2025-03-15T19:39:34.340+06:00  INFO 36364 --- [devtools] [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
Enter fullscreen mode Exit fullscreen mode

Livereload server uses the web socket for communication from spring boot devtools end to browser extension end.

Project setup

We will start from a scratch spring boot project. Lets head to Spring Initializer and select following dependencies:

  • Spring Web.
  • Thyemeleaf.
  • Spring Boot DevTools.

Scratch spring boot project from spring initializer

Extract the downloaded zip and open in your editor of choice. I have opened mine into visual studio code.

Opening downloaded project into vscode

Now add an ExampleController.java containing following,

package com.example.devtools;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ExampleController {

    @GetMapping(path = "/")
    public String index(Model model) {
        return "example";
    }
}
Enter fullscreen mode Exit fullscreen mode

and inside the template folder added example.html.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example</title>
</head>
<body>
    <h1>Hello world</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

To run the application from VSCode run option, add following run configuration

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Devtools",
            "request": "launch",
            "mainClass": "com.example.devtools.DevtoolsApplication",
            "projectName": "devtools"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Or the project can ran directly from terminal by following:

cd {PROJECT_DIRECTORY}
./mvnw spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Accessing the http://localhost:8080/ shows following:

Running Scratch Project

Experiencing the LiveReload

Now run your application from run configuration or as maven goal. Open the browser and enable the LiveReload extension. Now do any change in the html and see the updates into the browser.

Demonstrating LiveReload

Look upon changing the html, the spring boot application server does not restarted.

But if we change anything in the java end, which requiring the java program to restart, devtools will automatically detect that and restart the server and browser automatically.

Inspecting the network tabs web socket section shows each reload triggers and data receive from the liverealod server.

Demonstrating the usage of websocket in livereload mechanism

Conclusion

Specifically saying for monolith applications, reloading the whole server for a design change is very frustrating, because it requires significant time to restart the application server.

Using the livereload with devtools can significantly reduce the change-checking time during development.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed
  • 2:34 --only-changed
  • 4:27 --repeat-each
  • 5:15 --forbid-only
  • 5:51 --ui --headed --workers 1

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay