Today, webservices are used primarily to expose some services of an application to the outside world. There are a lot of tools available to consume webservices. Tools like Mule, Axis and Spring.
This article describes a method of exposing Java services as webservices. The technique’s / tools used to accomplice this are; Java, Spring and Castor.
There are many other tools available, like Axis, to expose java services as webservices. The reason I used Spring and Castor is the simplicity in creating webservices. Spring takes care of all boilerplate coding and Castor’s mapping capability takes care of the Object <-> XML mapping without the need to generate any java objects the way Axis does. This way you have clean java code, supported with configuration in Spring and a clean Castor mapping to expose your service as a webservice.
Knowledge
I presume that you posses the following knowledge
- Java web component developer knowledge level
- Know how to use Eclipse and the svn and/or Maven plugin
- Basic understanding of Spring
- Basic understanding of Maven (but you do not need knowledge besides being able to install the tooling)
- Basic understanding of webservices
- Basic understanding of Tomcat
Requirements
To be able to run the provided example code you need the following applications and tools.
- Maven 2.X
- Eclipse Mylin or SpringSource Toolsuite
- Spring 3.0.0 M3 or higher
- Castor 1.2
- Tomcat 6
For a complete overview of the required libraries, you can view the pom.xml in the example code. There is NO need o download the tooling yourself, this will be done by maven.
The example
This example exposes a webservice that returns a Artist object from a (HSQL) database, given an id. The example will not explain how to create a CRUD application using DAO’s and services etc. This should be knowledge you already have. I have provided an example project, that contains the necessary CRUD code for this webservice project to use.
Project configuration
The next thing you need to do is to download the example projects from google code. These are the Top2000Persistence and Top2000Webservices project.
First you need to checkout the persistence project. The webservice project depends on it. The persistence project is a smaple project using Spring, JPA and Hibernate.
The way I do this is the following:
- Startup your Eclipse
- File -> new -> Other -> Maven -> Checkout maven projects……
- select svn from the dropdown
- Paste the url http://top2000persistence.googlecode.com/svn/trunk/
- click on Finish
The project wil be downloaded into your Eclipse and configured as a maven/java project. Below an image showing the steps I have described here:

New Maven Project Dialog
Now you can build the project either with Eclipse, Maven plugin in Eclipse or cmdline Maven command. For example, if you right-click the project -> run as -> maven test, this should be part of your output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building Top2000Persistence [INFO] [INFO] Id: Top2000:Top2000Persistence:jar:1.0-SNAPSHOT [INFO] task-segment: [test] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:compile] [INFO] Nothing to compile - all classes are up to date [INFO] [resources:testResources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:testCompile] [INFO] Nothing to compile - all classes are up to date [INFO] [surefire:test] ..... ..... ..... Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 1 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 9 seconds [INFO] Finished at: Mon Jun 29 11:28:38 CEST 2009 [INFO] Final Memory: 2M/9M [INFO] ------------------------------------------------------------------------ |
If you experience other results that the above, you might need to manaully download libraries and install then into your local maven repository. In the section resources you will find the resource for the maven documentation and the maven install plugin documentation.
The same you must do for the Top2000Webservices project.
The way I do this is the following:
- File -> new -> Other -> Maven -> Checkout maven projects……
- select svn from the dropdown
- Paste the url http://top2000webservice.googlecode.com/svn/trunk/
- click on Finish
The project wil be downloaded into your Eclipse and configured as a maven/web project/java. The dependency Top2000Persistence will be included by the maven process. You should have 2 project which look something like this:

New Maven Web project
You can now do right-click project -> run as -> maven test, the output should be as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building Top2000Webservices [INFO] [INFO] Id: Top2000:Top2000Webservices:war:1.0 [INFO] task-segment: [test] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. ...... ...... ...... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6 seconds [INFO] Finished at: Mon Jun 29 12:18:37 CEST 2009 [INFO] Final Memory: 3M/12M [INFO] ------------------------------------------------------------------------ |
Now we have 2 projects in our workspace, where the web project has a dependency on the persistence project. Maven test confirms that the projects compile and run the available test without problems.
The next thing we need to do is configure the HSQL database location for our webservices project. The persistence project uses the HSQL database in memory type for testing purposes. For usage in other purposes you need to create a file called local.config and put a set of properties into it. Below a sample of the required properties:
1 2 3 4 5 6 7 | ds_driverclassname org.hsqldb.jdbcDriver ds_url <path-to-your-db-location>/test <-- You need to provide this ds_username sa ds_password vendor_dbplatform org.hibernate.dialect.HSQLDialect showsql false generateddl false |
Make sure the local.config file is in your webapp folder. (I have provided a sample in there when you download the project. Just replace my url with your own).
Next thing we need to do is add a server config to our eclipse workspace. I am not going to explain in detail how to do this, since I expect you already know how. But in short terms:
- Download and install tomcat 6.x
- In server view right-click –> new server –> select tomcat 6 –> point it to the home dit of tomcat
- Add the top2000webservice project
- Finish
Now you can start the server and the Top2000webservice application should start without any problems.
Resources
Here are usefull resources that can help you in running and working with the example project.
- Maven documentation
- Maven install plugin documentation
- Spring documentation
- Tomcat documentation
- Hibernate documentation
- Top2000Persistence demo project
- Top2000Webservices demo project
Tags: Java, soap, Spring-WS, Webservices