DEV Community

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

Posted on

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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay