【问题标题】:springboot could not found feignclientspringboot找不到feignclient
【发布时间】:2018-06-24 17:34:51
【问题描述】:

错误信息如下:

***************************
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 接口实现。

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

测试通用:

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 放在一起,以便我们可以优雅地管理它们?

或者只有使用它们冗余的方法?

谢谢!

【问题讨论】:

    标签: spring-boot spring-cloud spring-cloud-feign


    【解决方案1】:

    使用配置的解决方案

    • 如果您使用 Swagger Codegen,您可以使用配置来引导 api:
    @Configuration
    public class ParkingPlusFeignClientConfiguration {
    
      @Autowired
      private ParkingPlusProperties properties;
    
      @Bean
      public ServicoPagamentoTicket2Api ticketApi() {
        ApiClient client = new ApiClient();
        // https://stackoverflow.com/questions/42751269/feign-logging-not-working/59651045#59651045
        client.getFeignBuilder().logLevel(properties.getClientLogLevel());
        client.setBasePath(properties.getHost());
        // Generated from swagger: https://demonstracao.parkingplus.com.br/servicos
        return client.buildClient(ServicoPagamentoTicket2Api.class);
      }
    
    }
    

    【讨论】:

      【解决方案2】:

      Feign 不知道@ComponentScan

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

      【讨论】:

      • 你知道2.0.0.M+版本是否解决了吗?
      猜你喜欢
      • 2021-07-15
      • 2017-04-12
      • 2016-08-15
      • 2018-09-20
      • 2021-09-15
      • 2015-08-09
      • 2017-02-01
      • 2016-11-28
      • 2019-08-27
      相关资源
      最近更新 更多