Tag Archives: webservice

JAX-WS: Webservices with Java EE 6

SOAP webservices were never so much fun and ease before Java EE 6! So without further ado, we’ll see how to get a JAX-WS service up and running in a matter of minutes! The source code is hosted on Google codes, which you can download and directly import into your IDE.

(I used JBoss Developer Studio 5 and JBoss Application Server 7 )

Download the Source Code

We’ll expose a PizzaService as a JAX-WS. Our PizzaService would be a very light-weight service which will have just two functions:-

  1. getPizza – This will buy/get you a Pizza object.
  2. returnPizza – This will help you return a stale/cold/substandard Pizza back!

Let’s define Pizza entity class:-

Pizza.java

package webservice.jaxws.resources;
import java.util.Date;

public class Pizza {
 private String size;
 private Date dateOfManufacture;
 private float cost;

/*Setters and Getters removed for brevity */

}

The PizzaService Interface
PizzaService.java


package webservice.jaxws.service;

import webservice.jaxws.resources.Pizza;

public interface PizzaService {
 public Pizza getPizza();
 public String returnPizza(Pizza pizza);
}

Writing the implementation of the above interface.

PizzaServiceImpl.java


package webservice.jaxws.service;

import java.util.Date;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import webservice.jaxws.resources.Pizza;

@WebService(serviceName="PizzaService", portName="PizzaPort", targetNamespace="generated.jaxws.webservice")
public class PizzaServiceImpl implements PizzaService {

 @Override
 @WebMethod
 @WebResult(name="pizza")
 public Pizza getPizza() {
 //Creating a new Pizza
 Pizza pizza = new Pizza();
 pizza.setSize("Large");
 pizza.setCost(375.45f);
 pizza.setDateOfManufacture(new Date());

 return pizza;
 }

 @Override
 @WebMethod
 @WebResult(name="PizzaReturnStatus")
 public String returnPizza(@WebParam(name="pizza") Pizza pizza) {
 System.out.println("Pizza returned by the customer :(");
 System.out.println("Details of the returned pizza:-");
 System.out.println(pizza);

 return "Pizza successfully returned.";
 }

}

To expose a class as webservice all you need to do is to annotate it with @Webservice and you’re done!

The other annotations make the generated WSDL file pretty and more-meaningful to read.

  1. @WebMethod – It declares the method as an endpoint. Although the spec says it to be mandatory, but the code still runs once you annotate the service class with @WebService.
  2. @WebResult – It is used when the method is returning something, it gives the returning object a name, else your wsdl file would contain a parameter called “return” which might not seem so much readable once you have got a significant number of methods featuring in a particular webservice.
  3. @WebParam – This is also almost same as @WebResult with the only difference that this annotation is used to make the name of input argument of the endpoint more meaningful. In the absence of @WebParam, the wsdl file would read the input argument as arg0, arg1 etc.

It’s now time to put the code to test and see the output. Run the project and to see the wsdl open up your browser and type in the following url:-

http://localhost:8080/webservice-jaxws/PizzaService?wsdl

The anatomy of the above url is simple enough, http://localhost:8080/webservice-jaxws/ is the server-address and the project location and PizzaService is the service name we defined as an attribute of @WebService annotated at the class PizzaServiceImpl (scroll up to check!)

You can test the above webservice by either writing a client or using Soap-UI which is the easier and quicker way!

UPDATE

A client consuming this webservice can be seen and downloaded from this URL.

That’s all for this one.

Spring Web Services Client

In this post we’ll develop a Spring WebService client which will consume the Webservice we created in the previous post, getSquareService. This getSquareService takes in an integer as an input and gives back its square via http using spring web services 2.0. Earlier we used a Soap UI, to test and send/recieve our request/response, but now we’ll develop our own client as in real applications.

This client will be a normal Spring MVC application. As far as content is concerned, we’ll use two things here:-

  1. Web Service Template by Spring: This web service templates, makes our client code, extremely easy and good. It’s a helper template just as in JDBCTemplate, which is to access database using jdbc.
  2. wsimport tool by JAX-WS: This tool, takes in the wsdl url, and generates the Java Proxy classes and other artifacts which are necessary to consume the webservice.

Let’s start developing our client to the webservice. You don’t have to copy paste, the source code is shared and can be downloaded easily.

Download source code.

You can see the older version from here. The current version incorporates the changes required to comply with Maven 3

Now, since you have the source code at your disposal, let’s start it from the very beginning 🙂

Developing the Web Service Client from scratch!

1. Download the webservice project created in the earlier post Spring Web Services 2. Made Easy! and import it using Existing Maven Projects in your STS/Eclipse. (I use SpringSourceToolSuite, STS in short, though!)

2. Now open up your browser and type in the following URL to see the WSDL generated and examine it.

http://localhost:8080/spring-ws2-exemplary/getSquare.wsdl

3. Download the wsimport tool and execute it by following the instructions and set the path of it’s bin folder in your classpath.

4. Now we’ll use wsimport tool to generate our Java classes, as we generated it for jaxb.

5. Open command prompt and enter the following command to generate the classes at some place.

wsimport -d F:\generated -keep http://localhost:8080/spring-ws2-exemplary/getSquare.wsdl

Make sure the target directory already exists. Below is the screeen shot of this command.

Now check at the target place for the files generated, F:\Generated in my case.

6. Create the project by executing the following command in a single line.

mvn archetype:generate -DgroupId=ankeet.ws2.client -DartifactId=spring-ws2-client -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

7. Open your STS/Eclipse and import the above as Existing Maven Projects. This import option will come only if you have maven plugin installed for your IDE. With STS it comes in built, but you need to install it explicitly for Eclipse. Else do an mvn eclipse:eclipse and then import as Existing Projects in workspace.

8. Create a new source folder and in it a package com.wordpress.ankeetmaini.spring_ws2_square and copy all the .java files created earlier by wsimport.

9. Create another package ankeet.spring.ws2.controller, where we’ll create the Controller of our Client App. Call it ClientController.java

/**</pre>
 *
 */
package ankeet.spring.ws2.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ws.client.core.WebServiceTemplate;

import com.wordpress.ankeetmaini.spring_ws2_square.ObjectFactory;
import com.wordpress.ankeetmaini.spring_ws2_square.SquareServiceRequest;
import com.wordpress.ankeetmaini.spring_ws2_square.SquareServiceResponse;

/**
 * @author Ankeet Maini
 *
 */
@Controller
@RequestMapping("/")
public class ClientController {
 @Autowired
 private WebServiceTemplate webServiceTemplate;

 /**
 * This is the default handler. When application goes live
 * the control comes to this, and it fires a JSP,
 * asking the input.
 */
 @RequestMapping(method = RequestMethod.GET)
 public String getRequest(Model model) {
 model.addAttribute("squareServiceRequest", new ObjectFactory().createSquareServiceRequest());
 //Show request.jsp
 return "request";
 }

 /**
 * This is the handler, which takes in the input number, and calculates
 * the square by taking it from the webservice, via WebServiceTemplate, and then finally
 * sets as a ModelAttribute, which in turn is shown by "response.jsp"
 */
 @RequestMapping(method = RequestMethod.POST)
 public String showResponse(@ModelAttribute SquareServiceRequest squareServiceRequest, Model model) {
 //Creating the SquareServiceResponse object.
 SquareServiceResponse squareServiceResponse = new ObjectFactory().createSquareServiceResponse();
 //Sending the request object via WebServiceTemplate and getting back the response from WebService 🙂
 squareServiceResponse = (SquareServiceResponse) webServiceTemplate.marshalSendAndReceive(squareServiceRequest);
 //This was supposed to be a hard part. Piece of cake 🙂
 model.addAttribute("squareServiceResponse", squareServiceResponse);
 //Show response.jsp
 return "response";
 }

}
<pre>

10. Create a folder “views” inside WEB-INF to keep your JSP files. Inside the “views” folder create two JSP files:

  1. request.jsp    -> This will ask user a number whose square is to be calculated using WebService: getSquare.
  2. response.jsp -> This will show the user calculated square, which came in response from getSquare webservice.

request.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page session="false" %>

<html>
<head>
<%@ page isELIgnored="false" %>
<title>Square Webservice 2 Client</title>
</head>
<body>
 <form:form commandName="squareServiceRequest" method="post">
 <table>
 <tr>
 <td>Enter Number:</td>
 <td><form:input path="input"/></td>
 <td><font color="red"><form:errors path="input"/></font></td>
 </tr>
 </table>
 <input name="submit" type="submit" value="Get it's square!" />
 </form:form>
</body>
</html>

response.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<%@ page isELIgnored="false" %>
<title>Square Webservice 2 Client</title>
</head>
<body>
 The square of the entered number is: <b><font color="red" size="3">${squareServiceResponse.output}</font></b>
 <br />
 <br /> This is brought to you by <i>Spring Webservices 2</i> and <font color="blue" size="4.5"><b>Ankeet Maini</b></font> 🙂
</body>
</html>

11. Define your web.xml. Make sure web.xml is directly inside the WEB-INF.


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Archetype Created Web Application</display-name>
 <servlet>
 <servlet-name>spring-ws2-client</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
 <servlet-name>spring-ws2-client</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

12. Create your spring-ws2-client-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:sws="http://www.springframework.org/schema/web-services"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/web-services
 http://www.springframework.org/schema/web-services/web-services-2.0.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <!-- Activates various annotations to be detected in bean classes -->
 <context:annotation-config />

<!-- Scans the classpath for annotated components that will be auto-registered
 as Spring beans. For example @Controller and @Service. Make sure to set the
 correct base-package -->
 <context:component-scan base-package="ankeet.spring" />
 <context:component-scan base-package="com.wordpress" />

<!-- Configures the annotation-driven Spring MVC Controller programming
 model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
 <mvc:annotation-driven />
 <!-- Declare a view resolver -->
 <bean id="viewResolver"
 class="org.springframework.web.servlet.view.InternalResourceViewResolver"
 p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

 <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"
 p:marshaller-ref="jaxbMarshaller"
 p:unmarshaller-ref="jaxbMarshaller"
 p:defaultUri="http://localhost:8080/spring-ws2-exemplary/squareService"
 p:messageSender-ref="messageSender">
 <constructor-arg ref="messageFactory"/>
 </bean>
 <bean id="messageSender" class="org.springframework.ws.transport.http.CommonsHttpMessageSender"/>
 <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>
 <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"
 p:contextPath="com.wordpress.ankeetmaini.spring_ws2_square"/>

</beans>

13. Lastly, define your POM.xml file so that maven can provide for all your dependencies.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>ankeet.ws2.client</groupId>
 <artifactId>spring-ws2-client</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>spring-ws2-client Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
 <dependency>
 <groupId>javax.xml.bind</groupId>
 <artifactId>jaxb-api</artifactId>
 <version>2.0</version>
 </dependency>
 <dependency>
 <groupId>com.sun.xml.bind</groupId>
 <artifactId>jaxb-impl</artifactId>
 <version>2.0.3</version>
 </dependency>
 <dependency>
 <groupId>org.apache.xmlbeans</groupId>
 <artifactId>xmlbeans</artifactId>
 <version>2.4.0</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-beans</artifactId>
 <version>3.0.5.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>3.0.5.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context-support</artifactId>
 <version>3.0.5.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework.ws</groupId>
 <artifactId>spring-ws-core</artifactId>
 <version>2.0.3.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>3.0.5.RELEASE</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>3.0.5.RELEASE</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.14</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-tx</artifactId>
 <version>3.0.5.RELEASE</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>jstl</groupId>
 <artifactId>jstl</artifactId>
 <version>1.1.2</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>taglibs</groupId>
 <artifactId>standard</artifactId>
 <version>1.1.2</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>3.0.5.RELEASE</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-aop</artifactId>
 <version>3.0.5.RELEASE</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>

<dependency>
 <groupId>commons-httpclient</groupId>
 <artifactId>commons-httpclient</artifactId>
 <version>3.1</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>
 </dependencies>
 <build>
 <finalName>spring-ws2-client</finalName>
 </build>
</project>

14. Check your directory structure. Make sure you are not missing anything, neither the resource, nor the structure.

15. To run this web client, first run the webservice, spring-ws2-exemplary project, download it from here.

16. Run the client now. The project is hosted on google codes. Download it from here. Now, run the client by importing it as Existing Maven Projects.

Enter the number, and press the button.

That’s all folks. If you come across any problem, do let me know, and if you do go a step further and do some improvements then do share it here, and I’ll incorporate into the post citing your name and contribution. 🙂

Spring Web Services 2. Made easy!

We’ll see how to develop Spring Webservices 2, along with validation on the base of xml schema. We’ll use spring-ws 2, maven, jaxb for making this sample project spring-ws2-exemplary. 

Source Code!

You don’t need to copy paste the code, it’s available for download at the bottom of the page. The project may give some errors while building due to non-conformance of Maven 3, so for that I’ve enhanced the POM and you can download the new project from here.

We’ll see the whole process in easy steps, making no big dragon of this!

1. I presume you are probably aware of maven. Setting it up is easy. If you’re not a maven-compatriot, don’t worry, google it, and you’ll have it up and running in a matter of mins.

So all said and done, lets start the development rightaway.

2. Open command prompt and navigate to your workspace location, or wherever you need to make the project.

3. Type in the following command, all in one line.

mvn archetype:create -DarchetypeGroupId=org.springframework.ws -DarchetypeArtifactId=spring-ws-archetype -DarchetypeVersion=2.0.1.RELEASE -DgroupId=ankeet.spring.ws2 -DartifactId=spring-ws2-exemplary

So there you have the basic skeleton of the project ready for you, including the spring config files, pom.xml and the requisite folder structure. Go have look 😉

4. Now you have the basic structure with you, but it still doesn’t qualifies to be imported as a project in eclipse or SpringsourceToolSuite (STS), as it lacks the .classpath and .project files.

To overcome, go to the project folder spring-ws2-exemplary in this case and run the following command:-

mvn eclipse:eclipse

Now you can import this as Existing Projects into Workspace

5. Creating the xml request and response messages. We’ll create a service SquareService, which will give the square of the input number.

So our request will be

        <SquareServiceRequest>
	   <Input>3</Input>
	</SquareServiceRequest>

and response will be

       <SquareServiceResponse>
	  <Output>9</Output>
       </SquareServiceResponse>

6. Since, you now have the request and response messages combine it into one xml file and generate the xsd.

Combining the request and response in a file spring-ws2-square.xml

<?xml version="1.0" encoding="UTF-8"?>
<SquareService xmlns="https://ankeetmaini.wordpress.com/spring-ws2-square">

	<SquareServiceRequest>
		<Input>3</Input>
	</SquareServiceRequest>

	<SquareServiceResponse>
		<Output>9</Output>
	</SquareServiceResponse>

</SquareService>

7. To generate the xsd, you can use trang.jar if you don’t have anything else. Its an open source product.

Download the latest version and unzip it on your system. Open command prompt and navigate to the unzipped folder.

To create the XSD run the following command:

java -jar trang.java <Path-of-XML-file> <Path-where-xsd-is-to-be-generated>

I copied my xml to the trang direcotory only, and generated my corresponding xsd there too, and then copied it to WEB-INF. Simple.

So you have the xsd generated in the current directory. Before putting it into use, we need to tweak it a little bit to suit our purposes.

Original spring-ws2-square.xsd


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="https://ankeetmaini.wordpress.com/spring-ws2-square" xmlns:s="https://ankeetmaini.wordpress.com/spring-ws2-square">
  <xs:element name="SquareService">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="s:SquareServiceRequest"/>
        <xs:element ref="s:SquareServiceResponse"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="SquareServiceRequest">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="s:Input"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Input" type="xs:integer"/>
  <xs:element name="SquareServiceResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="s:Output"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Output" type="xs:integer"/>
</xs:schema></pre>

Editing it, we get the final xsd. Please note the finer details yourself, and try to comprehend. You’ll be able to figure out the need to edit it most of the times for the sake of more conciseness, and removal of extraneous elements, if you know what I mean 😉

Final spring-ws2-square.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="https://ankeetmaini.wordpress.com/spring-ws2-square" xmlns:s="https://ankeetmaini.wordpress.com/spring-ws2-square">

  <xs:element name="SquareServiceRequest">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Input" type="xs:integer"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="SquareServiceResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Output" type="xs:integer"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

Now copy this to the src/main/webapp/WEB-INF folder, so that it’ll be detected at classpath.

8. Open web.xml located in WEB-INF folder and change the name of the servlet to spring-ws2 (This is not required but I do, to remove the ambiguity, if at all it arises). Don’t forget to change in the <servlet-maping> also.

Add an init param

<init-param>
			<param-name>transformWsdlLocations</param-name>
			<param-value>true</param-value>
		</init-param>

Verify your web.xml looks like this:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>Archetype Created Web Application</display-name>

 	<servlet>
		<servlet-name>spring-ws2</servlet-name>
		<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>transformWsdlLocations</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>

    <servlet-mapping>
        <servlet-name>spring-ws2</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

9. Now, since you changed the servlet’s name, you need to change the Spring’s configuration file too, because the container will look for <servlet-name>-servlet.xml

Rename (or better Refactor) it to spring-ws2-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:sws="http://www.springframework.org/schema/web-services"
       xsi:schemaLocation="http://www.springframework.org/schema/beans	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
	   http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/web-services                      	http://www.springframework.org/schema/web-services/web-services-2.0.xsd">

    <!-- To detect @Endpoint -->
<sws:annotation-driven/>

<!-- To detect @Service, @Component etc -->
<context:component-scan base-package="ankeet.spring" />

    <!-- To generate dynamic wsdl -->
	<sws:dynamic-wsdl
		id="getSquare"
		portTypeName="SquareService"
		locationUri="/squareService"
		targetNamespace="https://ankeetmaini.wordpress.com/spring-ws2-square">
		<sws:xsd location="/WEB-INF/spring-ws2-square.xsd"/>
	</sws:dynamic-wsdl>

    <!-- For validating your request and response -->
    <!-- So that you don't send a string instead of an integer -->

	 <sws:interceptors>
<bean id="validatingInterceptor"
        class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
    <property name="schema" value="/WEB-INF/spring-ws2-square.xsd"/>
    <property name="validateRequest" value="true"/>
    <property name="validateResponse" value="true"/>
</bean>
  </sws:interceptors>

</beans>

10. In Eclipse/STS right click your project and and then new, and select a new source folder and name it as src/main/java

Create three packages : ankeet.spring.ws2.endpoint, ankeet.spring.ws2.service, ankeet.spring.ws2.generated

11. Since we’re using jaxb for converting xml <—> object, we need to make changes to the pom.xml to add its dependencies and plugins, so that the Java Classes are generated at compile time from the XSD given.

Add the following piece of code to the pom in the <plugins> section

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
	<goal>xjc</goal>
      </goals>
     </execution>
   </executions>
  <configuration>
    <schemaDirectory>src/main/webapp/WEB-INF/</schemaDirectory>
  </configuration>
</plugin>

Similarly add the dependencies in the <dependencies> section of POM.

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.0</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.0.3</version>
</dependency>
<dependency>
  <groupId>org.apache.xmlbeans</groupId>
  <artifactId>xmlbeans</artifactId>
  <version>2.4.0</version>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.8.1</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <scope>compile</scope>
  <version>1.2.16</version>
</dependency>

11. Write the endpoint class as follows:-

SquareWSEndpoint.java

/**
 *
 */
package ankeet.spring.ws2.endpoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import ankeet.spring.ws2.generated.*;
import ankeet.spring.ws2.generated.SquareServiceResponse;
import ankeet.spring.ws2.service.SquareService;

/**
 * @author Ankeet Maini
 *
 */
@Endpoint
public class SquareWSEndpoint {
	//To calculate square of the input.
	@Autowired
	private SquareService squareService;
	//This is like @RequestMapping of Spring MVC
	@PayloadRoot(localPart="SquareServiceRequest", namespace="https://ankeetmaini.wordpress.com/spring-ws2-square")
	@ResponsePayload
	public SquareServiceResponse getSquare(@RequestPayload SquareServiceRequest request) {
		SquareServiceResponse response = new ObjectFactory().createSquareServiceResponse();
		response.setOutput(squareService.square(request.getInput()));
		return response;
	}
}

12. Now write the service classes which will actually do the squaring.

SquareService.java

package ankeet.spring.ws2.service;

import java.math.BigInteger;

public interface SquareService {
	public BigInteger square(BigInteger bigInteger);

}

And it’s implementation class SquareServiceImpl.java

package ankeet.spring.ws2.service;

import java.math.BigInteger;

import org.springframework.stereotype.Service;

@Service
public class SquareServiceImpl implements SquareService {

	public BigInteger square(BigInteger bigInteger) {
		return (bigInteger.multiply(bigInteger));
	}

}

13. Now when you clean your project JAXB will create the classes from the XSD present on the classpath, in our case spring-ws2-square.xsd located in WEB-INF. The code generated will be in the target folder, so you’ll have to copy the files and put into the ankeet.spring.ws2.generated package, and correct the inconsistencies arising in the package names by shifting. Also, make src/main/webapp a source folder(By navigating to webapp and then right clicking and in Configure Buildpath, make it a source folder).

14. After this your project structure should prcisely look like this.

15. To run the project you can create a war and deploy on the server, or use maven. Go the project directory and type the following command to run the project on tomcat server:

mvn tomcat:run

16. Open your web browser and enter the following url

http://localhost:8080/spring-ws2-exemplary/getSquare.wsdl to see the WSDL file generated.

17. To test the application, you can build a client or use Soap UI. To test with soap UI, open it, create an new project and give it the above url of WSDL.

Press Ok.

Click on Request1 and on the left side an interface will appear asking your request. Enter any integer and press the green button to send it to the server. If you enter anything other than an integer, a Validation error will be reported on the other side.

and if you do enter any thing else than integer, say a string, the Validation will fail and you’ll get the following error.

Just in case if you wish to know, the above Response is

<pre><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Client</faultcode>
         <faultstring xml:lang="en">Validation error</faultstring>
         <detail>
            <spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-datatype-valid.1.2.1: 'a' is not a valid value for 'integer'.</spring-ws:ValidationError>
            <spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-type.3.1.3: The value 'a' of element 'spr:Input' is not valid.</spring-ws:ValidationError>
         </detail>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The code is hosted on Google Codes, to download the project click here.

To run it, just unzip the file, open command prompt, navigate to the directory and type : mvn tomcat:run, and voila you’re done!

While creating this project I read a lot, and found these blogs extremely helpful,

1. Jamesbnuzzo

2. Ice09

3. krams

That’s all for this one. Enjoy building and running it. 🙂