0
0
SpringbootHow-ToBeginner · 4 min read

How to Use Feign Client in Spring Boot: Simple Guide

To use Feign Client in Spring Boot, add the @EnableFeignClients annotation to your main application class and define an interface annotated with @FeignClient specifying the service name or URL. Then, inject this interface where needed to call remote REST endpoints declaratively.
📐

Syntax

The basic syntax involves three parts:

  • @EnableFeignClients on your Spring Boot application class to enable Feign support.
  • An interface annotated with @FeignClient(name = "service-name") that declares methods matching the remote REST endpoints.
  • Use Spring's @Autowired or constructor injection to use the Feign client interface in your services or controllers.
java
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableFeignClients
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

@FeignClient(name = "user-service", url = "http://localhost:8081")
public interface UserClient {
    @GetMapping("/users/{id}")
    User getUserById(@PathVariable("id") Long id);
}
💻

Example

This example shows a Spring Boot app calling a remote user service using Feign Client. The UserClient interface declares a method to get user details by ID. The UserController uses this client to fetch and return user data.

java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

@SpringBootApplication
@EnableFeignClients
public class FeignExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignExampleApplication.class, args);
    }
}

@FeignClient(name = "user-service", url = "http://localhost:8081")
interface UserClient {
    @GetMapping("/users/{id}")
    User getUserById(@PathVariable("id") Long id);
}

@RestController
class UserController {
    private final UserClient userClient;

    @Autowired
    public UserController(UserClient userClient) {
        this.userClient = userClient;
    }

    @GetMapping("/fetch-user/{id}")
    public User fetchUser(@PathVariable("id") Long id) {
        return userClient.getUserById(id);
    }
}

class User {
    private Long id;
    private String name;

    // getters and setters
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}
Output
When calling GET /fetch-user/1, the app returns JSON user data fetched from http://localhost:8081/users/1, e.g., {"id":1,"name":"Alice"}
⚠️

Common Pitfalls

Common mistakes when using Feign Client include:

  • Forgetting to add @EnableFeignClients in the main application class, so Feign clients are not scanned.
  • Not specifying the correct name or url in @FeignClient, causing connection errors.
  • Missing @PathVariable or @RequestParam annotations on method parameters, leading to wrong request URLs.
  • Not including the spring-cloud-starter-openfeign dependency in pom.xml or build.gradle.
java
/* Wrong: Missing @EnableFeignClients */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

/* Right: Add @EnableFeignClients */
@SpringBootApplication
@EnableFeignClients
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
📊

Quick Reference

StepDescription
Add DependencyInclude spring-cloud-starter-openfeign in your build file.
Enable FeignAdd @EnableFeignClients to your main Spring Boot class.
Create InterfaceDefine interface with @FeignClient and REST method mappings.
Inject ClientUse @Autowired or constructor injection to use the client.
Call MethodsCall interface methods to invoke remote REST endpoints.

Key Takeaways

Add @EnableFeignClients to enable Feign support in your Spring Boot app.
Define interfaces annotated with @FeignClient to declare remote REST calls.
Inject and use Feign client interfaces to call services declaratively.
Always include spring-cloud-starter-openfeign dependency in your project.
Annotate method parameters properly with @PathVariable or @RequestParam.