Monday, November 15, 2010

Creating Web Services for Portlet Factory

The heart of any SOA strategy involves web services and the document that makes it happen is a WSDL. Websphere Portlet Factory is an excellent tool for putting together a SOA presentation layer since it easily accepts a WSDL and quickly exposes its operations in a developer friendly IDE (Eclipse/RAD).

In previous WPF projects I'd start by building out schema that represent the data needed for display in the tool, but when working with web services the schema is defined in the WSDL document. Since I dislike working with schema, I typically create a sample XML and then use a tool to generate a schema from it. But what about the schema that are in the WSDL? What if I'm also developing the web services that will be ultimately consumed by WPF? I sure don't want to create a schema when something else can do the dirty work.

Enter web services metadata annotations (JSR 181).

Using annotations and a compatible runtime container like Apache's Axis2, I can throw together a web service in no time by just adding some annotations to a regular POJO. Suppose I have a need to retrieve contact information, but no implementation is available yet and I just need something stubbed out so I can get going. Someone else can follow up later and code in an implementation.

Let's start with a bean that will hold our contact information:

 package com.dsixe.blog;  
 public class ContactBean {  
      private String name;  
      private String phone;  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public String getPhone() {  
           return phone;  
      }  
      public void setPhone(String phone) {  
           this.phone = phone;  
      }  
 }  

The only code needed here is the declaration of the name and phone, the rest (class definition, getters/setters) is generated by Eclipse. Now let's create a POJO that will ultimately contain the implementation of the operation, but for right now it will just return a static instance of ContactBean:

 package com.dsixe.blog;  
 public class ContactInfo {  
      public ContactBean getContact(String uid){  
           ContactBean cb = new ContactBean();  
           cb.setName("Carl");  
           cb.setPhone("678 555-1212");  
           return cb;  
      }  
 }  

Nothing magical about this class, but it can be turned into a web service operation by adding 3 annotations:

 package com.dsixe.blog;  
 import javax.jws.WebMethod;  
 import javax.jws.WebService;  
 import javax.jws.soap.SOAPBinding;  

 @WebService(serviceName="ContactService", targetNamespace="http://dsixe.com/blog")  
 @SOAPBinding(style=SOAPBinding.Style.DOCUMENT)  

 public class ContactInfo {  
      @WebMethod  
      public ContactBean getContact(String uid){  
           ContactBean cb = new ContactBean();  
           cb.setName("Carl");  
           cb.setPhone("678 555-1212");  
           return cb;  
      }  
 }  

Believe it or not, that's it! I package it into a JAR (not a WAR) and deploy to Axis2 and I'm ready to pull a WSDL into a WPF web service builder.

In the time it probably would have taken me to build an XSD schema using some fancy editor, I've not only created a WSDL with the required schema but also a functional base implementation of a service operation.
Please note that generic comments containing links with the intent of self promotion will be flagged as spam and deleted.

0 comments:

Post a Comment

Are you about to post a generic comment that has nothing to do with this post? Something like "Hey thanks for this very valuable information, BTW here's my website". If so, it will be marked as spam and deleted within 24 hours.

Note: Only a member of this blog may post a comment.