Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the foxiz-core domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/posttrau/public_html/mdtWordpress/wp-includes/functions.php on line 6114
How to call REST API with WebClient - My Day To-Do

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

7 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) […]

How to fix 403 error in Github actions CI/CD - My Day To-Do · June 24, 2024 at 11:48 am

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

How to catch ExpiredJwtException in Spring OncePerRequestFilter - My Day To-Do · July 25, 2024 at 1:30 pm

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

How to build a blog engine with React & Spring Boot - Part 1 - My Day To-Do · September 21, 2024 at 12:35 am

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

How to build a blog engine with React & Spring Boot – Part 3 CI/CD pipeline · October 5, 2024 at 12:53 pm

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

Leave a Reply

Avatar placeholder
Verified by MonsterInsights