JAX-WS & JAXB rock and roll…

During his Wednesday keynote at the JavaOne conference last week, Oracle Senior Vice President Thomas Kurian unveiled the next-generation architecture for Oracle Fusion Middleware. The 11g Technology Preview of JDeveloper and OC4J are currently available. Also available are some nifty tutorials and demos like Web Services Development in Oracle JDeveloper which shows off some great JEE 5 features like JAX-WS and JAXB 2.0. Although the latter is not really focused on much in the tutorial, it is the basis of what gets your java objects to and from XML.

What these two standards bring to the table from a web service development point of view is a more lightweight POJO oriented way of developing services. They will not make for better SOA in the enterprise though. In fact, they may even make things worse since a poorly designed interface can easily get a false SOA badge of credibility amongs the uninitiated by being exposed as a web service. Enough with the rant already, on with the example…

I thought it would be interesting to expand upon the bottom up HelloService tutorial by making things a bit more complicated with some old world technology: Dice. The Dice class in this example has a roll(List) operation that rolls the dice (plural of die) passed to it and returns the result. So there are really only two classes in this example:

  • Die represents (you guessed it) a die. It has two properties, number of sides and value.
  • Dice provides the collective operation of rolling dice.

This is the Die source:

package soastation.jaxwsdice;

public class Die {
protected int numberOfSides;
protected int value;
public Die() {
this(6); //Default number of sides is Six;
}
public Die(int sides) {
if (sides < 2)
throw new throw new IllegalArgumentException();
this.numberOfSides = sides;
}
public int getNumberOfSides() {
return numberOfSides;
}

public int getValue() {
return value;
}

public void roll() {
int rollValue = (int) (this.getNumberOfSides() * Math.random()) + 1;
if (rollValue > this.getNumberOfSides())
rollValue -= 1;
this.value = rollValue;
}
}

Note that the number of sides property is immutable and the value defaults to zero until the die is first rolled. It would seem natural to extend something like Integer to hold the ‘value’. This can’t be done in Java as these classes (Number, Integer, BigInteger, etc) are immutable so changing the value on every roll is not a runner. Besides, Integer is final so can’t be extended anyway.

You’ll probably also note the lack of comments on the code too. Tell me if you are for or against comments in code!

Here is the Dice source:

package soastation.jaxwsdice;

import java.util.Iterator;
import java.util.List;

public class Dice {

public List roll(List dice) {
Iterator diceIterator = dice.iterator();
while (diceIterator.hasNext()) {
Die die = diceIterator.next();
die.roll();
}
return dice;
}
}

The rest of the steps are identical to the HelloService example (which you can also get to from the Build Web Service link on the Start page of the JDeveloper 11 Technology Preview ).

  1. Add an @WebService annotation to the Dice class
  2. In the code fix menu select Configure project for web services
  3. Choose the Java EE 1.5 option and press OK
  4. Add an @WebMethod annotation to the roll method
  5. Right click on Dice service (note that the icon has changed) and select Test Web Service
  6. Gaze in awe at the handy HTTP Analyzer with Web Service support. That’s all you can do at this stage because JAX-WS doesn’t know what to do with the Die class.

This is where JAXB really comes into play. We want the properties of the Die object to be properly represented in the web service. Lets make the number of sides an attribute and the value of the Die appear as the value of the element. Do this by:

  1. Add @XmlAttribute(name=”sides”) to the numberOfSides field.
  2. Add @XmlValue to the value field.

Now you can test the web service again by right clicking on the Dice service in the Applications Navigator and selecting Test Web Service. In the HTTP Analyzer window you can enter the details of each die you want to roll. By clicking on the plus sign you can add more die to the SOAP request. The value attribute is in the schema but has no relavance to the operation (unless you want to implement a LoadedDie object?). Actually, you could leave the number of sides blank too as it will default to 6.

This example takes advantage of convention over configuration features of JEE 5. The property inspector in JDeveloper 11g Technology Preview provides some great tooling around the annotations. It is worth playing with it to see the extent of configuration possible.

We can improve the WSDL and XML representations with annotations. In the Dice class add a @WebResult(name=”die”) annotation to the roll method so that the result is not called ‘result’ by default. Also, in the roll method arguments add @WebParam(name=”die”) annotation so that the die element does not get call ‘arg0’ by default.

These JAX-WS and JAXB annotations make the web service and XML representation of the java operation and classes much more suitable. Of course these simple examples only scratch the surface of what can be achieved. It is a good idea to get familiar with capabilities of both technologies.

Let me know how you get on.

Troubleshooting Siebel Web Services: No Active Web Service

Siebel 7.8 has the concept of inbound and outbound web services which provide a great deal of integration points with the Siebel application. It doesn’t mean that integration is straight forward though. Knowledge of the Siebel data model is a must. One thing that always catches me out when I set up a web service in the Siebel environment is this runtime error which is the fault returned when an operation on the generated WSDL is invoked:

There is no active Web Service with
operation named 'xxx'.
( SBL-EAI-04313 )
( IDS_EAI_WS_OP_NOT_FOUND )

Getting a WS-I compliant web services working is a simple process in the ‘Administration – Web Services’ view:

  1. Select web service and change status to Active
  2. Select operation and change binding to ‘SOAP_DOC_LITERAL’
  3. Click on Generate WSDL button and save the WSDL file
  4. Click on Clear Cache button.

It’s the ‘Clear Cache’ button step I always forget. Perhaps now that I’ve written about it I won’t forget the next time and I hope you don’t either. However, if you ever come across SBL-EAI-04313 when you try to use a web service for the first time, click on the Clear Cache button before checking anything else.

Welcome to SOA Station

Time is a train
Makes the future the past
Leaves you standing in the station
Your face pressed up against the glass.
U2

The more things change, the more they stay the same and we keep on trying to manage the gap between capabilities (what we can do) and ambitions (what we want to achieve). It’s not such a bad thing. Our ambitions grow with our capabilities so the destination is always out there.

This blog is a placeholder for musings and rants about software capabilities and ambitions. A few hints and tips will be thrown in for good measure. SOA is the current name for the train, but we have always been oriented towards service. Just as there has always been a gap between journey and destination.

Welcome to SOA Station. Please mind the gap!

Privacy Policy

Privacy Policy for soastation.blogspot.com

The privacy of our visitors to soastation.blogspot.com is important to us.

At soastation.blogspot.com, we recognize that privacy of your personal information is important. Here is information on what types of personal information we receive and collect when you use and visit soastation.blogspot.com, and how we safeguard your information. We never sell your personal information to third parties.

Cookies and Web Beacons
We do use thirdy party services for site usage analysis and they use technology such as cookies and web beacons to gather information. Such information includes your IP (internet protocol) address, your ISP (internet service provider, such as AOL or Shaw Cable), the browser you used to visit our site (such as Internet Explorer or Firefox), the time you visited our site and which pages you visited throughout our site.

We also use third party advertisements on soastation.blogspot.com to support our site. Some of these advertisers may use technology such as cookies and web beacons when they advertise on our site, which will also send these advertisers (such as Google through the Google AdSense program) information including your IP address, your ISP , the browser you used to visit our site, and in some cases, whether you have Flash installed. This is generally used for geotargeting purposes (showing New York real estate ads to someone in New York, for example) or showing certain ads based on specific sites visited (such as showing cooking ads to someone who frequents cooking sites).

You can chose to disable or selectively turn off our cookies or third-party cookies in your browser settings, or by managing preferences in programs such as Norton Internet Security. However, this can affect how you are able to interact with our site as well as other websites. This could include the inability to login to services or programs, such as logging into forums or accounts. To learn more about cookies and web beacons please visit http://www.allaboutcookies.org/.

Privacy Policy based on the sample Privacy Policy available from JenSense