Contract-First webservice with Spring

I’m using spring mostly for IOC (Inversion of control). Being able to let all the hideois plumbing and wiring do by another one (spring) is very comfortable.

But Spring has quite a list of subprojects and as I’m interested in web services, the Spring Web Services were looking interesting for me.
I have created Web Services before. I used Axis, which has a nice feature. You can create Java classes and build wsdl files out of them. So you don’t have to know anything about the XML
form of your Web Service.

Spring-WS is different in this aspect.

Update: The ServerSide has an article which summarizes the sprin web services project too.

Spring-WS is using a Contract-First approach. This means you have to define your XML files for the webservices first and THEN create the piece of java which uses this.
Only after reading the explanation why they are using this approach I recognized the sense after this.

Of course creating a java class and have a wsdl file generated on a mouse click is the easiest way to do. But there a disadvantages too.

  • Changing this java class leads to another wsdl file. Which also means that clients have to get the new wsdl file and take it into account.
  • In an XML schema file you can say that a variable can only have the form of a 5 letter word. In java you only have strings. So there is a missmatch of possibilities.
  • Focussing on XML first and after THIS focussing on java allows you to extend your system more safely. You KNOW changing your java file will not change your webservice in an unpredicted way.

Enough of this theoretical talk. On the Spring-WS site you can find a good tutorial on hotwo develop your XML and how do make a webservice out of it.
Be warned, in this tutorial you have to read a lot of XML. Yeah, at first glance I thought: No problems. Whats difficult about xml. Take some tags. Throw some attributes
to it and voila. You got XML.

True. XML is not difficult. But take this XML quiz and you will see, XML is easy. But you have to learn some basic rules. I bet you have an intuition about XML, but not a founded knowledge.

After refreshing your XML knowledge, why not take the hard road of getting you a shot of XML schema definition.
First: It’s neccesary to understand the tutorial.
Second: It’s interesting and quite handy for projects where XML plays an important role.

After learning this two technologies you can make this tutorial… Yeah… some maven2 experience may be good. But hey.. Who doesn’t have it this times?
Serous, if you REALLY want to learn about cool and trendy.. not to forget USEFULL new java technologies.. Learn about maven2!

A little obstacle which is not explained in the tutorial is the activation.jar. You need this jar to get your webservice to work. Now you say, hey, I have got maven2.
What could be easier to add a new depency? The problem is: This jar is from SUN. And so it has an improper license to be redistributed over the maven2 server.
But luckily thats not a great problem. Running
mvn compile
Will you get this error:
t HEY, maven2 gives you the right clue what to do.
Grab the zip file from sun. Unzip and search the activation.jar (it’s in the lib directory). And then do as maven2 tells you:

mvn install:install-file -DgroupId=javax.activation -DartifactId=activation -Dversion=1.0.2 -Dpackaging=jar -Dfile=/path/to/file/activation.jar

And automagically, the activation.jar is in your local maven2 repository.

Just follow the rest of the tutorial and check if tomcat is invoking your webservice. Doing this is easy with.. But hey.. This tip is at the end of
the tutorial.. Don’t wanna be a spoiler..

So.. have fun and learn new things…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: