In this blogpost you will see how to call REST API with WebClient to get exchange rate. In this blogpost, you will go through a scenario of building an API for a foreign exchange app to perform currency conversions. To get the exchange rate data, the app will be invoking an external third party API to get the latest currency conversion rates. In this post,

How to call REST API with WebClient

public class ExchangeRateClient {
    private final Logger logger = Logger.getLogger(ExchangeRateClient.class.toString());
    WebClient webClient = WebClient.create();

     * Get exchangeRates for code defined
     * @param currencyCode
     * @return
    public ExchangeRateResponse exchangeRateData(String currencyCode) {
        String url = "<your-key>/latest/USD";
        return webClient.get()


Specifying the ExchangeRateResponse.class indicates that the bodyToMono method would parse the JSON response and serialize it to the ExchangeRateResponse class. You can see what the

public class ExchangeRateResponse {
    private String result;
    private String documentation;

    private String termsOfUse;

    private String timeLastUpdateUnix;

    private String timeLastUpdateUtc;

    private String timeNextUpdateUnix;

    private String timeNextUpdateUtc;

    private String baseCode;

    Map<String, BigDecimal> conversionRates = new LinkedCaseInsensitiveMap<>();

@JsonProperty is one of the annotation classes from the jackson library specifies which Java class variable to map to the attributes in the json response.


Hope you found this blogpost useful and you can find the full source code for this repo here on Github which also has instructions to setup and run this repo. You can view more info on the Weather API over here.

If you find any of my posts useful and want to support me, you can buy me a coffee 🙂

While you are here, maybe try one of my apps for the iPhone.

Products – My Day To-Do (

Have a read of some of my other posts on AWS

Deploy NodeJS, Typescript app on AWS Elastic beanstalk – (

How to deploy spring boot app to AWS & serve via https – My Day To-Do (

Some of my other posts on Javascript …

What is Javascript event loop? – My Day To-Do (

How to build a game using Vanilla Javascript – My Day To-Do (

Vanilla Javascript: Create Radio Buttons (How-To) – Bhuman Soni (

Java Spring Boot & Vanilla Javascript solution – My Day To-Do (

Vanilla Javascript: Create Radio Buttons (How-To) – Bhuman Soni (

Categories: Java


Leave a Reply

Avatar placeholder