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

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.

Related

Spring security refuses to use my config file

im learning Spring security and created a config file to change the login from form to basic as a first step, however the config file doesn't do anything and no changes happen
here is the code in the config file:
package Security;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
#Configuration
#EnableWebSecurity
public class securityConfiguration {
#Bean
public SecurityFilterChain configure(HttpSecurity Sec ) throws Exception {
Sec
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
return Sec.build();}
}
and this is my 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>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-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>
i suspected the problem might be spring not picking up the config file but the annotations did nothing to fix this.
I believe the issue might have to do with how you are organizing the packages. By default, Spring will look under the package of the main application or any sub-packages.
It's hard to be sure from the information provided in the question, but for instance, if your main application class is under the package com.example, and the security configuration class is under the package Security, it won't find it by default. Also, be sure to not use the default package (or "no package").
So the options are:
Add #ComponentScan annotation to the main class to indicate where to look: #ComponentScan("Security")
Move the security config class under the same package as the main class or a sub-package: com.example.security (recommended).
See Spring documentation for reference.
I also suggest that you use the standard naming and styling conventions for Java. (lower-case package names, lower-case method attributes, pascal-case class names). See Naming conventions for reference. It will not only look better and consistent with other code bases but will also make it compatible with many tools that follow convention over configuration, which is favored a lot in the Spring ecosystem.

Simple Spring Boot Project is giving 404 Error

I am learning Spring Boot but I am not able to execute the basic "Hello World" program. I have built the basic project using https://start.spring.io/ and updated the code as per below code snippets:
Spring Boot Main Class:
package com.rohit.springboot.practice.selfspringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan(basePackages = "com.rohit.springboot.practice.selfspringboot")
public class SelfSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SelfSpringBootApplication.class, args);
}
}
Controller Class:
package com.rohit.springboot.practice.selfspringboot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class ThisWorldSpringContainer {
#RequestMapping(method = RequestMethod.GET, value = "/hello-world")
public String getWelcome() {
return "Hello World";
}
}
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>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rohit.springboot.practice</groupId>
<artifactId>self-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>self-spring-boot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>
Error:
Message: The requested resource [/hello-world] is not available
I know this question has been asked by multiple people but i have gone through those answers and tried those given answers but didn't work.
Technologies used are Java 8, Tomcat 9, Spring Boot 2.6.6 etc.
Kindly help.
Since you don't have the Tomcat dependency, I'm implying that you want your application to run in a standalone Tomcat instance. Also, given your comment, I believe you're working on either Eclipse or Spring Tools Suite (Which is the same, really).
With that, it seems your problem is the lack of a packaging method. Tomcat needs the code its going to run in a package. This package is a zip-like file with .war extension. Maven, the program that handles the dependencies of your project according to the pom.xml file, can package it in either war of jar format. Since you are running it in an external Tomcat, you want the war extension. So please, add the following line to your pom.xml file. It has to be in the same level as <name>:
<packaging>war</packaging>
Then, update your project (Alt + F5, check your project and hit OK) then try running your project again. You should see an stylish "SPRING" output in your console.
Now, since you are running this externally, you need to specify the name of the project, so you should call your endpoint with
http://localhost:8080/<your_project_name_here>/hello-world
I advise you to check this new change in Spring Boot 2.6 :
PathPattern Based Path Matching Strategy for Spring MVC
In this release, the default strategy for matching request paths against registered Spring MVC handler mappings has changed from AntPathMatcher to PathPatternParser.
If you need to switch the default strategy back to AntPathMatcher, you can set the property spring.mvc.pathmatch.matching-strategy to ant-path-matcher as in the line below to add in the file application.properties :
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
I encountered the same issue of HTTP 404 error when upgrading to Spring Boot 2.6 and adding this property solved my problem and should solve yours.
I have modified your code as below:
#RequestMapping
#RestController
public class ThisWorldSpringContainer {
#GetMapping(value = "/hello-world")
public String getWelcome() {
return "Hello World";
}
}
I thinking you are missing
#Requestmapping
in classlevel

Springboot Devtools not hot reloading?

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.

[Solved]Request Mapping returns 404 instead of the mapped String after creating a basic example from spring.start.io

Solution: rename package Controller to myrest.controller , has to be a subpackage of the main package(the one that contains the main method)
I've already been on the following topics but none seemed to worked for me:
RequestMapping leads to 404 Error , added the regex ( /app/** to Rest Controller and **/api to the Method) but still won't map it.
Spring #RequestMapping, 404 error , added <packaging>jar</packaging>
to my pom.xml , although i can't find the path to change it (i doubt that the issue might be there, i'm not sure though).
Request Mapping returning error 404 , i visit the correct endpoint which is localhost:8080/api (port is not in use), i also tried it with another port number.
Spring RequestMapping 404 error , we might have the same issue here, i don't see why i should change the mapping, haven't changed anything on default directories or files.
This is not the first time i'm making a Springboot application
My 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>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>myrest</groupId>
<artifactId>remoteapi</artifactId>
<version>0.0.1</version>
<name>remoteapi</name>
<description>Rest Mobile Dev ICSD Api</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>
My RestController class
package Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class MainController {
#RequestMapping(value="/api")
public String test()
{
return "Hello World";
}
}
This is my directory, the MainController class is inside Controller package
The URL i'm accessing it from is localhost:8080/api
Important Note: if i run the exact method with the exact mapping on an older project, it runs perfectly.
My Comments: This is pretty odd , since i've never changed anything on configurations, probably i missed something when i created the project? I Still haven't figured what that might be.
Edit: Added the directory, and the that location i'm trying to access it
I renamed the Controller package to myrest.controller ,
has to be a subpackage of the main package!
As I remember, SpringBoot only scan packages which have the same prefix with Application class (in your case: myrest).
If you want spring boot scan another package, you should define them through annotation: #ComponentScan("package.hear")

Java Spring Boot Actuator Metrics system load average returning -1

I am a new user to Spring Boot Actuator Metrics, I need to determine the CPU utilization of the system. The /metrics url does give me rest of the details, however the systemload.average returns -1 (if load average is not available -1 is returned). Could you let me know where I went wrong and how do I correct it?
I am using maven and Eclipse IDE (Mars). I am accessing metric details on localhost itself. the url is http://localhost:8080/details/metrics (details used for context path)
Here is my code:
Application.java file
package spring.boot.admin.actuator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
#EnableAutoConfiguration
#ComponentScan
#PropertySource(value = "classpath:application.properties")
public class Application{
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
POM File
4.0.0
<groupId>spring.boot.admin</groupId>
<artifactId>actuator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>actuator</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>
**application.properties file**
management.port=8080
management.context-path=/details
management.security.enabled=true
endpoints.health.enabled=false
security.basic.enabled=true
security.user.name=admin
security.user.password=admin
endpoints.health.id=health
endpoints.health.sensitive=true
endpoints.health.enabled=true
endpoints.metrics.id=metrics
endpoints.metrics.sensitive=true
endpoints.metrics.enabled=true
endpoints.server.id=server
endpoints.server.sensitive=false
endpoints.server.enabled=true
endpoints.info.id=info
endpoints.info.sensitive=false
endpoints.info.enabled=true
info.app.name=Spring Actuator Example
info.app.description=Spring Actuator Working Examples
info.app.version=0.0.1-SNAPSHOT
management.security.enabled=true
The systemload.average returns what the JVM returns through the OperatingSystem MBean (Available in the java.lang tree node). Use JConsole or the VisualVM-Beans plugin in VisualVM to view what is returned there.
If the value of the SystemLoadAverage attribute is the same, it is no bug in Spring Boot or your usage of Spring Boot.

Categories