Spring Boot Hello World Example Not working - java

I have downloaded spring boot example from https://start.spring.io/.
I tried many times but i get same error again and again:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at com.example.demo.Application.main(Application.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
Here 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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBoot</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-tomcat</artifactId>
<scope>provided</scope>
</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>
and SpringBootClass
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
also my ControllerClass
package com.example.demo.controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
#RestController
#EnableAutoConfiguration
public class HelloWorldController {
#RequestMapping("/hello")
#ResponseBody
public String sayHello() {
return "Hello World Developer!!!";
}
}
I am struggling from past three days and not getting any help from anywhere.
I am using Spring Tool Suite
I am new to Spring Boot and need help here

Set Maven to you path and try from command prompt/ terminal with command:
mvn clean install
this will create jar file in target folder.
go to target folder and run java -jar jarName.jar.
OR
Try cleaning the project from project tab,
Add all the maven dependencies to eclipse build path.
Then try running it as java application.

It is caused by a corrupt Maven repository.
Delete everything under C:\Users\<me>\.m2\repository.
Then do Eclipse Maven Update, and it will work.
it is like spring-boot.jar got corrupted.

Related

Sping RestController not responding to request

I am just trying to implement a sample rest controller on spring boot for learning. When i am sending the request through web browser i am getting a white label error.
My applications context path is set to /learn. Below are my source files, project structure and pom.xml.
TestController.java
package com.practice.learn.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/learn")
public class TestController {
TestController(){
System.out.println("TestController initialized");
}
#GetMapping("/test")
public String testMethod() {
System.out.println("Inside test method");
return "This is sample text";
}
}
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.7.8</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.practice</groupId>
<artifactId>learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>learn</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
LearnApplication.java
package com.practice.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class LearnApplication {
public static void main(String[] args) {
SpringApplication.run(LearnApplication.class, args);
}
}
application.properties
server.port=8081
server.servlet.context-path=/learn
since you already set the servlet context path as '/learn' in your configuration file, you do not need to add '/learn' again in the RequestMapping in your controller. just remove it and let us know.

If I add spring-boot-starter-data-jpa to a simple spring application I get 404s but none if it's not there

I am trying to debug why a my application won't show, and in doing so I have this small application running hello world. This does work, it shows the text on the screen, great. But then if I add spring-boot-starter-data-jpa to the pom file, I get a 404 error instead. I need to use spring-boot-starter-data-jpa and have no idea why it doesn't work as expected.
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>
<artifactId>spring-boot-hello</artifactId>
<packaging>war</packaging>
<name>Spring Boot Hello World Example</name>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
</parent>
<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-starter-tomcat</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!--this is the issued one-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<finalName>testapp</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
controller
package org.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloController {
#RequestMapping("/")
String hello() {
return "Hello World, Spring Boot!";
}
}
main class
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class MyWebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyWebApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(MyWebApplication.class);
}
}
I have gone through every other answer I could find and made sure I was using restcontroller instead of controller, I believe my main class is in the correct spot, I am at a loss as to why one dependency, especially one that isn't being used yet, causes a 404.
In application.properties file in resource folder did you set database configuration such asusername, password and url? It should be set like this.
spring.datasource.url=jdbc:mysql://localhost:3306/restapi
spring.datasource.username=root
spring.datasource.password=
And it's better to use #GetMapping annotation instead of #RequestMapping.

can't import #Entity annotation from javax.persistence using Spring Data JPA

So I'm trying to solve this problem for hours and it's really annoying.
I used Spring Initializr to generate a spring boot project: java 19, spring 3.0.0, maven, with mariadb driver, spring web, spring data jpa dependencies.
The problem is that after I create a model class, Student, I can't import #Entity annotation from javax.persistence. All the tutorials and videos I watched, they imported from javax.persistence but I got only jakarta.persistence. Why?
I tried to add myself dependency for javax.persistency but after I create the model with all annotation (#Entity, #Table, #column) the table is not created.
Here Are my files:
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.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>19</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.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</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>
Student.class
package com.example.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
#Entity(name = "student") // CAN'T GET FROM JAVAX.PERSISTENCE, SAME FOR ANNOTATIONS BELOW
public class Student {
#Id
#Column(name="id")
private int id;
#Column(name="name")
private String name;
}
application.properties locates in src/main/resources
spring.datasource.url=jdbc:mariadb://localhost:3307/db1
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
The entry point
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
What am I Doing wrong??
In the newest version of Spring Boot, javax library is replaced by jakarta (see more details here https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0.0-M1-Release-Notes) and here https://spring.io/blog/2022/01/20/spring-boot-3-0-0-m1-is-now-available, so it's ok that you can only find jakarta. You may go ahead and use it.

Spring Boot - Deployable War - ClassNotFoundException

I have been attempting to create a spring boot deployable war so that it can be deployed to a tomcat server (I would like to note that it runs fine as an executable jar). However, I have been running into the following exception which occurs on my Tomcat 9 server (I have one running within IntelliJ and another one running on a linux server, both throw the same exception):
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.demo.DemoApplication]; nested exception is java.lang.NoClassDefFoundError: org/springframework/context/annotation/AdviceMode
From my understanding, this lives in the spring-context jar, which is correctly being pulled into the build via maven. I created a super simple demo app with spring boot and I really can't see why I would be running into this issue.
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.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>
<properties>
<java.version>11</java.version>
<start-class>com.example.demo.DemoApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
And here is the main application file:
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#SpringBootApplication
#RestController
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);
}
#RequestMapping(value = "/")
public String hello() {
return "Hello World from Tomcat";
}
}
I am at a total loss as to why I'm hitting this ClassNotFoundException. Looking at the war file, I do see all the appropriate libraries in the WEB-INF/lib folder. The two files I pasted above are literally all the project I'm attempting to run is.
I have looked at this guide: Spring Boot Traditional Deployment
If I remove extends SpringBootServletInitializer, then the error goes away, but then I of course don't really have a functional spring boot war file.
did you import spring-boot-starter-web? This class is located in context lib that is imported by spring-boot-starter-web
Well, apparently I had a lib in my Tomcat lib folder that was built to include ALL dependent libraries... which happens to have a spring dependency... so it was pulling in an older version of spring, which was causing a conflict. With the jar swapped out with one that doesn't include all dependencies, it loaded.

Index page doesn't work but api works on AWS beanstalk Sprint Boot

I am trying to deploy a spring boot application to aws.
I changed the port to 5000 via application.properties
Now, url/api/hello works fine but url/ doesn't work
I am getting a white label error.
package com.example.package-name;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
#EnableAutoConfiguration
#SpringBootApplication
#ComponentScan()
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
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>package-name</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>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Can anyone tell me how to fix this?
Is there anything wrong with the main Starter file?
Note: Everything works fine locally, the error page is loading instead
of index page only in AWS
I assume you deploy in aws elastic beanstalk tomcat server, please make sure access via beanstalk URL which will give you after uploading jar/war, generally whitespace error gives you whenever you try to access wrong URL/wrong parameters, if possible provide details with screenshots.

Categories

Resources