EnableFeignClients and FeignClient annotation is not recognized in application class file - spring-boot

I am using spring boot starter parent version 2.6.3(latest version). I am using spring cloud version 2021.0.0 . I am using openfeign dependency in maven pom file. But, my import statements in the class are not recognizable. Also, I am getting red tick in pom.xml. If I comment out openfeign dependency , red tick disappears from pom.xml . I tried to check openfeign jar files content, it does not have FeignClient and EnableFeignClients class files. So, it does make sense to get error while importing in my application class file.
What should I do to fix the import statements? I would like to have working version of dependency. I saw earlier versions tutorial but however I am looking forward to work with current version. I tried to put older version like spring boot starter parent 2.4.8 with spring cloud dependencies 2020.0.3 but I am getting same error for openfeign class files.
Here is my code.
My application class file.
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
#Configuration
#EnableFeignClients
#EnableDiscoveryClient
public class HelloWorldClientConfig {
#Autowired
private TheClient theClient;
#FeignClient(name = "HelloWorld")
interface TheClient {
#RequestMapping(path = "/helloworld", method = RequestMethod.GET)
#ResponseBody
String helloWorld();
}
public String HelloWorld() {
return theClient.helloWorld();
}
}
Here 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>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.travelport.ts</groupId>
<artifactId>HelloWorldConsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloWorldConsumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- spring-cloud.version>Hoxton.SR4</spring-cloud.version-->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<!-- version>2.2.6.RELEASE</version-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.0</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>

Try adding another dependency in your pom.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>

Related

JSP file is not rendering in Spring-Boot after included dependencies

I am creating simple demo Spring boot application. I have included all required dependencies in pom.xml file for jsp and set in application.properties as well but still not working.
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.1</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>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-test</artifactId>
<scope>test</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project
controller.java
package com.example.demoController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class controller {
#RequestMapping("/")
String first() {
return "index";
}
}
here is the project directory tree
please suggest something to resolve this problem.
Your JSP file should be in by default "src/main/webapp/WEB-INF/jsp" directory and right now it seems to be in wrong location.
UPDATE:- Looking at your shared code - the reason you are facing this problem is that you have annotated your Controller with #RestController instead of #Controller because of which the String you are returning from your controller is being written directly on the output stream instead of framework first checking the view name against a JSP template and then rendering it. If you change the annotation to #Controller it works fine

Unable to import JdbcBatchItemWriterBuilder in spring boot 1.5.7 version

I am trying to write below code in the spring boot 1.5.7 version.
#Bean
public JdbcBatchItemWriter<WriterSO> writer(DataSource dataSource, ItemPreparedStatementSetter<WriterSO> setter) {
return new JdbcBatchItemWriterBuilder<WriterSO>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.itemPreparedStatementSetter(setter)
.sql("insert into writer (id, fullName, randomNum) values (?,?,?)").dataSource(dataSource).build();
}
but unable to use or import the package belongs to JdbcBatchItemWriterBuilder. does this package is not available in springboot 1.5.7?
Below is my pom.xml which is using in the project :
<?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>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.batch</groupId>
<artifactId>Batchwithpostgres</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Batchwithpostgres</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-data-jpa</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</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
>
Any suggestions to write alternate code whcich works in springboot 1.5.7
According to JdbcBatchItemWriterBuilder javadoc, it was added in version 4, and Spring Boot 1.5.7 uses version 3 of Spring Batch, therefore it is not available to you.
You can use the constructor and set all the required fields:
#Bean
public JdbcBatchItemWriter<WriterSO> writer(DataSource dataSource, ItemPreparedStatementSetter<WriterSO> setter) {
JdbcBatchItemWriter<WriterSO> jdbcBatchItemWriter = new JdbcBatchItemWriter<>();
jdbcBatchItemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<WriterSO>());
jdbcBatchItemWriter.setItemPreparedStatementSetter(setter);
jdbcBatchItemWriter.setSql("insert into writer (id, fullName, randomNum) values (?,?,?)");
jdbcBatchItemWriter.setDataSource(dataSource);
return jdbcBatchItemWriter;
}
Spring Boot 1.5.7 brings Spring Batch 3.0.8 which does not provide builders for item readers and writers.
These builders were introduced in Spring Batch 4 which can be consumed using Spring Boot 2.x.

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 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.

Resources