Spring Web Services - Part 1 (Create JAXB classes automatically using Maven)

Today we are starting a series of Java Web Services using Spring tutorial series, this spring web services tutorial series with ends with three parts. In todays discussion we will cover xsd to java classes using maven. In our previous discussion we came across, What are Web Services ?, What is XSD ?, What is WSDL ? and a basic introduction to Spring Web Services. In this particular blog we will create java classes from an xsd automatically using Maven in Eclipse. We will start from a simple Maven project having required XSD?s, than we will create JAXB files automatically using Maven.

Our objective for today?s discussion is to create an Maven web application in Eclipse with required pom.xml entries and setup so that our objective xsd to java classes can be obtained. It will contain two Web Services one to except user?s details and save them in My Sql database. In second service we will retrieve those user?s details on the basis of their id?s.


Database Setup

Before we start writing code for the application let?s first create a database that will be used by our application to save and retrieve User?s data. Here is sql script :
      -- Dumping database structure for userdb
      CREATE DATABASE IF NOT EXISTS `userdb` 

      /*!40100 DEFAULT CHARACTER SET latin1 */;
      USE `userdb`;

      -- Dumping structure for table userdb.user
      CREATE TABLE IF NOT EXISTS `user` ( `id` varchar(10) DEFAULT NULL, `name` varchar(10) DEFAULT NULL, `gender` varchar(10) DEFAULT NULL, `status` varchar(10) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
   

Create Simple Spring Web Service in Eclipse using Maven

Creating Spring Web Services in Eclipse using Maven is a four step process, and everything will be done.

Step 1 : Create a simple Maven Project in Eclipse and add required goal entries and dependencies details to pom.xml.
Step 2 : Create xsd files as per the functioning, the Web Service is going to implement. Add them to project (ex. /WEB-INF/schemas/..) , and generate classes using Maven.
Step 3 : Create an Endpoint, Service and Dao layer classes for the application.
Step 4 : Create spring-configurations and test the Web Services.

Now let?s discuss all these steps in details and create a simple Web Service application step by step.

Step 1 : Create a simple Maven Project in Eclipse and add required goal entries and dependencies details to pom.xml.

Create a Maven Project from eclipse (File > New > Project > Maven Project) and provide Group Id and Artifact Id as ?spring-server-final? or something you desire and replace your pom.xml contents with this file mentioned below :
<?xml version="1.0"?>
<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">
   <artifactId>spring-server-final</artifactId>
   <modelVersion>4.0.0</modelVersion>
   <packaging>war</packaging>
   <groupId>spring-server-final</groupId>
   <version>1.0-SNAPSHOT</version>
   <name>spring-server-final</name>
   <url>http://maven.apache.org</url>
   <repositories>
      <repository>
         <id>spring-maven-milestone</id>
         <name>Springframework Maven Repository</name>
         <url>http://maven.springframework.org/milestone</url>
      </repository>
   </repositories>
   <properties>
      <spring.version>3.2.2.RELEASE</spring.version>
      <spring.ws.version>2.0.0.RELEASE</spring.ws.version>
      <context.path>spring-webservice-api</context.path>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.6</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-beans</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aop</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aspects</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>commons-collections</groupId>
         <artifactId>commons-collections</artifactId>
         <version>3.2</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-oxm</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.ws</groupId>
         <artifactId>spring-ws-core</artifactId>
         <version>${spring.ws.version}</version>
      </dependency>
      <dependency>
         <groupId>org.apache.ws.commons.schema</groupId>
         <artifactId>XmlSchema</artifactId>
         <version>1.4.3</version>
      </dependency>
      <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>
   </dependencies>
   <build>
      <finalName>spring-server-final</finalName>
      <pluginManagement>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>2.3.1</version>
               <configuration>
                  <source>1.6</source>
                  <target>1.6</target>
               </configuration>
            </plugin>
            <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/schemas/</schemaDirectory>
               </configuration>
            </plugin>
         </plugins>
      </pluginManagement>
   </build>
</project>

Step 2 :  Create xsd files as per the functioning, the Web Service is going to implement. Add them to project (ex. /WEB-INF/schemas/..)

Create a new folder /webapp/schemas/  under /src/main/ and copy xsd files listed below there.

\src\main\webapp\schemas\UserDetails.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://user.javaguys.blog.com" targetNamespace="http://user.javaguys.blog.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
   <xs:element name="User" type="User" />
   <xs:complexType name="User">
      <xs:sequence>
         <xs:element name="UserId" type="xs:string" />
         <xs:element name="UserName" type="xs:string" />
         <xs:element name="UserGender" type="xs:string" />
         <xs:element name="UserStatus" type="xs:string" />
      </xs:sequence>
   </xs:complexType>
</xs:schema>

\src\main\webapp\schemas\GetUserServiceOperations.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://com/javaguys/webservices/getUserServices" xmlns:user="http://user.javaguys.blog.com" targetNamespace="http://com/javaguys/webservices/getUserServices" elementFormDefault="qualified">
   <xsd:import namespace="http://user.javaguys.blog.com"  schemaLocation="UserDetails.xsd" />
   <xsd:element name="GetUserRequest">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="userId" type="xsd:string" />
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
   <xsd:element name="GetUserResponse">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="userDetails" type="user:User" />
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>

\src\main\webapp\schemas\SaveUserServiceOperations.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://com/javaguys/webservices/saveUserServices" xmlns:user="http://user.javaguys.blog.com" targetNamespace="http://com/javaguys/webservices/saveUserServices" elementFormDefault="qualified">
   <xsd:import namespace="http://user.javaguys.blog.com"  schemaLocation="UserDetails.xsd" />
   <xsd:element name="SaveUserRequest">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="userDetails" type="user:User" />
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
   <xsd:element name="SaveUserResponse">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="userId" type="xsd:string" />
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>
Now do a maven-install mvn install -Dmaven.test.skip=true, and you will get all required classes automatically created in target folder. See the picture below :

Just copy these files to your src folder and you are done with almost half of your work. Please note that while copying copy from ?com? folder and exclude jaxb and generated-sources folders. After copying these classes to your src folder your project will look something like this picture:



In this tutorial we saw how to generate Java classes from .xsd files using Maven, the task is almost done we only need to add some database related files and simple java code. Step 3 and Step 4 are covered in the next part of this tutorial. Click Here to go next part of the tutorial.

Download "Spring Web Service Example project" from "SkyDrive"