Home | Blog

AEM API Integration with Feign HTTP client

Cover Image for AEM API Integration with Feign HTTP client
Matija Kovacek
Matija Kovacek

Introduction

In projects we often have some integrations with API layer of 3rd party systems. This means that on our side we need to prepare HTTP client to be able consume those endpoints. Based on API specification we need to prepare Resource representation classes. Then, HTTP client with some configurations, like headers, timeouts, error handling. Parse request and response payloads into objects and many other things. All off this on the end creates a lot of boilerplate code.

Inspired with my latest project I want to introduce Feign HTTP client. Feign is HTTP client which goal is to simplify writing HTTP clients. Goal is to reduce complexity of REST API integrations.

With Feign we need to declare and annotate an interface based on API specification. And in background Feign will process annotations into a templated requests. Feign offers you to configure different HTTP clients, JSON/XML processors, metric providers, loggers and some other features.


Example

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.


Setup

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, additionaly 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 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>

Feign HTTP client

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;

}

Next step is to define FakerApi client which follows Faker endpoints specification. The @RequestLine annotation defines 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);

}

Last thing is to create 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");

Conclusion

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.

Read more

Cover Image for Optimizing slow Unit Tests

Optimizing slow Unit Tests

Understanding the motivation behind optimizing slow unit tests is crucial. We'll explore the challenges faced by Client XYZ, why we wanted to fix them, and the good things that happened afterward. Expect insights into how faster tests can boost productivity and project success.

Matija Kovacek
Matija Kovacek
Cover Image for Importance of Code reviews

Importance of Code reviews

Inspired by some of the last few projects, I have noticed that still a lot of people don't consider Code Review seriously. So what is code review? A code review is a process where someone other than the author(s) of a piece of code examines that code. Code review should be used to maintain the quality of our code and products.

Matija Kovacek
Matija Kovacek
Cover Image for AEM API Integration with Feign HTTP client

AEM API Integration with Feign HTTP client

How to call RESTful Web Service in AEM? Luckily there is Feign HTTP client which simplifies REST API Integrations. Check out how to integrate it in AEM project.

Matija Kovacek
Matija Kovacek
Cover Image for Speed up the Maven Build Time

Speed up the Maven Build Time

How to speed up Maven build time? 2x time faster Maven build time with Maven Daemon.

Matija Kovacek
Matija Kovacek
Cover Image for Test behaviour, not implementation

Test behaviour, not implementation

Test behaviour, not implementation if you want to build right product. For your own good it will save you time and money.

Matija Kovacek
Matija Kovacek
Cover Image for Really? Preselected checkbox not working, common AEM?

Really? Preselected checkbox not working, common AEM?

Why one simple preselected checkbox doesn't work in page properties?

Matija Kovacek
Matija Kovacek
Cover Image for Updating AEM content with Sling Pipes

Updating AEM content with Sling Pipes

You are still updating content manually? Try out Sling pipes. Sling pipes is simple tool for executing CRUD operations over resources in AEM repository. But is it powerfull enough to replace your groovy scripts?

Matija Kovacek
Matija Kovacek