been trying to access my REST api with OpenFeign client. It should return String but all I get is NULL. Tried every possible solution but nothing worked.
My POM 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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>feignclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feignclient</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-client</artifactId>
<scope>runtime</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>
<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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
Then my main class plus class that FeignClient dependency gets injected too:
package com.example.feignclient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FeignclientApplication {
public static void main(String[] args) {
SpringApplication.run(FeignclientApplication.class, args);
//System.out.println(strAppNameClient.getAppName());
feignClientObject feignClientObj = new feignClientObject();
}
}
package com.example.feignclient;
import org.springframework.beans.factory.annotation.Autowired;
public class feignClientObject {
@Autowired
StringAppNameClient strAppNameClient;
public feignClientObject() {
try {
System.out.println(strAppNameClient.getAppName());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
And the FeignClient class:
package com.example.feignclient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "SPRING-CLOUD-EUREKA-CLIENT")
public interface StringAppNameClient {
@GetMapping(value= "/api/AppName")
String getAppName();
}
Any help would be deeply appreciated.
Thank you
It doesn't work because you create feignClientObject
instance manually new feignClientObject();
FeignclientApplication
classFeignClientObject
classIf you want to directly get the bean from the IOC , then:
ConfigurableApplicationContext context = SpringApplication.run(FeignclientApplication.class, args);
feignClientObject client = context.getBean(feignClientObject.class);
PS also please have a look at java class name convention : feignClientObject
to FeignClientObject
Feign Client needs to be enabled.
@EnableFeignClients(basePackages = {"com.example.feignclient"}) // Required
@SpringBootApplication
public class FeignclientApplication {
.....
An object of the class StringAppNameClient
can be consumed through @Autowire
.
@Autowire
private StringAppNameClient stringAppNameClient;
Then invoke
stringAppNameClient.getAppName();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.