簡體   English   中英

FeignClient 返回 null

[英]FeignClient returning null

一直在嘗試使用 OpenFeign 客戶端訪問我的 REST api。 它應該返回字符串,但我得到的只是 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>

然后我的主要 class 加上 class 也注入了 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 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();
}

任何幫助將不勝感激。

謝謝

它不起作用,因為您手動創建feignClientObject實例new feignClientObject();

  1. 首先,您必須在FeignclientApplication class 中添加注解@EnableFeignClients
  2. @Component 或@Service添加到FeignClientObject class

如果要直接從IOC獲取 bean,則:

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

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

PS 也請看一下java class 名稱約定feignClientObject to FeignClientObject

需要啟用 Feign Client。

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

可以通過@Autowire使用 class StringAppNameClient的 object 。

 @Autowire
 private StringAppNameClient stringAppNameClient;

然后調用

 stringAppNameClient.getAppName();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM