Currency Conversion App in Java

Consuming a rest web service is easy and with RestTemplate Spring has made dealing with these rest services easier. In this article we will build a simple Currency Conversion App that uses the API exposed by Currency Layer

Softwares Used

  • Spring Boot 1.5.9.RELEASE
  • Java 8
  • Currency Conversion API by apilayer
  • Maven
  • Eclipse

For this example we will build on top of our Web Application Starter Application. The application then adds support for Rest to consume the currency conversion API. The apilayer provides very reliable and easy to integrate exchange rates api that can be used for any business of currency conversion app. The currency layer api is subscription based and they have many plans. You can choose from them as per your business and application need.  For our tutorial purpose we are using Free Plan.

We are also using caching functionality provided by Google using Guava. This is to minimize the API calls within a particular duration of time. Apart from standard Spring Boot dependencies, below are few dependencies from our pom file.

The currency layer api provides JSON reponse like below

To hold this response we have to define our model class. The class will be like

As the api calls need access key to be sent along with request we have kept those details in our application properties.

As mentioned earlier we are using simple caching provided by Google Guava. This is to minimize api calls. Once fetched exchange rates we will store them in our cache. These rates will be valid for certain duration, in our case 60 minutes. Below is our class that defines this cache.

Now we are all set and now its time to do actual API call. This is done by our business processor method as shown below

You can see how we first checking with our cache to fetch the rates and if not available then we do actual rest call and store the rate in our cache for immediate future use.

Here is how it looks when we start our currency conversion app and access it from browsercurrency conversion appLimitation

As we are using Free Plan from Currency Layer,  this has some limitations like I can only make 1000 requests per month. The source currency switching is not allowed i.e. I can only use USD as source for conversion. If we try to do, we get error response from api. Like below 

But this limitation is  only of plan I chose. With other plans like Pro or Enterprise there are many things which you can achieve easily.

Summary

In this tutorial we have seen how easy it is to implement a currency conversion app. All thanks to Apilayer and Currencylayer. You can download the complete code from our repository. Please feel free to comment your queries or questions.

Download from Git

Add a Comment

Your email address will not be published. Required fields are marked *