oznomal
oznomal

Reputation: 449

Using Feign in a standalone library vs a Springboot application

I want to create a client for Some-Micro-Service as a library (Some-Micro-Service-Client) that way it can easily be included in the pom of Some-Other-Micro-Service.

I would like to use Feign because it makes things easier, but I am not sure if this is possible with my architecture. All of the Feign examples that I see start with using the @EnableFeignClient annotation on the SpringBootAppplication class, but since I don't want the client library to have to be "started up" I want to know if it is possible just to include it in the library without using the EnableFeignClient annotation.

Upvotes: 3

Views: 2427

Answers (1)

Golov Pavel
Golov Pavel

Reputation: 672

Yes, you can use feign without @EnableFeingClient annotation. Assume, we want to receive data from this API. In below example I used Feign Core and Feign Gson dependencies.

First of all we need to create class, in which we will get the json result:

public class TODO {
    private long id;
    private long userId;
    private String title;
    private boolean completed;

    \\ getters and setters ...
}

After that we declare the interface with the future rest-client methods:

public interface TaskApi {

    @RequestLine("GET /todos/{id}")
    TODO getTODO(@Param("id") int id);
}

And in conclusion let's build desired rest client and make test request:

public class FeignTest {

    private static final String API_PATH = "https://jsonplaceholder.typicode.com";

    public static void main(String[] args) {
        TaskApi taskApi = Feign.builder()
                .decoder(new GsonDecoder())
                .target(TaskApi.class, API_PATH);
        TODO todo = taskApi.getTODO(1);
    }
}

For more information and possibilities you can read in official repository.

Upvotes: 2

Related Questions