Feign Client In Spring Boot

FeignClient is a Declarative REST Client in Spring Boot Web Application. FeignClient is used to call RESTFul API endpoints exposed by third-party or microservice.Declarative REST Client means you just give the client specification as an Interface and spring boot takes care of the implementation for you.

Rather than using RestTemplate to call an external API , we can use Feign Client too , follow this session Step by Step To Understand feign client.

Step 1 : Create New Spring boot starter project , with dependency as Spring Web :

create a UserController class as below :

package com.suv;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

	@GetMapping("/name")
	public String getName() {
		return "Swapnil V";
	}
	
	@GetMapping("/address")
	public String getAddress() {
		return "Pune";
	}
	
	@GetMapping("/status")
	public String getStatus() {
		return "active";
	}
}

Run your spring Boot Application and check all end point in browser :

http://localhost:8080/user/status

http://localhost:8080/user/name

http://localhost:8080/user/address

Step 2 : Create New Spring Boot Starter Project with dependency : Spring Web and Open Feign , In this new project we will create a new API , which will call to above API which is running on 8080.

At Main class add annotation @EnableFeignClients as below :

package com.suv;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class DemoApiApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApiApplication.class, args);
	}

}

2.1- create an Interface for Feign Client As below, all method declaration signature must be same as per API endpoints which we want to call , in url , we have to mention address of API which we want to call :

package com.suv;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "feignClient", url="http://localhost:8080/user")
public interface FeignClientForUserApi {

	@GetMapping("/name")
	public String getName();
	
	@GetMapping("/address")
	public String getAddress();
	
	@GetMapping("/status")
	public String getStatus();
}

Create new controller as below , We can autowire above interface and call external API endpoints directly as below :

package com.suv;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {

	@Autowired
	FeignClientForUserApi fc;
	
	@GetMapping("/dname")
	public String callUserApiName() {
		
		return fc.getName();
	}

	@GetMapping("/daddress")
	public String callUserApiAddress() {
		
		return fc.getAddress();
	}
	
	@GetMapping("/dstatus")
	public String callUserApiStatus() {
		
		return fc.getStatus();
	}
}

2.2-for second api change port number as 8082 , and we can directly get response from first API which is running on 8080 :

http://localhost:8082/demo/dstatus

http://localhost:8082/demo/daddress

http://localhost:8082/demo/dname

check Endpoints in browser :

Happy Learning

Leave a Reply

Your email address will not be published. Required fields are marked *