Spring Boot - Auto browser refresh with LiveReload

[Updated: Mar 15, 2017, Created: Feb 23, 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-starter-web 1.4.4.RELEASE: Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
    Corresponding Spring version: 4.3.6.RELEASE
  • spring-boot-devtools 1.4.4.RELEASE: Spring Boot Developer Tools.
    Corresponding Spring version: 4.3.6.RELEASE
  • spring-boot-starter-tomcat 1.4.4.RELEASE: Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web.
    Corresponding Spring version: 4.3.6.RELEASE
  • 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
        • resources
        • webapp
          • WEB-INF
            • pages

See Also