Sunday, January 2, 2011

JavaEE 6 + Metro WebServices

Following the post about WebServices with Grails here's a short summary of my attempt to make Java's EE 6 WebServices work with .NET. It's quite a basic example but as history shows even the simplest examples might not work (as it is the case with Axis2).


So here's a definition of a Hello WebService:
package com.aplaline.integration.webservices;

import javax.jws.*;

@WebService(name="Hello", serviceName="HelloService")
public class HelloService {
public String sayHello(@WebParam(name="name") String name) {
return "Hello " + name + "!";

As you can see there's no magic here. Let's see how NetBeans' configuration option look like for this guy:

Surprisingly enough there's mentioning of a .NET version! Let's see how this works. I'll be using VWD 2008 SP1 to test it:

Let's add a web reference to this web site:

Now let's create a blank Default.aspx page and add some code to call that web service:
<form id="form1" runat="server">
<td><asp:Label ID="Label1" runat="server" Text="Enter name" /></td>
<td><asp:TextBox ID="input" runat="server" /></td>
<td align="right" colspan="2"><asp:Button ID="Button1" runat="server"
onclick="Button1_Click" Text="Click me" /></td>
<td colspan="2"><asp:Label ID="output" runat="server" /></td>
protected void Button1_Click(object sender, EventArgs e)
output.Text = new localhost.HelloService().sayHello(input.Text);


As it turns out it works out of the box properly.

For the kicks of it let's try and consume the same web service from PHP 5.3:
$client = new SoapClient("http://localhost:8080/WebServices/HelloService?wsdl");
var_dump($client->sayHello(array("name" => "John")));
There's a little inconvenience when calling web services from PHP in that the parameters need to be passed as an array instead of simply specifying their value. In the long run it's kind of healthy because different webservice stacks tend to handle things differently and putting the proper context into the whole thing.

This is the first pleasant surprise since I've started to work on the web-service interoperability between .NET and Java. It looks like things are definitely going in the right direction :)

Here's the Java project for reference.
Here's the .NET website for reference.

Have fun!

No comments: