torsdag, november 22, 2007

Building a Web Service from a XSD using JDeveloper

Yesterday I was asked how to create a Web Service from a XSD file using JDeveloper by a colleague, so I thought I'd might share the answer I gave to a wider audience. Hopefully someone else out there might also have some use for it.

The purpose of this example is to show how you can build a Java Web Service starting with only a single XSD file using JDeveloper.

1. Create an empty project in JDeveloper (I used version 10.1.3.3).

2. Add the XSD to your project, in this example I use an XSD that looks like:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.oracle.com/pcbpel/CountryInfo"
elementFormDefault="qualified">
<element name="CountryInfo">
<complexType>
<sequence>
<element name="Country" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="Name" type="string"/>
<element name="Capital" type="string"/>
<element name="Area" type="decimal"/>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>

3. Select the XSD in the Applications Navigator

4. Select Tools -> JAXB Compilation from the Menu

5. Now you have JAXB generated Java classes based on your XSD in your project. This is described more in:

http://www.oracle.com/webapps/online-help/jdeveloper/10.1.3?navId=4&navSetId=_&vtTopicFile=working_with_xml/xml_pjaxb.html

6. Create a new Java class, this is the class that will be the Web Service

7. In this class create a method (this will be exposed as a Web Service method) it has a javax.xml.soap.SOAPElement as input parameter and void as return value, like:

public void testMethod(javax.xml.soap.SOAPElement myDoc)

8. In the method body, add code to unmarshal the document and in this example just write it to System.out:

try {
JAXBContext jc = JAXBContext.newInstance("project3");
Unmarshaller u = jc.createUnmarshaller();

CountryInfo countryInfo = (CountryInfo)u.unmarshal(myDoc);
List lst = countryInfo.getCountry();

for(Iterator iter = lst.iterator(); iter.hasNext();) {
CountryInfo.CountryType country = (CountryInfo.CountryType)iter.next();
System.out.println( country.getName() + ", " + country.getCapital());
}
}
catch (JAXBException je) { je.printStackTrace(); }


9. Start the Java Web Service wizard from the New Gallery, select the newly generated class and just use the default values in the Wizard, except for SOAP Format, here use Document/Literal.

10. Before deploying the Web Service, make sure that all needed files are included in the deployment profile.

11. Once done, either deploy the Web Service to an Application Server or standalone OC4J instance and test it using XML like:

<?xml version="1.0" encoding="UTF-8" ?>
<CountryInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.oracle.com/pcbpel/CountryInfo src/xsd/CountryInfo.xsd"
xmlns="http://www.oracle.com/pcbpel/CountryInfo">
<Country>
<Name>Sweden</Name>
<Capital>Stockholm</Capital>
<Area>1.0</Area>
</Country>
</CountryInfo>

this should give the following printed to System.out:

07/11/22 12:29:31 Sweden, Stockholm

4 kommentarer:

  1. What is the WSDL's view of this? I'm pretty sure the WSDL will accept any document - and not use the XSD to type the service this way?

    SvaraRadera
  2. Yes, you’re right; the WSDL will take an any element here as input, and this is of course not ideal, so the WSDL will have to be modified manually.

    Thanks for pointing this out!

    Cheers,
    Stellan

    SvaraRadera
  3. I found this very useful.
    Can you please share complete jdeveloper workspace for this project.

    thanks a lot

    SvaraRadera