springboot找不到feignclient

时间:2018-01-16 04:12:15

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

错误信息如下:

***************************
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.

项目结构:

module:test-client as feignclient caller。

module:test-server as feignclient interface implementation。

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

测试共同:

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

test-server :(工作正常)

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

有没有把所有假装客户放在一起,以便我们可以优雅地管理它们?

或者只有这种方法可以使用冗余吗?

谢谢!

2 个答案:

答案 0 :(得分:6)

Feign不了解FAIRVILLE NY DPS 7026||WALMART SFAIRVILLUTUSA WALMART DEPOT 901||PRICEWALMART SLC DRY A0901

使用@ComponentScan

答案 1 :(得分:0)

使用配置的解决方案

  • 如果您使用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);
  }

}