I have started creating a RESTful API using Spring Boot. I haven't touched Spring in 2 years. I am using Maven. I have the main class:
package com.tsakirogf.smartapi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SmartapiApplication
{
public static void main(String[] args)
{
SpringApplication.run(PeopleapiApplication.class, args);
}
}
I have a Controller, Model and Service. 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>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tsakirogf</groupId>
<artifactId>peopleapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>smartapi</name>
<description>Technical Test</description>
<properties>
<java.version>11</java.version>
<!-- <tomcat.version>8.5.63</tomcat.version>-->
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<!-- <scope>test</scope>-->
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The project is building successfully and the mvn clean and mvn install commands run successfully as well.
The problem is that when I am trying to run it in IntelliJ I get this error:
2021-02-11 18:19:42.692 ERROR 14584 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1355)
The following method did not exist:
'java.lang.String javax.servlet.ServletContext.getVirtualServerName()'
The method's class, javax.servlet.ServletContext, is available from the following locations:
jar:file:/F:/Workspace/Uni/SoftwareEngineer/peopleapi/lib/javax.servlet.jar!/javax/servlet/ServletContext.class
jar:file:/C:/Users/Fotis/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.41/tomcat-embed-core-9.0.41.jar!/javax/servlet/ServletContext.class
The class hierarchy was loaded from the following locations:
javax.servlet.ServletContext: file:/F:/Workspace/Uni/SoftwareEngineer/peopleapi/lib/javax.servlet.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext
Process finished with exit code 1
Any thoughts or directions I should move to? I have tried to synchronize dependencies and tried the latest and RELEASE packages in the pom. I have also deleted the whole m2 repo (twice).
Your problem is F:/Workspace/Uni/SoftwareEngineer/peopleapi/lib/javax.servlet.jar. Check why you have that JAR in your classpath (most likely it comes through some script or so). This seems to be an older version than the one you're referencing in your pom.xml
Related
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.
I have a maven project which schematically looks like below:
Project
|- App (where the Spring Application class lives)
|- Repository (Spring Data JPA repositories)
|- Entities
When I run mvn clean test all the tests run without any problem. I don't like this because no point running all the tests always. And clunky as hell...
I wanted to use IntelliJ test runner (it seems more comfortable (less clunky)) than maven. However, to be able to use it mvn clean install must work if I assume right. Here is where the problem arises.
#DataJpaTest annotation stops working, meaning tests don't find Application class
When I add #SpringBootTest(classes = Application.class) it fails due to that Application class is not on the path
For more details here is the test class:
#DataJpaTest
public class AddChildToNodeTests extends BaseTest{
#Test
public void addsChildToNode() {
// Arrange
SourceFormatNode parentNode = _sut.save(new SourceFormatNode("parent"));
SourceFormatNode childNode = _sut.save(new SourceFormatNode("child"));
// Act
SourceFormatNode result = _sut.addChildToNode(childNode.getId(), parentNode.getId());
// Assert
assertThat(result).isNotNull();
assertThat(result.getId()).isEqualTo(childNode.getId());
assertThat(result.getParentNodeId()).isEqualTo(parentNode.getId());
}
This is the error message I get:
[INFO] Running org.encyclopediagalactica.sourceformats.worker.dml_repositories.unit.tests.AddChildToNodeTests
18:00:53.212 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
18:00:53.212 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,or
g.springframework.test.context.CacheAwareContextLoaderDelegate)]
18:00:53.212 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [org.encyclopediagalactica.sourceformats.worker.dml_repositories.unit.tests.AddChildToN
odeTests] from class [org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper]
18:00:53.212 [main] INFO org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - Neither #ContextConfiguration nor #ContextHierarchy found for test class [org.encyclopediagalactica.source
formats.worker.dml_repositories.unit.tests.AddChildToNodeTests], using SpringBootContextLoader
18:00:53.213 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [org.encyclopediagalactica.sourceformats.worker.dml_repositories.unit
.tests.AddChildToNodeTests]: class path resource [org/encyclopediagalactica/sourceformats/worker/dml_repositories/unit/tests/AddChildToNodeTests-context.xml] does not exist
18:00:53.213 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [org.encyclopediagalactica.sourceformats.worker.dml_repositories.unit
.tests.AddChildToNodeTests]: class path resource [org/encyclopediagalactica/sourceformats/worker/dml_repositories/unit/tests/AddChildToNodeTestsContext.groovy] does not exist
18:00:53.213 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [org.encyclopediagalactica.sourceformats.worker.dml_repositories.un
it.tests.AddChildToNodeTests]: no resource found for suffixes {-context.xml, Context.groovy}.
18:00:53.213 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [org.encyclopediagalactica.sourceformats.worker.dml
_repositories.unit.tests.AddChildToNodeTests]: AddChildToNodeTests does not declare any static, non-private, non-final, nested classes annotated with #Configuration.
18:00:53.216 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class
[org.encyclopediagalactica.sourceformats.worker.dml_repositories.unit.tests.AddChildToNodeTests]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.06 s <<< FAILURE! - in org.encyclopediagalactica.sourceformats.worker.dml_repositories.unit.tests.AddChildToNodeTests
[ERROR] org.encyclopediagalactica.sourceformats.worker.dml_repositories.unit.tests.AddChildToNodeTests Time elapsed: 0.06 s <<< ERROR!
java.lang.IllegalStateException: Unable to find a #SpringBootConfiguration, you need to use #ContextConfiguration or #SpringBootTest(classes=...) with your test
When I modify my tests so #SpringBootTest(classes = Application.class) is used I get the following error message:
#SpringBootTest(classes = Application.class)
public class AddChildToNodeTests extends BaseTest{
#Test
public void addsChildToNode() {
// Arrange
SourceFormatNode parentNode = _sut.save(new SourceFormatNode("parent"));
SourceFormatNode childNode = _sut.save(new SourceFormatNode("child"));
// Act
SourceFormatNode result = _sut.addChildToNode(childNode.getId(), parentNode.getId());
// Assert
assertThat(result).isNotNull();
assertThat(result.getId()).isEqualTo(childNode.getId());
assertThat(result.getParentNodeId()).isEqualTo(parentNode.getId());
}
}
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/andrascsanyi/Dev/github.com/EncyclopediaGalactica/SourceFormats.Worker/dml-repositories.unit.tests/src/test/java/org/encyclopediagalactica/sourceformats/worker/dml_repositories/unit/tests/AddChildToNodeTests.java:[3,54] cannot find symbol
symbol: class Application
location: package org.encyclopediagalactica.sourceformats.worker
What I tried so far:
I found a few posts where the spring-boot-maven-plugin placement was discussed. Eventually, I removed it from the main pom.xml and added only the modules which will be executed e.g. app module and test modules. However, I haven't seen a much changes due to this, but probably I changed multiple parameters at once and I can't see clearly what changes caused what result
I put the module dependencies of the test module into test scope, and moved back to normal scope --> no changes
If you take a look at the pom files you'll see that unit.test project pom.xml file contains app module as dependency, so, to my knowledge, it should see the Application class. Moreover, Application class namespace makes possible that looking-for-application-class functionality of Spring can find it, that is why #DataJpaTest works.
So, the main question is why mvn clean install doesn't work while mvn clean test does with the same codebase? I'm totally clueless what I'm missing here...
The main 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>pom</packaging>
<modules>
<module>app</module>
<module>model</module>
<module>dml-repositories</module>
<module>dml-repositories.unit.tests</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.encyclopediagalactica</groupId>
<artifactId>sourceformats.worker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SourceFormats Worker</name>
<description>A worker service for SourceFormats management.</description>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<assertj.version>3.21.0</assertj.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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.210</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- <pluginManagement>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </pluginManagement>-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
app module's 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">
<parent>
<artifactId>sourceformats.worker</artifactId>
<groupId>org.encyclopediagalactica</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>app</artifactId>
<dependencies>
<dependency>
<groupId>org.encyclopediagalactica</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.encyclopediagalactica</groupId>
<artifactId>dml-repositories</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
unit test project's 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">
<parent>
<artifactId>sourceformats.worker</artifactId>
<groupId>org.encyclopediagalactica</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dml-repositories.unit.tests</artifactId>
<dependencies>
<dependency>
<groupId>org.encyclopediagalactica</groupId>
<artifactId>dml-repositories</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.encyclopediagalactica</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.encyclopediagalactica</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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.
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.
I am new for spring Boot, I got this problem when I follow the Spring Guide-Accessing data with MySQL
I got an Error in my STS IDE, I don't know what's the hell happening.
In the src/main/java/UserRepository.java
package hello;
import org.springframework.data.repository.CrudRepository;
import hello.User;
public interface UserRepository extends CrudRepository<User, Long> {
}
STS does warn me about this mistake, I ignore it until I run Cmd: mvn spring-boot
so I am back to check the warning, it is:
The type org.springframework.data.repository.Repository cannot be
resolved. It is indirectly referenced from required .class files
Thanks very much for helping me!
I bulid the project using Maven as recommended,the Pom.xml is:
<?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>org.springframework</groupId>
<artifactId>gs-mysql-data</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Apply some spring boot standards to the code
Keep your boot application on top of the package, in your case your BootApplication should be in hello package. Like shown in below
#SpringBootApplication
public class BootApplication extends SpringBootServletInitializer {
public static void main(String args[]) {
SpringApplication.run(BootApplication.class, args);
}
}
Now keep your repository inside the sub-package of hello. In your case it should be in hello.repository
Now try to run with BootApplication.
Buddy,It doesn't work anyway.This Exception is caused by CrudRepository.The eclipse can't resolve this class.My project is very simple,I get this project from spring website.its guide--Accessing data with MySQL
And now my project is completely same as [gs-accessing-data-mysql-complete]
The Spring official Website think it is successful project,and let the trainer to check [gs-accessing-data-mysql-initial] against [gs-accessing-data-mysql-complete]
.Howerver,even the later can't be build successfully.My project structure:
src
+main
+java
+hello
--Application.java
--MainController.java
--User.java
--UserRepository.java
and the log info ,I had better show you about the digest:
1.[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:run (default-cli) on project gs-mysql-data: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'mainController' defined in file [D:\workbunch\Spring Workbunch\gs-accessing-data-mysql-initial\target\classes\hello\MainController.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [hello.MainController] from ClassLoader [java.net.URLClassLoader#6424f2f4]: org/springframework/data/repository/CrudRepository: org.springframework.data.repository.CrudRepository -> [Help 1]