Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:


1. Overview

In this tutorial, we'll demonstrate how to use @RequestLine annotation in Feign client. @RequestLine is a template for defining the URI and query parameter for connecting with a RESTful web service.

2. Maven Dependency

To begin, let's create a Spring Boot web project and include the spring-cloud-starter-openfeign or feign-core dependency to our pom.xml file. The spring-cloud-starter-openfeign includes feign-core dependency within it:




3. @RequestLine in Feign Client

The @RequestLine Feign annotation specifies the HTTP verb, path, and request parameters as arguments in the Feign client. The path and request parameters are specified using the @Param annotation.

Normally in a Spring Boot application, we'd use @FeignClient, but we can also use @RequestLine if we don't want to use the spring-cloud-starter-openfeign dependency. Using that dependency will give us an IllegalStateException if we use @RequestLine with @FeignClient.

The @FeignClient annotation's String value is an arbitrary name that is used to create a Spring Cloud LoadBalancer client. We may additionally specify a URL and other parameters based on the requirements.

Let's create an interface for using @RequestLine:

public interface EmployeeClient {
    @RequestLine("GET /empployee/{id}?active={isActive}")
    @Headers("Content-Type: application/json")
    Employee getEmployee(@Param long id, @Param boolean isActive);

We should also provide @Headers where we define headers required by the rest API.

Now, we'll call the interface thus created to call the actual API:

EmployeeClient employeeResource = Feign.builder().encoder(new SpringFormEncoder())
  .target(EmployeeClient.class, "http://localhost:8081");
Employee employee = employeeResource.getEmployee(id, true);

4. Conclusion

In this article, we've demonstrated how and when @RequestLine annotation is used in Feign Client.

As is the custom, all code samples used in this tutorial are available on GitHub.

REST bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course :

REST footer banner
Comments are closed on this article!