dimitrisd
dimitrisd

Reputation: 652

Create java classes from an xml schema

The point is to make a simple white-pages web service which adds a person to a phone book (called addPerson() ) using SOAP. If there is already a person with the same first- and last name in the phone book, a fault message should be returned.

I made the following wsdl file

 <?xml version="1.0" encoding="UTF-8"?>
<definitions name="whitepages" targetNamespace="http://whitepages.ws"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://whitepages.ws" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
    <types>
        <xsd:schema targetNamespace="http://whitepages.ws" xmlns:tns="http://whitepages.ws">
            <xsd:complexType name="personType">
                <xsd:sequence>
                    <xsd:element name="address" type="tns:addressType"></xsd:element>
                </xsd:sequence>
                <xsd:attribute name="firstName" type="xsd:string"/>
                <xsd:attribute name="lastName" type="xsd:string"/>
                <xsd:attribute name="phone" type="xsd:string"/>
            </xsd:complexType>
            <xsd:complexType name="addressType">
                <xsd:sequence>
                    <xsd:element name="street">
                        <xsd:complexType>
                            <xsd:sequence/>
                        </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="postcode">
                        <xsd:complexType>
                            <xsd:sequence/>
                        </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="city">
                        <xsd:complexType>
                            <xsd:sequence/>
                        </xsd:complexType>
                    </xsd:element>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="person" type="tns:personType"></xsd:element>
            <xsd:element name="response" type="xsd:string"></xsd:element>
            <xsd:complexType name="faultType">
                <xsd:sequence>
                    <xsd:element name="errorMessage" type="xsd:string"></xsd:element>
                    <xsd:element name="person" type="tns:personType"></xsd:element>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="fault" type="tns:faultType"></xsd:element>
        </xsd:schema>
    </types>
    <message name="whitepagesOperationRequest">
        <part name="person" element="tns:person"/>
    </message>
    <message name="whitepagesOperationResponse">
        <part name="response" element="tns:response"/>
    </message>
    <message name="WPFault">
        <part name="errorMessage" type="xsd:string"/>
        <part name="person" element="tns:person"/>
    </message>
    <portType name="whitepages">
        <operation name="addPerson">
            <input name="input1" message="tns:whitepagesOperationRequest"/>
            <output name="output1" message="tns:whitepagesOperationResponse"/>
            <fault name="fault1" message="tns:WPFault"/>
        </operation>
    </portType>
    <binding name="whitepagesBinding" type="tns:whitepages">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="addPerson">
            <soap:operation style="document"/>
            <input name="input1">
                <soap:body use="literal"/>
            </input>
            <output name="output1">
                <soap:body use="literal"/>
            </output>
            <fault name="fault1">
                <soap:fault name="fault1" use="literal"/>
            </fault>
        </operation>
    </binding>
    <service name="whitepagesService">
        <port name="whitepagesBindingPort" binding="tns:whitepagesBinding">
            <soap:address location="http://localhost:${HttpDefaultPort}/service"/>
        </port>
    </service>
</definitions>

I validated the xml and now I need to make the java classes(for both) for that schema but I don't know how to proceed...Any advice or tip would be really appreciated..

Thanks in advance

Upvotes: 3

Views: 1707

Answers (1)

Victor
Victor

Reputation: 2546

The eclipse distribution that I am using has a wizard for creating JAXB class from schema:

  • File -> New -> Other -> Create JaxB classes from schema.
  • You will have to provide the target package for the generated output
  • You might need to provide a binding file if you need any customization
  • This is the detailed info on the eclipse distribution:

Eclipse Java EE IDE for Web Developers.

Version: Juno Service Release 1 Build id: 20120920-0800>

(c) Copyright Eclipse contributors and others 2005, 2012. All rights reserved. Visit http://www.eclipse.org/webtools

Another option is to use the XJC compiler to generate the classes from the schema. it is included in the standard JDK.

In any case, you will have to extract the schema definitions to a XSD file and reference it from your WSDL:

    <schema xmlns="http://www.w3.org/2001/XMLSchema">
        <import namespace="http://whitepages.ws" schemaLocation="NAME_OF_Your_XSD_FILE.xsd" />             
    </schema>

Upvotes: 1

Related Questions