Interface bean is not created in Spring Boot 3.2.3 microservice

37 Views Asked by At

First Micro service:

@RestController
@RequestMapping("/orders")
public class MyController {
    @Autowired
    private SecondInterface secondInterface;
    
    @GetMapping
    public ResponseEntity<List<Integer>> getAllOrders(){
    List<Integer> orderIds = secondInterface.getAllOrders().getBody();
    return new ResponseEntity<>(orderIds, HttpStatus.OK);}
}


@FeignClient("SECOND-SERVICE")
public interface SecondInterface {
    @GetMapping("/order")
    public ResponseEntity<List<Integer>> getAllOrders();
}

Second Micro service:

@RestController
public class MyController {
    
    @GetMapping("/order")
    public ResponseEntity<List<Integer>> getAllOrders(){
        List<Integer> orderIds = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        return new ResponseEntity<>(orderIds, HttpStatus.OK);
    }
}

Both services are registered in Eureka service registry. When I start the First Micro service, I got the below error.

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

Description:

Field secondInterface in com.tutorial.firstservice.controller.Implement required a bean of type 'com.tutorial.firstservice.controller.SecondInterface' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.tutorial.firstservice.controller.SecondInterface' in your configuration.

Approach1: I have changed that SecondInterface to class then bean was created successfully. But when I used interface then bean is not created.

Approach2: If I change @Autowired to @Qualifier then service start successfully, but when API call SecondInterface object is null because bean is not created, even I have mentioned @Component annotation as well.

0

There are 0 best solutions below