How To: Call a Java EE Web Service from a .Net Client
How To: Call a Java EE Web Service from a .Net Client
Many organizations have server side investments in Java technologies. While they want to build a compelling UI with Microsoft’s latest technologies, such as WPF and Silverlight, they still want to benefit from those existing investments instead of rewriting them. In order to do so, we have to bridge between those technologies and allow client side technologies consume Java web services.
This post is a step by step guide for building a Java EE Web Service, and a .Net client application that consumes it.
Before we get started with this walkthrough, make sure you have the following installed on your machine:
Create a Java Web Service (Java EE, JAX-WS)
1. Create a new Web Application
In the NetBeans 6.1 IDE, choose File –> New Project. In the New Project Dialog select the Web category, and choose Web Application from the projects list. Then, Click Next.
* If the web category is not available in this dialog, it means that the NetBeans version you have installed isn’t the Web and Java EE package.
In the Name and Location page, set the location where you want to create the web application, and provide a name for the project. Click Next.
In the Server and Settings page, leave the default settings (Java EE 5, Use GlassFish V2) and Click Finish.
This creates the initial web application and opens the project for editing.
2. Create the Web Service
Add a new web service to the project. Right click the project node and choose New –> Web Service.
* Notice that the location of the Web Service option in the menu may change from this image and your IDE.
In the New Web Service dialog, provide the Web Service Name, and the Package. The name of the service will affect the final URL for calling the service, and the package name will be the namespace of the service contract. Click Finish.
The Web Service now appears in the project tree.
To implement the service, double click the service node in the project tree (in the figure above – CalculatorService). This will open the Web Service in Design mode, that lets you graphically design your service.
Change to Source View by clicking on the Source button in the upper toolbar, and this will open the CalculatorService.Java file for editing. Here is a sample implementation of the service. Notice how Java Annotations are similar to .Net Attributes, especially how similar they are to the Web Services attributes we know…
package org.bursteg.calculator;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public class CalculatorService
{
@WebMethod
public int Add(@WebParam(name="a") int a,
@WebParam(name="b") int b)
{
return a + b;
}
}
Deploy the web service to the web application server. From the NetBeans IDE this is done by right clicking the project node, and choosing Undeploy and Deploy.
After the web application has been deployed, just to make sure the web service works as expected, you can right click the web service node, and choose Test Web Service.
This will open the browser and navigate to a test page with the url of the service (https://localhost:9232/Calculator/CalculatorServiceService) with a ?Tester suffix.
Call the Java Web Service from a .Net Client
In Visual Studio 2008, create a new console application.
This creates a new solution with a single Console Application project in it.
Right click the project node and choose Add Service Reference.
In the Add Service Reference Dialog, paste the address of the service metadata endpoint (service address + ?wsdl suffix: https://localhost:9232/Calculator/CalculatorServiceService?wsdl), and click Go. The dialog will get the service metadata and understand the service contract.
Provide a namespace for the service reference, and click OK.
This will generate the client side proxy that lets you consume the service easily, and the required configuration settings into the configuration file.
To call the service using the generated client side proxy, open Program.cs and use the following code:
static void Main(string[] args)
{
CalculatorServiceClient proxy = new CalculatorServiceClient();
int result = proxy.Add(2, 3);
Console.WriteLine("Calculator Service returned: " + result.ToString());
}
Run the program and see that the web service is being called and the result is correct.
Conclusion
Since Java EE Web Services (JAX-WS) are standard SOAP services, they are easily interoperable from a .Net client application with only several clicks. Visual Studio generated a .Net client proxy that makes it very easy to connect and call the service.
Enjoy!
Comments
- Anonymous
July 18, 2008
PingBack from http://blog.a-foton.ru/2008/07/how-to-call-a-java-ee-web-service-from-a-net-client/ - Anonymous
August 12, 2008
The comment has been removed - Anonymous
June 17, 2009
Hi Guy,I just read and test your explanations. It was just perfect for what I need : a clear and simple introduction to web services interoperability !!However, I would like to ask you for a precision on the code snippet you provided :In the .NET/C# code, when you declare a proxy as a "CalculatorServiceClient" object, where does this type come from ? Is it some kind of a standard in which a type is automatically created by concatenating the name of the java web service and the word "Client" ???Thanks in advance,regards. - Anonymous
August 17, 2010
Hi Guy,I'm struggling a little as I have to access an external corporate WS built in java... I thought it would be relatively simple as you describe above, however although I am able to bind to the webservice the references created bear little resemblance to what I would normally expect. My Proxy class (CalculatorServiceClient in your sample) does not have a blank constructor which is a minor annoyance, added to this are the fact that the methods that the webservice exposes are not defined on this class at all. I have a couple of interfaces, one called wsCallBack and that does contain the methods but it's just and Interface!!! Without having a method to call on the main proxy I can't see how it is supposed to work! Any ideas? - Anonymous
October 08, 2010
Guy,Great introduction to WS Interoperability, simple and easy to follow. Thanks for taking the time to share your knowledge.bn - Anonymous
October 31, 2010
How do we implement the same in case one uses https instead of http for the web services. - Anonymous
December 21, 2010
CalculatorServiceClient ???where did u get that fromit should the name from wsdl(URL)CalculatorWSSERVICE