I'm trying to run a basic Spring Boot code but it is throwing some errors - spring-boot

This is the only file that I created to Start learning Spring Boot.
I'm trying to run a basic Spring Boot code but it is throwing a couple of errors that I couldn't figure it out.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CourseApiApp {
public static void main(String[] args) {
SpringApplication.run(CourseApiApp.class, args);
}
}
Console output:
***************************
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.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:436)
The following method did not exist:
org.springframework.beans.factory.annotation.InjectionMetadata.forElements(Ljava/util/Collection;Ljava/lang/Class;)Lorg/springframework/beans/factory/annotation/InjectionMetadata;
The method's class, org.springframework.beans.factory.annotation.InjectionMetadata, is available from the following locations:
jar:file:/Users/kranthitalloju/.m2/repository/org/springframework/spring-beans/5.0.3.RELEASE/spring-beans-5.0.3.RELEASE.jar!/org/springframework/beans/factory/annotation/InjectionMetadata.class
The class hierarchy was loaded from the following locations:
org.springframework.beans.factory.annotation.InjectionMetadata: file:/Users/kranthitalloju/.m2/repository/org/springframework/spring-beans/5.0.3.RELEASE/spring-beans-5.0.3.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.annotation.InjectionMetadata
Here is the pom.xml file I'm using to deploy the basic Spring Boot app.
<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.codeapi.springboot</groupId>
<artifactId>APIDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Java SprinBoot Course API</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
</dependencies>
</project>

Since you're using the spring-boot-starter-parent, many versions are managed for you out of the box. Also, the spring-boot-starter-web already includes your other dependencies.
You can simplify your pom.xml like so:
<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>
<name>Java SprinBoot Course API</name>
<groupId>org.codeapi.springboot</groupId>
<artifactId>APIDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

Related

Spring Boot Application:- The import org.springframework.boot.SpringApplication cannot be resolved

I'm beginner for Spring-boot;refering some tutorials I have set up a spring boot project using the Spring Initializer. I have tried to get rid of this issue by downloading dependencies again and even created new projects several times.
I'm using Eclipse neon IDE and I find error on imports import org.springframework.boot.SpringApplication.
I have removed repositories too many times so that jars will auto downloaded by itself when I refresh\clean the project but problem still persists.
DemoApplication.java
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);
}
}
below is my pom.xml
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</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>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Try doing this, I think this would solve
public static void main(final String[] aArgs)
{
new SpringApplicationBuilder(DemoApplication.class).run(aArgs);
}
within Eclipse: Right click Project -> Maven -> Update project should work
It was eclipse issue I found corrupted jars in repository and upgraded m2e to 1.8(solution found in Eclipse forum). After upgrading m2e I had removed my repository and restarted eclipse. Everything worked smooth 🙂

Turbine not working with spring boot 2.0.6 and spring cloud Finchley.SR1

I am trying to implement a simple service which will use turbine, but it doesn't work - turbine not getting any data from other Hystrix-used services.
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>jar</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.6.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>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.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>
</project>
application.properties
server.port=5000
spring.application.name=turbine-aggregator
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
turbine.app-config=weather-app
turbine.cluster-name-expression='default'
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
#SpringBootApplication
#EnableTurbine
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
I can see in the output that it recognize my Hystrix-used app:
click here for snapshot
but in turbine stream it looks empty:
click here for snapshot
If someone know what I did in the wrong way please let me know. Thanks.
At the end - it was my fault.
Since the Hystrix client had an older version of spring boot/cloud it "broadcast" the hystrix stream on ../hystrix.stream, but in the version which I used for turbine it aggregated streams from ../actuator/hystrix.stream.
So I just updated the versions in my Hystrix client.

Spring Data Source is an unknown property?

I am trying to configure my data source in application properties resource and getting a yellow warning that spring data source is an unknown property. Error is coming up with all pasted line below.
How to properly configure it so i can get rid of this error?
I have checked the pom.xml and mysql and jpa dependencies are also imported correctly.
This is my first project so i don't know how to solve this error. I think if i dont resolve it here it will turn into more severe error.
spring.datasource.url=jdbc:mysql://localhost:3306/projectdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
Here's the main class code:
package com.bilal.student.dal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class StudentdalApplication {
public static void main(String[] args) {
SpringApplication.run(StudentdalApplication.class, args);
}
}
Here's the 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bilal.student.dal</groupId>
<artifactId>studentdal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>studentdal</name>
<description>STUDENT DAL</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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>
I have resolved the error #Martin Zaragoza. Actually problem was in my project build path. I have deleted the .m2 folder in my C:\Users\hp.m2 and all the error gone. Now i am successfully building my project. Thanks for sticking up with me.

Spring Boot - Run a WAR which is injected as a dependency as a standalone JAR

I am trying to move a Spring WebMVC project to containerless architecture. I thought to start by having a simple Spring Boot project which only needs to start the embedded WAR file, and inject all the beans as normal. In my Application.java class, I have the following:
package demo.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
#SpringBootApplication
#ImportResource("classpath:beans/beans.xml")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
In my pom.xml, I have included the Spring WebMVC project as a dependency:
<dependency>
<groupId>com.other.app</groupId>
<artifactId>CoolWARApplication</artifactId>
<version>${app.version}</version>
</dependency>
My full pom:
<?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.springbootapp</groupId>
<artifactId>springbootapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</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.5.9.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>
<app.version>3.0.27-SNAPSHOT</app.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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.other.app</groupId>
<artifactId>CoolWARApplication</artifactId>
<version>${app.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
In my project structure in Eclipse, I can see this project and all of its dependencies being pulled in. However, when I start the Spring Boot app, I get the following error:
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
I'm not sure what I'm missing here. I thought that importing the WebMVC project as a dependency and injecting its beans into the Spring Boot application would run the project as normal, only inside a standalone JAR file. Is it advisable, or even possible, to do the above as described?
Thanks in advance.

How to create a multi-module maven project with spring boot

I am really new to spring boot and maven. I have tried to create a multi-module spring boot project using maven. When I create that and tried to access my controller endpoint through the url it will display 404 error.
I have tried a lot to resolve this problem and searched in the online documents but I was failed. Can anybody help me to resolve this.
In my project there are two modules called demo and activityops.
the parent 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>org.multimodule</groupId>
<artifactId>multimodule</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>activityops</module>
<module>demo</module>
</modules>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>com.example.DemoApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</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>
pom.xml in the activityops module is below
<?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>multimodule</artifactId>
<groupId>org.multimodule</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>activityops</artifactId>
pom.xml in the demo module is below
<?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>multimodule</artifactId>
<groupId>org.multimodule</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
</project>
My controller class is below
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.ws.rs.core.MediaType;
#org.springframework.web.bind.annotation.RestController
#RequestMapping("/user")
public class TestController {
private static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(TestController.class);
#RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON})
public User getSingleUser() {
User user = new User();
user.setId(11l);
user.setFirstName("Shane");
user.setLastName("John");
return user;
}
}
I have typed the following url to access the controller method.
http://localhost:8080/user
This will display and 404 error. Can anybody please help me to resolve this?
Thanks
There is quite a lot going on in your project right now but the biggest problem is that your demo project has no dependency on the other module.
Also, that module defines your controller in the root package (don't do that!). Your Spring Boot application is located in com.example. By default, Spring Boot will scan the package where your Spring Boot application is located and all its sub-packages. TestController is not in com.example so it's not found. That's why you get a 404.
There were other issues, I have submitted a pull request that demonstrates what you can do to make this sample project works. Check the commit message for more details.

Resources