Skip to content
VirtualLP Virtual Learning Platform

Practical 1: Cross-Platform Currency Conversion Web Service (Java and .NET Clients)

16 min read Topic 25 of 34 1 hands-on lab

Aim

Build a service that converts Indian Rupees (INR) to US Dollars (USD). Keep the exchange rate in a settings file. Call the service from Java and .NET programs. Check wrong input and record requests and replies.

Theory

A web service lets one program ask another program to do work over a network. The two programs can use different languages. This ability to work together is called interoperability.

Here, the client sends an amount in rupees. The service reads the exchange rate, calculates the dollar amount and sends it back.

money exchange counter

At an airport money-exchange counter, a person speaking Hindi and a person speaking English both hand over rupees and get dollars back. The counter follows one standard rate card that everyone can read. Here, the counter is the web service, the rate card is the WSDL (Web Services Description Language), and the Java and .NET programs are the two customers speaking different languages.

flowchart LR
    J["Java client<br/>(wsimport stubs)"] -- "SOAP/HTTP" --> S["CurrencyService<br/>JAX-WS on Tomcat"]
    N[".NET 8 client<br/>(WCF service reference)"] -- "SOAP/HTTP" --> S
    S --> P[("currency.properties<br/>inr.to.usd=0.012")]
    S --> L["Request / response log"]
    W["WSDL contract"] -.describes.-> S
    W -.generates.-> J & N
Concept Role in this practical
SOAP (originally Simple Object Access Protocol) The XML (Extensible Markup Language) message format sent over HTTP (Hypertext Transfer Protocol); works on any platform
WSDL The XML "menu card" describing the operations, messages, data types and the service address
JAX-WS (Java API for XML Web Services) Java's library for SOAP web services – uses @WebService, @WebMethod, @WebParam
wsimport A Java tool that reads a WSDL and generates client stub classes
WCF (Windows Communication Foundation) service reference / dotnet-svcutil The .NET tools that generate a client proxy from the same WSDL
Stub / proxy A ready-made local class: you call a normal method, it turns it into a SOAP request and turns the answer back into a return value
SOAP Fault The standard error message – Java shows it as SOAPFaultException, .NET as FaultException

Why the exchange rate is kept in a file

Exchange rates change every day. If 0.012 were written inside the code, you would have to change the code and redeploy every time. Keeping it in currency.properties (read with Java's ResourceBundle) keeps settings separate from code – an administrator edits one line, redeploys, and every client immediately gets the new rate.

Key point

The WSDL is the contract that makes cross-platform calls possible. Java and .NET never see each other's code – both generate clients from the same WSDL and exchange the same SOAP XML.

Requirements

Use a Jakarta-compatible Metro runtime with Tomcat 10.1. Java 11+ does not include JAX-WS or wsimport; install these tools separately. Keep the runtime, generated client and jakarta.* imports compatible.

  • JDK (Java Development Kit) 21, Eclipse IDE (Integrated Development Environment) for Enterprise Java, Apache Tomcat 10.1 with a JAX-WS runtime (Eclipse Metro)
  • .NET 8 SDK (Software Development Kit) with the System.ServiceModel.Http package
  • Tools: wsimport (JAX-WS RI (Reference Implementation)) and WCF Connected Services / dotnet-svcutil

Program – service (CurrencyConverter.java)

package com.vlp.ws;

import java.util.ResourceBundle;
import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;
import jakarta.jws.WebService;

@WebService(serviceName = "CurrencyService")
public class CurrencyConverter {

    @WebMethod
    public double convertCurrency(@WebParam(name = "amountInr") double amountInr) {
        if (amountInr <= 0) {
            throw new IllegalArgumentException("Amount must be greater than zero");
        }
        double rate = Double.parseDouble(ResourceBundle.getBundle("currency").getString("inr.to.usd"));
        double usd = Math.round(amountInr * rate * 100.0) / 100.0;
        System.out.println("[CurrencyService] convertCurrency(" + amountInr + ") -> " + usd + " USD (rate " + rate + ")");
        return usd;
    }

    @WebMethod
    public String getServiceStatus() {
        return "UP";
    }
}

What the code does:

  • @WebService(serviceName = "CurrencyService") – makes this class a SOAP web service named CurrencyService.
  • convertCurrency(...):
    1. Checks the input – if the amount is 0 or negative, it throws an error (which becomes a SOAP Fault).
    2. Reads the rate from currency.properties.
    3. Converts and rounds to 2 decimal places.
    4. Logs the request and result to the Tomcat console.
    5. Returns the dollar amount.
  • getServiceStatus() – a simple health check that returns "UP".

src/main/resources/currency.properties

inr.to.usd=0.012

Program – Java client (CurrencyClient.java)

CurrencyService service = new CurrencyService();          // generated by wsimport
CurrencyConverter port = service.getCurrencyConverterPort();

double inr = 5000;
double usd = port.convertCurrency(inr);
System.out.println(inr + " INR = " + usd + " USD");
try {
    port.convertCurrency(-100);
} catch (Exception e) {
    System.out.println("Service error: " + e.getMessage());
}

It creates the generated service object, gets the port (the proxy), converts 5,000 rupees, then deliberately sends -100 to show how the error comes back.

Program – .NET client (Program.cs)

using ServiceReference;

var client = new CurrencyConverterClient();               // generated service reference
double inr = 5000;
double usd = await client.convertCurrencyAsync(inr);
Console.WriteLine($"{inr} INR = {usd} USD");
try { await client.convertCurrencyAsync(-100); }
catch (Exception ex) { Console.WriteLine("Service error: " + ex.Message); }

Exactly the same steps in C#: create the generated client, convert 5,000, then try -100 and catch the error.

Procedure

Part A – the service

  1. In Eclipse, create a Dynamic Web Project named CurrencyService.
  2. Add the class CurrencyConverter.java and the file currency.properties.
  3. Right-click the project → Run As → Run on Server (choose Tomcat).
  4. Open http://localhost:8080/currency/CurrencyService?wsdl in a browser. You should see the WSDL XML – find the operation convertCurrency in it.

Part B – the Java client 5. Generate the stubs: run wsimport -keep -p generated http://localhost:8080/currency/CurrencyService?wsdl (or in Eclipse use Generate Client). 6. Write CurrencyClient.java, then Run As → Java Application.

Part C – the .NET client 7. Create a console app: dotnet new console. 8. Add the service: Add → Connected Service → WCF Web Service (or run dotnet-svcutil <wsdl URL>). 9. Write Program.cs and run dotnet run.

Part D – change the rate without changing code 10. Change inr.to.usd to 0.0118, redeploy, and run either client again – the result changes, but no code was changed. 11. Look at the request/response log in the Tomcat console, and the raw SOAP XML in Eclipse's TCP (Transmission Control Protocol)/IP (Internet Protocol) Monitor.

Virtual lab · Web Services · 45 min

Practical 1: Cross-platform currency conversion service with Java and .NET clients

Build an INR → USD SOAP service with a configurable exchange rate, validation and request logging, then call it from a Java client (wsimport stubs) and a .NET 8 client (WCF service reference) – one service, two platforms.

Open lab

Output / Observations

Example output for the settings in this lesson:

Java  : 5000.0 INR = 60.0 USD
        Service error: Amount must be greater than zero
.NET  : 5000 INR = 60 USD
        Service error: Amount must be greater than zero
Tomcat: [CurrencyService] convertCurrency(5000.0) -> 60.0 USD (rate 0.012)
After rate change → 5000 INR = 59 USD
  • 5,000 × 0.012 = 60 dollars.
  • A negative amount gives the same error message in both languages – the SOAP Fault works across platforms.
  • After changing the rate to 0.0118: 5,000 × 0.0118 = 59 dollars.

Conclusion

Check that both clients can call the same service and show the converted amount. Change the rate in the settings file and run them again. Also check the error for a negative amount and the request records in the console.

Viva questions

  1. What makes a web service platform-independent? Open standards – XML/SOAP over HTTP, described by a WSDL contract.
  2. What does wsimport generate? Service and port (SEI (Service Endpoint Interface)) classes plus JAXB (Java Architecture for XML Binding) types that turn method calls into SOAP messages.
  3. How does a .NET client call a Java service? By generating a proxy from the WSDL (WCF Connected Service / dotnet-svcutil).
  4. How does a client see an exception thrown in the service? As a SOAP Fault → SOAPFaultException (Java) / FaultException (.NET).
  5. Why keep the exchange rate in a properties file? So the setting can change without recompiling the service.
  6. What is the role of @WebParam(name = "amountInr")? It names the XML element for the parameter in the WSDL and the SOAP body.

Practise this topic

Hands-on virtual lab

Apply what you just read in a realistic, guided simulation. Free with a verified account.

Create a free account to track your progress and unlock virtual labs.