Spring Boot FeignClient返回null

af7jpaap  于 2023-06-22  发布在  Spring
关注(0)|答案(2)|浏览(286)

我一直在尝试使用OpenFeign客户端访问我的REST API。它应该返回String,但我得到的全部是NULL。尝试了所有可能的解决方案,但没有任何效果。
我的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 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());
        }
    }
}

FeignClient类:

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();
}

任何帮助都将不胜感激。
谢谢你

a14dhokn

a14dhokn1#

无法使用,因为您手动创建feignClientObject示例new feignClientObject();
1.首先,您必须向FeignclientApplication类添加annotation @EnableFeignClients
1.将@Component或@Service添加到FeignClientObject
如果你想直接从IOC获取bean,那么:

ConfigurableApplicationContext context = SpringApplication.run(FeignclientApplication.class, args);

 feignClientObject client = context.getBean(feignClientObject.class);

另外,请看java class name conventionfeignClientObjectFeignClientObject

vyswwuz2

vyswwuz22#

需要启用伪客户端。

@EnableFeignClients(basePackages = {"com.example.feignclient"}) // Required
 @SpringBootApplication
 public class FeignclientApplication {
 .....

StringAppNameClient的对象可以通过@Autowire使用。

@Autowire
 private StringAppNameClient stringAppNameClient;

然后调用

stringAppNameClient.getAppName();

相关问题