17

ERROR INFO LIKE BELOW:

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

project structure:

module: test-client as feignclient caller.

module: test-server as feignclient interface implementation.

module: test-common put all feignclient together.

test-common:

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

test-server:(works fine)

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

test-client:

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

Is there anyway to put all feignclient together so that we can manage them gracefully?

Or there is only way to use them redundancy?

THANKS!

ajianzheng
  • 283
  • 2
  • 3
  • 11

2 Answers2

53

Feign doesn't know about @ComponentScan.

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

spencergibb
  • 24,471
  • 6
  • 69
  • 75
0

Solution using Configuration

  • In case you use Swagger Codegen, you can use a configuration to bootstrap the apis:
@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);
  }

}
Marcello DeSales
  • 21,361
  • 14
  • 77
  • 80