By Smitheo
Jan 03 2025
Integrating REST APIs into Adobe Experience Manager (AEM) projects can be complex, often requiring significant boilerplate code. Feign HTTP Client simplifies this process, enabling developers to reduce setup complexity while maintaining flexibility and scalability. In this post, discover how Feign streamlines REST API interactions, making it easier to configure, manage, and optimize AEM integrations. Whether you're building for speed, reliability, or scalability, this guide will show you how to transform your API workflows.
In projects, we often have some integrations with the API layer of 3rd party systems. This means that on our side we need to prepare an HTTP client to be able to consume those endpoints. Based on API specification we need to prepare Resource representation classes. Then, the HTTP client with some configurations, like headers, timeouts, and error handling. Parse request and response payloads into objects and many other things. All of this in the end creates a lot of boilerplate code.
Inspired by my latest project I want to introduce Feign HTTP client. Feign is an HTTP client whose goal is to simplify writing HTTP clients. The goal is to reduce the complexity of REST API integrations.
With Feign we need to declare and annotate an interface based on API specification. In the background, Feign will process annotations into templated requests. Feign allows you to configure different HTTP clients, JSON/XML processors, metric providers, loggers, and some other features.
Throughout this article, we will integrate Faker APIs in Adobe Experience Manager (AEM) project. Goal is to get some fake generated data about users and addresses.
First, in your AEM project add needed dependencies.
<!-- Feign -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>11.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
<version>11.0</version>
</dependency>
Besides the feign-core, additionally, we'll use feign-Jackson for JSON processing. Feign-jackson requires jackson-core, Jackson-annotations, and jackson-databind dependencies which are already provided by AEM.
To prevent unresolved import packages, we need to install those dependencies in the Apache Felix Web Console or embed them into our project bundle.
Embed dependencies in filevault-package-maven-plugin configuration.
<embedded>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<target>/apps/aem-feign-vendor-packages/application/install</target>
</embedded>
<embedded>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
<target>/apps/aem-feign-vendor-packages/application/install</target>
</embedded>
First of all, we need to prepare Resource representation classes.
@Data
public class Users {
private String uuid;
private String firstname;
private String lastname;
private String username;
private String password;
private String email;
private String ip;
private String macAddress;
private String website;
private String image;
}
@Data
public class Address {
private String street;
private String streetName;
private String buildingNumber;
private String city;
private String zipcode;
private String country;
@JsonProperty("county_code")
private String countyCode;
private Double latitude;
private Double longitude;
}
@Data
public class Response<T> {
private String status;
private int code;
private long total;
private List<T> data;
}
The next step is to define the FakerApi client which follows the Faker endpoints specification. The @RequestLine annotation defines the HTTP method and UriTemplate for request. Expressions wrapped in curly braces are resolved using their corresponding @Param annotated parameters.
public interface FakerApi {
@RequestLine("GET /users?_quantity={quantity}&_gender={gender}")
Response<Users> users(@Param("quantity") long quantity, @Param("gender") String gender);
@RequestLine("GET /addresses?_quantity={quantity}")
Response<Address> addresses(@Param("quantity") long quantity);
}
The last thing is to create a FakerApi client and that's all.
FakerApi fakerApi = Feign.builder()
.decoder(new JacksonDecoder())
.target(FakerApi.class, "https://fakerapi.it/api/v1");
Response<Address> address = fakerApi.addresses(2);
Response<Users> femaleUsers = fakerApi.users(2, "female");
In this article, we've explained how to simplify REST API Integrations in AEM with Feign HTTP client.
All code samples are available on GitHub.
For more details about Feign check official documentation.
Leverage best practices and tailored solutions to drive success. Contact us today to learn how we can help optimize your Adobe Experience Manager implementation.