If you are lucky enough to work in a microservices-based environment, you will come across the need to communicate with internal or external services using RESTful APIs. This article will introduce you to OpenFeign and how to use it efficiently.
This article is for beginner to intermediate developers who have a basic understanding of Java and Spring Boot.
We will create an app that queries Shazam for our favorite artists (in this article, Coldplay) through RapidAPI and display their top songs.
Note: If you already have the APIs that you need to call, you can skip to Step 3.
Select the following configurations:
build.gradle
dependencies should look like:dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
gradle bootRun
or
./gradlew bootRun
This step is optional. If you already have an account, or if your external API is ready, you can skip this step.
'X-RapidAPI-Host'
'X-RapidAPI-Key'
In order to be able to use OpenFeign in our project, we will need to use the @EnableFeignClients
annotation.
Your Main class should look like this:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@SpringBootApplication
public class OpenfeigndemoApplication {
public static void main(String[] args) {
SpringApplication.run(OpenfeigndemoApplication.class, args);
}
}
Great! Now the project is set up and we can start creating the Feign Clients to call the external API.
src/main/java/com/omaryaya/openfeigndemo
called feign_clients
.Create a interface called ShazamClient
.
{
"method": "GET",
"url": "https://shazam.p.rapidapi.com/search",
"params": {
"term": "Coldplay",
"offset": "0",
"limit": "10"
},
"headers": {
"X-RapidAPI-Host": "shazam-api.p.rapidapi.com",
"X-RapidAPI-Key": "YOUR_API_KEY"
}
}
So here’s the corresponding FeignClient configuration:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@FeignClient(name = "shazam-client", url = "https://shazam.p.rapidapi.com")
public interface ShazamClient {
String API_HOST_HEADER_NAME = "X-RapidAPI-Host";
String API_KEY_HEADER_NAME = "X-RapidAPI-Key";
@GetMapping("/search")
Map<String, Object> search(@RequestHeader(name = API_HOST_HEADER_NAME) String apiHostHeader,
@RequestHeader(name = API_KEY_HEADER_NAME) String apiKeyHeader,
@RequestParam("params") Map<String, String> params);
So far, we configured our application to be able to call Shazam API through RapidAPI to retrieve top songs of our favorite artist. However, we have not invoked the API yet. In this step, we’ll create an API endpoint that takes the artist name as query parameter and returns their top songs.
src/main/java/com/omaryaya/openfeigndemo/services
directory.MusicService.java
file under services
and add the following code:import com.omaryaya.openfeigndemo.feign_clients.ShazamClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class MusicService {
private static final String API_HOST_HEADER_VALUE = "shazam.p.rapidapi.com"; // <-- Or replace with your host name
private static final String API_KEY = "YOUR_API_KEY"; // <-- replace with your API key
@Autowired
private ShazamClient shazamClient;
public Map<String, Object> retrieveTopSongs(String artist) {
Map<String, String> params = new HashMap<>();
// fill default values
params.put("offset", "0");
params.put("limit", "10");
// add artist
params.put("term", artist);
// Call Shazam API
return shazamClient.search(API_HOST_HEADER_VALUE, API_KEY, params);
}
}
src/main/java/com/omaryaya/openfeigndemo/controllers
directory.MusicController.java
file under src/main/java/com/omaryaya/openfeigndemo/controllers
and add the following code:import com.omaryaya.openfeigndemo.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/api/v1/music")
public class MusicController {
@Autowired
private MusicService musicService;
@GetMapping("")
public Map<String, Object> getMusic(@RequestParam String artist) {
return musicService.retrieveTopSongs(artist);
}
}
gradle bootRun
or
./gradlew bootRun
To call our API & supply our artist name, we will use Postman. Here is how typical request & response should look like: Postman Request & Response Example
Bonus: You can try to use different terms to retrieve different results, or play around with API parameters to get more/less hits.
Please feel free to tweet me @OmarYayaa if you have any questions.