Spring Cloud: Creating REST Client Using Feign

In this tutorial we will be exploring Spring Cloud Feign component.

What is Feign?

Feign is a declarative REST Client. With the help of Feign, you can declare web services as interfaces in your application. And you can simply use those interfaces to consume those web services. While runtime Feign will provide actual REST API implementation.

TL;DR You can download whole project by clicking following link.

To give you a brief idea… In this tutorial, we will be consuming web service using Feign.

Producer Service

We will need something to consume. So we will first have producer service first. We will be using producer service created in the previous tutorial. You can refer How to develop a eureka client application post.

We will need to make some changes in application.properties of the producer. We will add ‘server.port’ property since we will be using it in our consumer.


TL;DR you can download producer project by clicking on following link.

Consumer Service (Feign)

Let’s create a consumer service with Feign component.

This will be a Maven Project. Our project structure is given below. We will be creating five files which are as follows:

  1. AsmFeignConsumerApplication.java – Spring boot runable file
  2. Student.java – Our POJO/model
  3. StudentService.java – Service interface created using Feign
  4. ConsumerController.java – Rest controller
  5. application.properties – Spring properties file
Project Structure

Adding Dependencies

Let’s add the required dependencies. Replace content in pom.xml file with the following content. Don’t forget to update the project using Maven > Update option. That will download all the necessary dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath /> <!-- lookup parent from repository -->








If you have noticed, we have added ‘spring-cloud-starter-openfeign’. That is the required dependency.

Creating Spring Boot Application

Create file AsmFeignConsumerApplication.java and add the following content. This file will serve as the entry point for our application.

package com.example.asmfeignconsumer;

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

public class AsmFeignConsumerApplication {

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


We have added @EnableFeignClients annotation here. It will denote that this spring boot app is a Feign client.

Creating model

Create class Student.java. This will be our POJO/model class. It has only 3 attributes viz, id, name and enrolled course.

package com.example.asmproducer;

import java.io.Serializable;

public class Student implements Serializable {

	private int id;

	private String name;

	private String enrolledCourse;

	public Student() {

	public Student(int id, String name, String enrolledCourse) {
		this.id = id;
		this.name = name;
		this.enrolledCourse = enrolledCourse;

	public int getId() {
		return id;

	public void setId(int id) {
		this.id = id;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public String getEnrolledCourse() {
		return enrolledCourse;

	public void setEnrolledCourse(String enrolledCourse) {
		this.enrolledCourse = enrolledCourse;


Creating Feign Service Interface

Create an interface StudentService.java and paste following code. Here we will be specifying the URL of our producer service. We will be creating abstract methods as well with @RequestMapping annotations with method and value attribute same as what we specified in our producer service (or the service which we want to consume).

package com.example.asmfeignconsumer;

import java.util.List;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "StudentProducer", url="http://localhost:11801")
public interface StudentService {

	@RequestMapping(method = RequestMethod.GET, value = "/student/{studentId}")
	public Student getStudent(@PathVariable int studentId);

	@RequestMapping(method = RequestMethod.GET, value = "/student/all")
	public List<Student> getAllStudents();


Here if you have noticed, we have used @FeignClient annotation and we have specified “http://localhost:11801” as URL. This is where the producer service is running. Another thing if you have noticed, we have added @RequestMapping annotations with the same method and value attributes as of those in producer service. This annotation will create a mapping between those URLs.

Creating Controller

Create class ConsumerController.java and add the following content. This will be our rest controller and will have endpoint related stuff.

package com.example.asmfeignconsumer;

import java.util.List;

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

public class ConsumerController {

	private StudentService studentService;

	public List<Student> getAllStudents() {
		return studentService.getAllStudents();

	public Student getStudent(@PathVariable int studentId) {
		return studentService.getStudent(studentId);


In this case, we have auto-wired our interface and directly invoked its methods in our controller.

Adding Properties

Under Resources folder create a file with name application.properties and add the following content.


Now start both producer and consumer applications. Since both of our applications are Eureka Clients, you should able to see them in Eureka dashboard.

Eureka Dashboard

Let’s see if we can access our service or not. For that, you will need to navigate to ‘http://localhost:11802/studentdetails/all’ URL. You should see some result as shown in below screenshot.

Output 1

If you navigate to ‘http://localhost:11802/studentdetails/2’ URL, you should see some result as shown in below screenshot.

Output 2

Downloads for this tutorial:


  1. Spring Cloud- Netflix Feign Simple Example by JavaInUse

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.