0

I want to add live reload for my spring boot application. For that I am testing it on the spring boot default demo app.

I have added the dependency and running gradle bootRun:

plugins {
    id 'org.springframework.boot' version '2.7.2'
    id 'io.spring.dependency-management' version '1.0.12.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}

tasks.named('test') {
    useJUnitPlatform()
}

Here is my controller:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {


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

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s!", name);
    }

}
        

I am hitting this api with postman. When i change the response from hello to hi, this is not being reflected.

The live server is running according to logs but does not reflect changes

P.S. I have already tried the build options, advance settings option, registry option and run-configuration option

Shantanu
  • 33
  • 1
  • 1
  • 6
  • You can follow this article as well https://faun.pub/10-steps-to-enabling-auto-reload-for-spring-boot-in-intellij-230326413b68 – Akhil Aug 22 '22 at 11:00
  • It is already answered here https://stackoverflow.com/questions/23155244/spring-boot-hotswap-with-intellij-ide – DCO Aug 23 '22 at 05:54
  • Does this answer your question? [spring boot hotswap with Intellij IDE](https://stackoverflow.com/questions/23155244/spring-boot-hotswap-with-intellij-ide) – DCO Aug 23 '22 at 05:55

2 Answers2

0

If you are using IntelliJ there is an option called Build project automatically please try after enabling that.

enter image description here

Link: https://websparrow.org/misc/intellij-idea-spring-boot-dev-tools-is-not-working

For eclipse there should be some similar option.

Santanu
  • 691
  • 3
  • 5
  • I have already tried that, and the registry and the run configuration options. P.S. - My actual project takes 30 sec to build, i am looking for something faster like 2-3 sec with hot swapping of the classes – Shantanu Aug 23 '22 at 05:48
  • oh, some other solution is required – Santanu Aug 24 '22 at 20:35
0

On top of adding the spring-boot-devtools dependencies in the gradle build file, what worked for me was to continuously build the jar

gradle bootJar --continuous

Any changes to source file will trigger a repackage of the jars, before live server picks it up and reload the spring boot application.

darkvalance
  • 390
  • 4
  • 14