Leichtgewichtige REST-Clients mit Feign

Lightweight REST Clients with Feign (Feign is fine)

Problem: Seperate REST Clients for Every Direct Communication

Within the Microservices/REST “universe”, a separate client is required for each direct communication between services. Writing these clients is simple, but not very exciting, and leads to boilerplate code.

Solution: Minimal Code Usage in Spring Thanks to Feign

The declarative REST-client Feign allows in Spring applications to perform this task with minimal code usage.

Advantages / Features:

  • freely configurable encoders/decoders (JSON, XML, ...), loggers, error handlers and interceptors (e.g. for OAuth)
  • compatible with JAX-RS annotations, Spring Cloud, Hystrix, Spring MVC annotations, Spring Web HttpMessageConverter
  • allows load balancing (with Ribbon and Eureka)
  • more complex clients can be realized with the Feign Builder API

Example for a Feign Client

@FeignClient(name = "coffeeMachineClient", configuration = Config.class, url = "${myUrl}")
public interface ICoffeeMachineCient {

    @RequestMapping(method = POST, path = "/coffees")
    CupOfCoffeeDTO createCoffee(@RequestHeader("userId") Id uId, CupOfCoffeeDTO request);

    @RequestMapping(method = GET, path = "/coffees")
    List<CupOfCoffeeDTO> getCoffees();

    @RequestMapping(method = GET, path = "/coffees/{id}")
    CupOfCoffeeDTO getCoffeeById(@RequestParam("id") String coffeeId);

    @RequestMapping(method = PUT, path = "/coffees/{id}")
    CupOfCoffeeDTO updateCoffee(@RequestParam("id") String id, CupOfCoffeeDTO request);

    @RequestMapping(method = DELETE, path = "/coffees/{id}")
    void deleteCoffee(@RequestParam("id") String id); //who would do this?
→ less than 15 lines of code for a complete CRUD client.Using Feign particularly pays off in case of many different requests having to be issued against a particular server. Caution: Feign clients can’t handle binary data (e.g. file upload/download), but only text-based interfaces!
Lightweight REST Clients with Feign

Cookie Settings

This website uses cookies to personalize content and ads, provide social media features, and analyze website traffic. In addition, information about your use of the website is shared with social media, advertising, and analytics partners. These partners may merge the information with other data that you have provided to them or that they have collected from you using the services.

For more information, please refer to our privacy policy. There you can also change your cookie settings later on.

contact icon

Contact us now