Introduction To Open Feign

Overview

This is the first part of articles series about the Open Feign framework. In this part we will get the Introduction to Open Feign.


Feign, rather Open Feign is a Java to HTTP client binder (that’s what the GitHub project says), so what does it means actually?

Basically it means that this framework will generate the concrete implementation for the REST client interface, annotated by the Feign annotations.

But why do we need this? I mean we can write our own REST client using RestTemplate or Apache HTTP client, can’t we? Yes, we can but there are some problems with that and those are,

  • We will be writing boilerplate code for creating request and handling response
  • Of course, we have to write the test cases for the code which we have written in our REST client.
  • This is going to consume the time!

Feign Configuration

To use Feign in our project we need to add following dependencies in our pom.xml in Maven project,

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-core</artifactId>
    <version>10.5.1</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-jackson</artifactId>
    <version>10.5.1</version>
</dependency>

if you are using Gradle then you can add following dependency,

compile group: 'io.github.openfeign', name: 'parent', version: '10.5.1', ext: 'pom'
compile group: 'io.github.openfeign', name: 'feign-jackson', version: '10.5.1'

The first dependency is for core Feign framework and second dependency is for the Jackson encoder/decoder which as the name suggests, is used to encode the request and decode the response received from the API.

Using Feign

For this tutorial we will use https://jsonplaceholder.typicode.com website as our API server. This website exposes some placeholder REST API endpoints which we can use for testing purpose. It’s a very useful website and you should surely check it out.

Domain Model

We will use Users endpoint to fetch list of users from API. To fetch the users data, we will need a model to hold the data,

@JsonIgnoreProperties(ignoreUnknown = true)
public class User {

    private long id;
    private String username;
    private String email;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

Feign Client

Now that we have our model, lets write our Feign client to fetch the data from the API,

public interface UserService {

    @RequestLine("GET /users")
    List<User> getUsers();

}

Yes, that’s it! This is our Feign client to fetch users from the API. Notice @RequestLine annotation. This annotation comes from Feign and the value specified in the annotation specifies the request to be made.

In this example, we are making a GET request to /users endpoint to fetch users data. Base URL will be specified later when we will build our client.

Building and Using Feign Client

To build our Feign client i.e. UserService, we will use Feign class from the Feign framework.

UserService userService = Feign.builder()
        .decoder(new JacksonDecoder())
        .target(UserService.class, "https://jsonplaceholder.typicode.com");

Above code will build our UserService which we can use to fetch the users from the API.

System.out.println(userService.getUsers());

And we are done, this was the Introduction to Open Feign framework! You see, how much it is easy to build REST clients using Feign.

Request Options

In this tutorial we saw how Feign allows us to make GET request to API server. Along with GET, feign also supports additional request types using respective keywords like,

  • @RequestLine(“POST “) for POST request
  • @RequestLine(“PUT “) for PUT request
  • @RequestLine(“DELETE “) for DELETE request

and more.

Conclusion

This was the Introduction to Open Feign. As you must have realized by now, how easy it is to build REST clients using Feign framework. It will generate the implementation that we would have to write and we do not have to write the test cases for it as it’s already done by the Feign developers!

Complete project for this tutorial is available at Github, you can download it from here.