Monday, September 7, 2009

60 seconds on SOAP-based Web Services

According to this book, there are 74 distinct initiatives trying to define what a Web Service should look like. And I bet, there are at least 74 different abbreviations around to describe different parts of it. So it's surprising how simple it is to create a Web Service with JDK 1.6. First you define an interface with a bunch of annotations:

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
// use RPC or 'wrapped' document-style
@SOAPBinding(style = Style.RPC)
//@SOAPBinding(style = Style.DOCUMENT,
// parameterStyle = SOAPBinding.ParameterStyle.WRAPPED )
public interface Example
{
 @WebMethod public String getGreeting( @WebParam(name="myname") String myname);
}
And then implement the service itself. Use a thread-pool if your application is multi-threaded.

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService(endpointInterface = "simple.Example")
public class ExampleImpl implements Example
{
 @Override
 public String getGreeting(String myname)
 {
   return "hello, " + myname;
 }

 public static void main(String[] args)
 {
   String url = "http://127.0.0.1:9876/example";
   System.out.println("Starting WebService at: " + url);
   System.out.println("WSDL available at:      " + url + "?wsdl");

   Endpoint endpoint = Endpoint.create(new ExampleImpl());
   //endpoint.setExecutor( Executors.newFixedThreadPool(10) );
   endpoint.publish(url);
 }
}
The resulting WSDL definition of this service can be looked at http://127.0.0.1:9876/example?wsdl (use Firefox or IE, not Chrome!). Any SOAP-based client (such as the XML Test Utility in General Interface, see previous post) can now invoke this service.
REST-style would be harder to implement, but easier to test. In the REST case, you can simply do: http://127.0.0.1:9876/rs?name="Pete" for invocations. The difference to the previous solution is that with REST-style, the client uses an HTTP GET to invoke a method and the return value can be any kind of XML, not just a SOAP envelope.
The code above implements the main 'standard', but its also the most bloated possibilty. More efficient, but less standardized, is a message encoding of JSON instead of XML, or better, use the Google Web Toolkit. If you are in full control of the server and the client and don't need to follow standards, GWT seems an excellent choice right now.

No comments:

Post a Comment