Create WSDL Java API
The following listing shows how to create a WSDL document with Java and the Membrane SOA Model.
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 32 33 34 35 36 37 38 39 40 41 | package sample.wsdl; import java.io.StringWriter; import groovy.xml.MarkupBuilder; import com.predic8.wsdl.*; import com.predic8.wsdl.creator.*; import com.predic8.schema.*; import static com.predic8.schema.Schema.*; public class GenerateWSDL { public static void main(String[] args) { dumpWSDL(createWSDL()); } private static Definitions createWSDL() { schema.newElement( "add" ).newComplexType().newSequence().newElement( "summand" , INT).setMaxOccurs( "unbounded" ); schema.newElement( "addResponse" ).newComplexType().newSequence().newElement( "number" , INT); wsdl.add(schema); PortType pt = wsdl.newPortType( "AddPortType" ); Operation op = pt.newOperation( "add" ); op.newInput( "add" ).newMessage( "add" ).newPart( "parameters" , "tns:add" ); op.newOutput( "addResponse" ).newMessage( "addResponse" ).newPart( "parameters" , "tns:addResponse" ); return wsdl; } private static void dumpWSDL(Definitions wsdl) { StringWriter writer = new StringWriter(); WSDLCreator creator = new WSDLCreator(); creator.setBuilder( new MarkupBuilder(writer)); wsdl.create(creator, new WSDLCreatorContext()); System.out.println(writer); } } |
After executing you get the output shown in listing 2. It is an abstract WSDL document with an embedded XML Schema.
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 32 | < definitions targetNamespace = 'http://predic8.com/wsdl/AddService/1/' xmlns = 'http://schemas.xmlsoap.org/wsdl/' xmlns:wsdl = 'http://schemas.xmlsoap.org/wsdl/' xmlns:tns = 'http://predic8.com/wsdl/AddService/1/' /> < types > < xsd:schema targetNamespace = 'http://predic8.com/add/1/' attributeFormDefault = 'unqualified' elementFormDefault = 'unqualified' xmlns:xsd = 'http://www.w3.org/2001/XMLSchema' > < xsd:element name = 'add' minOccurs = '1' maxOccurs = '1' > < xsd:complexType > < xsd:sequence > < xsd:element name = 'summand' type = 'xsd:int' minOccurs = '1' maxOccurs = 'unbounded' /> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:element name = 'addResponse' minOccurs = '1' maxOccurs = '1' > < xsd:complexType > < xsd:sequence > < xsd:element name = 'number' type = 'xsd:int' minOccurs = '1' maxOccurs = '1' /> </ xsd:sequence > </ xsd:complexType > </ xsd:element > </ xsd:schema > </ types > < message name = 'add' > < part name = 'parameters' element = 'tns:add' /> </ message > < message name = 'addResponse' > < part name = 'parameters' element = 'tns:addResponse' /> </ message > < portType name = 'AddPortType' > < operation name = 'add' > < input message = 'tns:add' name = 'add' /> < output message = 'tns:addResponse' name = 'addResponse' /> </ operation > </ portType > </ definitions > |
Now let's have a look at the code from listing 1 in detail. In line 9 we have a static import that makes constants for the build-in XML Schema types available.
import static com.predic8.schema.Schema.*;
First we create a new Schema with the targetnamespace "http://predic8.com/add/1/" at line 18.
Schema schema = new Schema("http://predic8.com/add/1/");
Then we create a new element for the add operation. Using the chained syntax we can host a new complex type into the element and within that we host a new sequence. This notation makes it very comfortable to create schemas especially for Web Services.
schema.newElement("add").newComplexType().newSequence().newElement("summand", INT).setMaxOccurs("unbounded");
Next we create the WSDL document for the AddService in the targetnamespace.
Definitions wsdl = new Definitions("http://predic8.com/wsdl/AddService/1/", "AddService"); wsdl.add(schema);
Then we create a new portType and the add operation.
PortType pt = wsdl.newPortType("AddPortType"); Operation op = pt.newOperation("add");
In line 29 you can see how an input for the add operation is created. And now the input is linked over a part to the fomerly created add element.
op.newInput("add").newMessage("add").newPart("parameters", "tns:add");