Springboot Devtools not hot reloading? - java

I haven't used spring in a while, and I'm trying to get spring boot dev tools to perform hot reloading. I've imported the web and spring boot dependencies from start.spring.io project generator and made a uri for "/hello". I can run the project but changing the uri values doesn't hot reload and I don't believe that the hot reloading is working. From what I've found on searching the internet, the only necessary step is to add the hot reloading dependency in your pom file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
My entire pom.xml file is as follows (generated from the start.spring.io website):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.java</groupId>
<artifactId>java_app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>java_app</name>
<description>Web Project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
And my web uri file is as follows (works, but without hot reloading):
package com.java.java_app;
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 JavaAppApplication {
public static void main(String[] args) {
SpringApplication.run(JavaAppApplication.class, args);
}
#GetMapping("/hello")
public String hello(#RequestParam(value = "name", defaultValue = "World") String name){
return String.format("Hello %s!", name);
}
}
This is as simple an application as I think it is possible to build, so I'm confused on what extra necessary step is needed to make hot reloading work. It does not appear to be documented say here (https://www.baeldung.com/spring-boot-devtools) other than to add the dependency. If it matters I'm running ubuntu 20.04.
EDIT:
I am running the application in the terminal on port :8080 and using vim to modify the uri file while the application is running. Since java has a lot of plugins for various IDEs someone suggested that it is worth mentioning that.

If you are not using an IDE you can use ./mvnw compile and the project will recompile while the editor is running.

Related

SpringBoot application shuts down when running using JAR, but works well when running from cmd or VSCode

I am trying to run a SpringBoot application from a JAR file, which I have built using VSCode's Project Manager from Java.
The app runs smoothly when running from VSCode, or when running from the command line, using
mvn spring-boot:run
This is my Main Class file:
package com.jvc.interconnectingflights.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan("com.jvc.interconnectingflights")
public class InterconnectingFlightsApplication {
public static void main(String[] args) {
SpringApplication.run(InterconnectingFlightsApplication.class, args);
}
}
This is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jvc</groupId>
<artifactId>interconnecting-flights</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>interconnecting-flights</name>
<description>SpringBoot app</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
This is the output I am getting when running from the JAR:
22:05:28.287 [main] INFO com.jvc.interconnectingflights.app.InterconnectingFlightsApplication - Started InterconnectingFlightsApplication in 3.102 seconds (process running for 3.46)
22:05:28.318 [SpringApplicationShutdownHook] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#78e94dcf, started on Tue Jan 31 22:05:25 CET 2023
I have tried all the answers from related posts, but still no luck. I would really appreciate any advice!
Tried answers from this post and related ones:
https://stackoverflow.com/questions/22380119/why-does-my-spring-boot-app-always-shutdown-immediately-after-starting
Created a new JAR using mvn package, which turned out to work. VSCode's Java Project Manager doesn't seem to build the JAR correctly
Make sure the java version defined in your pom file match or lower than that installed in your computer for example if installed java 16 you need version 16 or lower in your dependence file. some IDE use the builtin java SDKs or JDK plugins.

Why I getting 404 error on my Vaadin app in azure?

I'm trying to run Vaadin app on azure and I constantly getting error 404. It's basic app with only 2 classes - from spring initializr and one my class.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class WsbProjektZalApplication {
public static void main(String[] args) {
SpringApplication.run(WsbProjektZalApplication.class, args);
}
}
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WsbProjektZalApplication.class);
}
}
And one my class
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
#Route("/co")
public class ListView extends VerticalLayout {
public ListView() {
add(new H1("dziaƂa chyba totto"));
}
}
My pom file is default and I wasn't editing it.
?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>zaliczenie</groupId>
<artifactId>wsb-projekt-zal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>wsb-projekt-zal</name>
<description>wsb-projekt-zal</description>
<properties>
<java.version>17</java.version>
<vaadin.version>23.0.10</vaadin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I'm trying to run this app with war artifacts on tomcat 9. Problems occur only with Vaadin, I can run this app locally without any problems. Can somebody help me and tell me where I'm making mistake?
Screen of error below.
enter image description here
Try to resolve the error by adding the following in $TOMCAT_HOME/conf/server.xml
To avoid memory leaks, Tomcat disables the loading of Java driver files by default.
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false" />
Also, if you're creating a spring boot application, add "SpringBootServletInitializer" to your main file, as seen in the code below. As without SpringBootServletInitializer, Tomcat will see it as a regular application rather than a Spring boot application.
#SpringBootApplication
public class DemoApplication extends *SpringBootServletInitializer*{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication .class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication .class, args);
}
}
References:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists ,
Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists [duplicate]

Basic spring boot app not working, showing: Failed to refresh live data from process xxxx

I am beginner for spring boot. I initialized a new project and tried to run it but it does not work successfully. WHen I run this as spring boot application, it starts execution. In bottom compiler/status bar, it shows processing and retrying. it goes upto 10 times and throw the following error:
Failed to refresh live data from process xxxx
More detail here
TanmayTestApplication.java
package com.example.tanmay_test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TanmayTestApplication {
public static void main(String[] args) {
SpringApplication.run(TanmayTestApplication.class, args);
}
}
DemoControler.java
package com.example.cntr;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
#RestController
public class DemoControler {
#RequestMapping(path = "/index")
public String index() {
return "By Tanmay!";
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>tanmay_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tanmay_test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I had the same problem in STS, and tried different things to resolve it. The following dependency for spring actuator makes that problem disappear, but however the main point of spring actuator provides more features than this. To learn more, click https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html
The dependency should be added to your pom.xml file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
I have faced the same problem but managed to solve it.
The controller class has to be in the "child package" relative to the TestApplication class.
In your case, your TanmayTestApplication class is in the package com.example.tanmay_test. Therefore, your DemoControler class must be inside the package com.example.tanmay_test.xxx.
**Note that xxx can be anything but extends from package com.example.tanmay_test. For example, package com.example.tanmay_test.web.
Hope this helps!
Add this line in your file application.properties (src/main/resources):
spring.devtools.livereload.enabled=true
Live data is collected with the help of Spring Actuator.
You need to include the following dependency in your pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
See https://github.com/spring-projects/sts4/wiki/Live-Application-Information#application-requirements-for-spring-boot-projects for reference.
I was also facing same issue after adding Spring Actuator dependency, it resolved.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
after adding this in POM.xml, do maven build and run again.
It is simply saying that you didn't enable LiveReload.
This is non other then the Data Source error
To resolves this I disabled the auto-configuration of the DataSource. And, this will not affect auto-configuring any other beans.
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
I'm using VS Code and the thing that worked for me was adding a dev tool dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Also, add spring.devtools.livereload.enabled=true in application.properties file so that server knows that it has to reload every time a change is made.
Thanks for this one.

Spring boot app won't start on Tomcat Server

I am trying to deploy a spring boot application as a WAR to a tomcat server. I can build and deploy the war to the tomcat server just fine. When I start the server though my spring application never runs. The server starts up just fine. I have done everything Spring says to do here,
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
My project has its own custom Parent POM and consists of 2 modules.
I have read several other similar threads and as far as I can tell I have everything set up properly but clearly something is wrong. Any help would be much appreciated.
THank You!
Parent pom
<groupId>com.project</groupId>
<artifactId>TelematicsNotificationSystem</artifactId>
<name>TelematicsNotificationSystem</name>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<artifact-deployer.version>2.0.0-RELEASE</artifact-deployer.version>
<cxf.version>2.5.2</cxf.version>
<start-class>com.project.TNS.TelematicsNotificationSystem</start-class>
</properties>
<modules>
<module>TelematicsNotificationSystem-wsclient</module>
<module>TelematicsNotificationSystem-web</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
</dependencies>
Web Project Pom
<artifactId>TelematicsNotificationSystem-Web</artifactId>
<name>TelematicsNotificationSystem-Web</name>
<packaging>war</packaging>
<parent>
<groupId>com.project</groupId>
<artifactId>TelematicsNotificationSystem</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<start-class>com.project.TNS.TelematicsNotificationSystem</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources/</directory>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
My Application Class
package TNS;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
#SpringBootApplication
public class TelematicsNotificationSystem extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(TelematicsNotificationSystem.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TelematicsNotificationSystem.class);
}
}
Remember to refresh the application once deployed in Tomcat. It may be working well and only a refresh is needed.
Although I agree with Deinum that I needed to clean up my Pom that was not the overall fix. The fix was to simply add a component scan to my class which contained my main method.
#SpringBootApplication
#ComponentScan(basePackageClasses = DashBoardController.class)
public class TelematicsNotificationSystem extends SpringBootServletInitializer
The starting app couldn't find the Controller so it could handle any of my mapping requests.

How can springboot app both run in main method and run in tomcat with the same configuration as below?

This is the whole pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>com.example.Application</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The Application class should be like this
Application.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.boot.web.support.SpringBootServletInitializer;
#SpringBootApplication
#Controller
public class Application extends SpringBootServletInitializer {
/**
* Used when run as JAR
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#RequestMapping("/")
public #ResponseBody String hello(){
return "hello world!";
}
/**
* Used when run as WAR
*/
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
The whole project is as you can see as above.However,the main method can not run if I set the scope
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
to provided.But if I remove the scope,the main method can run normally.The question is what should I do to run the main method without removing the scope.
In maven, when you omit the scope it defaults to compile.
When you are running outside a servlet container (i.e as a java
application) you need to provide the scope of compile (or remove scope as compile is the default). This is done so that these jars are added as dependency.
When you run within a servlet container (tomcat or jetty), then the scope could be set as provided as these jars are provided at runtime by the servlet container and should not be included within the packaged application.
Now, in order to solve the problem at hand you could use maven profiles. When executing maven tasks you could activate the profile by using the -P switch.

Categories