Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Automation Job Top
We’re looking for a Backend Java/Spring Developer with Integration Experience: Read More

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:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.1.2</version>
</dependency>

Or

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

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 :

>> CHECK OUT THE COURSE
REST footer banner
Comments are closed on this article!