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 = "https://v6.exchangerate-api.com/v6/<your-key>/latest/USD";
        return webClient.get()
                .uri(url)
                .retrieve()
                .bodyToMono(ExchangeRateResponse.class)
                .block();

    }

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;

    @JsonProperty("terms_of_use")
    private String termsOfUse;

    @JsonProperty("time_last_update_unix")
    private String timeLastUpdateUnix;

    @JsonProperty("time_last_update_utc")
    private String timeLastUpdateUtc;

    @JsonProperty("time_next_update_unix")
    private String timeNextUpdateUnix;

    @JsonProperty("time_next_update_utc")
    private String timeNextUpdateUtc;

    @JsonProperty("base_code")
    private String baseCode;

    @JsonProperty("conversion_rates")
    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.

Conclusion

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 🙂

https://www.buymeacoffee.com/bhumansoni

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

Products – My Day To-Do (mydaytodo.com)

Have a read of some of my other posts on AWS

Deploy NodeJS, Typescript app on AWS Elastic beanstalk – (mydaytodo.com)

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

Some of my other posts on Javascript …

What is Javascript event loop? – My Day To-Do (mydaytodo.com)

How to build a game using Vanilla Javascript – My Day To-Do (mydaytodo.com)

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

Java Spring Boot & Vanilla Javascript solution – My Day To-Do (mydaytodo.com)

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

Categories: Java

3 Comments

Upload to AWS S3 bucket from Java Spring Boot app - My Day To-Do · March 13, 2024 at 6:06 am

[…] How to call REST API with WebClient – My Day To-Do (mydaytodo.com) […]

File share app - social file share feature - My Day To-Do · March 21, 2024 at 4:01 am

[…] How to call REST API with WebClient – My Day To-Do (mydaytodo.com) […]

How to add basic authentication to Spring Boot API - My Day To-Do · April 4, 2024 at 5:22 am

[…] How to call REST API with WebClient – My Day To-Do (mydaytodo.com) […]

Leave a Reply

Avatar placeholder
Verified by MonsterInsights