Spring Boot找不到FeignClient。

17

以下为错误信息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field helloAgent in com.example.client.controller.Hello required a bean of 
type 'com.example.common.agent.HelloAgent' that could not be found.

Action:

Consider defining a bean of type 'com.example.common.agent.HelloAgent' in 
your configuration.

项目结构:

模块: test-client 作为 feignclient 调用方。

模块: test-server 作为 feignclient 接口实现。

模块: test-common 把所有的 feignclient 放在一起。

test-common:

package com.example.common.agent;
@FeignClient("hello")
public interface HelloAgent {
    @GetMapping("/hello")
    String hello(@RequestParam String msg);
}

测试服务器:(正常运行)

package com.example.server.controller;

@RestController
public class Hello implements HelloAgent {
    @Override
    public String hello(@RequestParam String msg) {
        System.out.println("get " + msg);
        return "Hi " + msg;
    }
}

测试客户端:

package com.example.client.controller;

@RestController
public class Hello {
    @Autowired
    private HelloAgent helloAgent;

    @GetMapping("/test")
    public String test() {
        System.out.println("go");
        String ret = helloAgent.hello("client");
        System.out.println("back " + ret);
        return ret;
    }
}
----------------------------
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.common.agent","com.example.client.controller"})
public class TestClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestClientApplication.class, args);
    }
}

有没有办法将所有的feignclient放在一起,以便我们可以优雅地管理它们?

还是只能使用它们的冗余方式?

谢谢!

2个回答

54

Feign不知道有关 @ComponentScan 的信息。

使用 @EnableFeignClients(basePackages = {"com.example.common.agent","com.example.client.controller"})


你知道 2.0.0.M+ 版本中是否已解决了这个问题吗? - Daniel Hajduk

0

使用配置文件的解决方案

  • 如果您正在使用Swagger Codegen,可以使用配置文件来启动API:
@Configuration
public class ParkingPlusFeignClientConfiguration {

  @Autowired
  private ParkingPlusProperties properties;

  @Bean
  public ServicoPagamentoTicket2Api ticketApi() {
    ApiClient client = new ApiClient();
    // https://dev59.com/QlgQ5IYBdhLWcg3wcjks#59651045
    client.getFeignBuilder().logLevel(properties.getClientLogLevel());
    client.setBasePath(properties.getHost());
    // Generated from swagger: https://demonstracao.parkingplus.com.br/servicos
    return client.buildClient(ServicoPagamentoTicket2Api.class);
  }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接