Javatpoint Logo
Javatpoint Logo

Invoking currency-exchange-service from currency-conversion-service

We have the currency-exchange-service ready, and we have set up a currency-calculation-service (currency-conversion-service). Now we will invoke the currency exchange service from the currency calculation service.

We use RestTemplate() constructor to invoke an external service. Let's create a RestTemplate and try to invoke currency-exchange-service.

Step 1: Select the currency-conversion-service project.

Step 2: Open the and create a new RestTemplate that invokes the currency-exchange-service application.

Step 3: Invoke the getForEntity() method of RestTemplate class.

getForEntity(): It is a method of RestTemplate class that retrieves an entity by using the HTTPGET method for the specified URL. It converts and stores the response in the ResponseEntity. It returns the ResponseEntity.

Parameters: It accepts two parameters:

  • URL: The URL.
  • responseType: The type of the return value.

Step 4: In the URL parameter, put the URL of currency-converter-service that is http://localhost:8000/currency-exchange/from/{from}/to/{to}. It takes values from the variable {from} and {to} from the request. Whatever comes in the request we sent it to the currency-exchange-service.

Step 5: In the above URL, we need to pass two values "from" and "to." For passing the values, create a Map for URI variables. Pass the uriVariables in the URI as a parameter.

Step 6: The response type that we are expecting back is CurrencyConversionBean, so store the response in the CurrencyConversionBean.

Step 7: Run the both services independently. When we run the currency conversion, it returns the response shown below:

Setting up Currency Conversion Microservice

The conversion Multiple is multiplied by the quantity and returns the totalCalculatedAmount 65000.00. It means $1000 is equal to 65000.00 INR. It also shows the port 8000 that denotes the other service (currency-exchange-service) is running on port 8000.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA