试图用OpenFeign客户端访问我的REST api。它应该返回String,但我得到的只是NULL。尝试了所有可能的解决方案,但没有任何效果。
我的POM文件:
"\033[1m$1\033[m"
然后我的主类以及FeignClient依赖项也被注入的类:
<?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>
还有FeignClient类:
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());
}
}
}
任何帮助将不胜感激。
谢谢
答案 0 :(得分:0)
它不起作用,因为您是手动feignClientObject
创建new feignClientObject();
实例
FeignclientApplication
类添加注释@EnableFeignClients FeignClientObject
类如果您想直接从IOC获取Bean,则:
ConfigurableApplicationContext context = SpringApplication.run(FeignclientApplication.class, args);
feignClientObject client = context.getBean(feignClientObject.class);
P.S。也请看看java class name convention:
feignClientObject
至FeignClientObject