Spring web services a product of spring community, helps in developing web services using contract first web services and helps in manipulating the xml in many ways. In this tutorial I am about to show a step by step guide in developing spring web services using JAXB as mashalling technology.
You can download spring, spring web services here.
We are about to develop a web service which search the blogs based on a search criteria on title string and published date.
So lets get started,
1.Create a web application
This is a general spring web MVC application. for a skeleton application, you can just run this maven archetype to quickly start with,
mvn archetype:create -DarchetypeGroupId=org.springframework.ws \
Or you can just add the spring nature to your existing web application by configuring the web.xml shown in the next step. Here is the file structure I am using for this simple demo.
Spring strongly supports only contract first web services for several reasons like Fragility, performance, re usability and versioning.
You can find more information visit this link
So lets start first by developing our XSD....
This is a plain regular xsd where we will define all our xml elements going to create our wsdl file.
Lets define our blog object, the blog is just a complex type object with three properties the "Title" for the blog, the "AbstractDetail" of the blog and the date when the blog is published "PublishedDate". This can be defined in the xml complex type
Next step is defining the search criteria object, I am wrapping the search criteria into a seperate object so that in future, we can extend the search criteria by adding more elements like author, subject. Here the xsd definition of the search criteria
object with two fields title and published date.
The next stepa are creating the actual request object for our web service, which is basically a wrapper on the search criteria and our response object which is just a array/list of blog objects (which can be either zero size, if search results nothing
or can be of size n if search returns n objects).
Here is our completed xsd,
The next in developing our web services is defining our contract, the WSDL file.
Here is the complete version the WSDL file.
Essentilly we are improting the xsd file which we had created in the earlier step to use them in the wsdl
Then create the message parts, the port and the binding operation for the blog search service. This is self explanatory.
next step is creating the JAXB objects for your xsd elements using xjc compiler. you can either craft the JAXB objects by hand or just
use the xjc compiler. I prefer the later one. see https://jaxb.dev.java.net/guide/ for more details. (My Intellij Idea supports generating JAXB objects directly from xsd).
(Posting all the code is not practical, So I am moving here ahead posting the important parts)
Here is the endpoint
now the last and more important step, putting all the things together to deploy the service,configuring the spring-ws-servlet.xml
First and the most important point here is I am autowiring the endpoint by using the annotation @Endpoint.
So in the context.xml, we are defining the wsdl to be exposed as a bean using the class org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition.
I am specifying the JAXBMarshaller and injecting all the jaxb classes.
I am also configuring a couple of beans for validating and logging each and every request and response.
you can access the wsdl at http://localhost:8080/spring-ws-test/wsdl/BlogSearch.wsdl