Close

Spring Boot - Auto browser refresh with LiveReload

[Last Updated: Mar 15, 2017]

In addition to auto restart feature of spring-boot-devtools, LiveReload is another very useful feature which triggers a browser refresh when there are some changes in the code or in a resource file. Let's see an example to use LiveReload.

Installing LiveReload browser extension

We have to install LiveReload browser extension to use LiveReload. After installing LiveReload in chrome browser (for example), you will see an icon like this:

Also after starting our spring boot application we have to make sure that this tool is enabled or not. Hover over the mouse pointer on the LiveReload circular icon and if we see 'Enable LiveReload' tooltip like this then we have to click the icon to enable it.

Creating a simple Spring Boot MVC project

@Controller
public class MyController {

  @RequestMapping("/test")
  public String handle(Model model) {
      model.addAttribute("msg", "test");
      return "testView";
  }
}

src/main/webapp/WEB-INF/pages/testView.jsp

<html>
<body>
<h3>${msg}</h3>
</body>
</html>
@SpringBootApplication
public class ExampleMain {

  public static void main(String[] args) {
      SpringApplication.run(ExampleMain.class, args);
  }
}

src/main/resources/application.properties

spring.mvc.view.prefix= /WEB-INF/pages/
spring.mvc.view.suffix= .jsp
spring.devtools.restart.additional-paths=.

Note that we have included a new property of 'spring.devtools.restart.additional-paths' this time. Spring boot watches all files which are in 'target' folder (if using maven). In exploded form, we don't have all resources copied to the target folder. That's the reason we have to include additional folders to be watched e.g. JSP files. The single dot points to the application base directory.

Output

Following video shows how auto-restart and LiveReload work without explicitly restarting the application and refreshing the browser. In this example, I'm using IntelliJ.

Note that in intellij 'Make project automatically' option should be selected otherwise we have to use Ctrl+F9 for compilation:

In Eclipse, we have to save (Ctrl+s or Ctrl+Shift+s) to trigger the compilation.

Example Project

Dependencies and Technologies Used:

  • Spring Boot 1.4.4.RELEASE
    Corresponding Spring Version 4.3.6.RELEASE
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • spring-boot-devtools : Spring Boot Developer Tools.
  • spring-boot-starter-tomcat : Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web.
  • tomcat-embed-jasper 8.5.11: Core Tomcat implementation.
  • JDK 1.8
  • Maven 3.3.9

Boot Live Reload Example Select All Download
  • boot-live-reload
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • MyController.java
          • resources
          • webapp
            • WEB-INF
              • pages

    See Also