javaspring-bootspring-cloud-feignfeign

Can i use Java Feign client to request for another API?


I have microservice why do some calculations. And there is public api where i can get some data. Can i use Feign to do request to this API?


Solution

  • Yes, you can do that :

    Step 1: create feign client

    @FeignClient(name="DemoClient", url="url of the api")
    public interface DemoClient {
        @GetMapping(value="/get-demo",consumes=MediaType.APPLICATION_JSON_VALUE)
        List<Model> getData();
    } 
    

    Step 2 : Create Model

    class Model {
    
    }
    

    Step 3 : Create Service Layer

    class DemoService {
    
       @Autowired
       DemoClient demoClient;
    
       public void getData() {
           List<Model> objData = this.democlient.getData();
       }
    
    } 
    

    Hope this will help