This is the third part in the series about SOAP Webservices with Maven. It continues after SOAP Webservices with Maven, Project setup
There are two ways of building up a SOAP Webservice, based on Java classes and based on XSDs. The latter is called Contract-First. This is the approach I prefer, and this is also the approach described in this tutorial.
XSD
Since the Contract-First approach uses the XSDs as a starting point, this is where the actual building of the webservice will begin.
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://coding.ractoc.com/tutorials/calculator" xmlns:tns="http://coding.ractoc.com/tutorials/calculator" xmlns:data="http://coding.ractoc.com/tutorials/data" elementFormDefault="qualified"> <element name="addValuesRequest" type="tns:addValuesRequest" /> <element name="addValuesReply" type="decimal" /> <complexType name="addValuesRequest"> <all> <element name="a" type="decimal" /> <element name="b" type="decimal" /> </all> </complexType> </schema>
This is a very basic XSD. It described two elements, an addValueRequest and an addValueResponse. These are the request and response data types for the webservice. In this case, the webservice simply adds two numbers together and returns the result.
The addValuesRequest is a complex type contains two values, a and b.
The addValuesResponse is a straight decimal type.
In later parts of the tutorial you will see a much more complex XSD but for now, this one will do to get the basics across.
If you want to know more on how the write an XSD and what are the various things you can do to write them feel free to use Google since that’s not really the point of this tutorial.
Code generation
This XSD should off course be created in the correct location, src/main/webapp/schemas. That way, Maven will be able to find it. Once the XSD is in the correct location, it is time to generate the code. For this we run a basic maven command.
mvn clean install
The clean makes sure that any previously generated code is correctly cleaned up before generating the new version. This prevents any hard to diagnose problems from old classes remaining.
Generated content
The code generation created 3 items:
- AddValuesRequest.java
- ObjectFactory.java
- package-info.java
AddValuesRequest.java
This is the actual java representation of the request object described in the XSD. Aside form the get and set methods, the important bit is at the beginning of the class.
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "addValuesRequest", propOrder = { }) public class AddValuesRequest { @XmlElement(required = true) protected BigDecimal a; @XmlElement(required = true) protected BigDecimal b; // getters and setters }
The annotations at the class level define this class as an XMLType, this means it represents a component in an XML document. The XmlAccesorType states that it is a FIELD component.
The XMLElement on the parameters makes them availlable for inclusion in the actual XML document. The XMLElement also indicates that both parameters are required. This is becuase in the XSD they were put in an block. Had they been put in a block, they would have been optional.
ObjectFactory.java
The object factory can be used to marshal and unmarshal between the XML documents and the Java objects. This class is used by the jax-ws framework to marshal and unmarshal the SOAP messages.
package-info.java
This class contains some meta data from the generation process.
This tutorial continuous in the next part of the series, SOAP webservices with Maven, Webservice