Search code examples
javaweb-serviceswsdl

How to use the User Defined Datatypes found in the WSDL file in our java code?


I am trying to consume a remote web service using wsimport tool. It generates the necessary client side code. In that I found the return type of the method is user defined by using the xml complex type. how can I use this complex types in my java code. Here is the code what I try

import net.restfulwebservices.servicecontracts._2008._01.WeatherForecastService;
import net.restfulwebservices.servicecontracts._2008._01.IWeatherForecastService;
import com.microsoft.schemas._2003._10.serialization.arrays.ArrayOfstring;
public class WeatherForecastClient 
{ 
    public static void main(String args[])throws     net.restfulwebservices.servicecontracts._2008._01.IWeatherForecastServiceGetCitiesByCountryDefaultFaultContractFaultFaultMessage
    {
        try
        {
            WeatherForecastService wf=new WeatherForecastService();
            IWeatherForecastService iw=wf. getBasicHttpBindingIWeatherForecastService();
            ArrayOfstring as=new ArrayOfstring();
            as=iw.getCitiesByCountry("US");
            System.out.println(as);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

In this ArrayOfstring is the user defined type. I found a file named ArrayOfString that contains the following code.

    package com.microsoft.schemas._2003._10.serialization.arrays;

import java.util.ArrayList;
    import java.util.List;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;
    /**
    * <p>Java class for ArrayOfstring complex type.
    * 
    * <p>The following schema fragment specifies the expected content contained within this class.
    * 
    * <pre>
    * &lt;complexType name="ArrayOfstring">
    *   &lt;complexContent>
    *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
    *       &lt;sequence>
    *         &lt;element name="string" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
    *       &lt;/sequence>
    *     &lt;/restriction>
    *   &lt;/complexContent>
    * &lt;/complexType>
    * </pre>
    * 
    * 
    */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "ArrayOfstring", propOrder = {
    "string"
    })
    public class ArrayOfstring {

    @XmlElement(nillable = true)
    protected List<String> string;

    /**
     * Gets the value of the string property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the string property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getString().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link String }
     * 
     * 
     */
    public List<String> getString() {
        if (string == null) {
            string = new ArrayList<String>();
        }
        return this.string;
    }

When I executing this code I got the following exceptions

 javax.xml.ws.WebServiceException
        at com.sun.xml.internal.ws.model.RuntimeModel.createJAXBContext(Unknown
Source)
        at com.sun.xml.internal.ws.model.RuntimeModel.postProcess(Unknown Source
)
        at com.sun.xml.internal.ws.modeler.RuntimeModeler.buildRuntimeModel(Unkn
own Source)
        at com.sun.xml.internal.ws.client.ServiceContextBuilder.processAnnotatio
ns(Unknown Source)
        at com.sun.xml.internal.ws.client.ServiceContextBuilder.completeServiceC
ontext(Unknown Source)
        at com.sun.xml.internal.ws.client.WSServiceDelegate.processServiceContex
t(Unknown Source)
        at com.sun.xml.internal.ws.client.WSServiceDelegate.createEndpointIFBase
Proxy(Unknown Source)
        at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Sour
ce)
        at javax.xml.ws.Service.getPort(Unknown Source)
        at net.restfulwebservices.servicecontracts._2008._01.WeatherForecastServ
ice.getBasicHttpBindingIWeatherForecastService(WeatherForecastService.java:50)
        at WeatherForecastClient.main(WeatherForecastClient.java:12)
    Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 5 c
ounts of IllegalAnnotationExceptions
    There's no ObjectFactory with an @XmlElementDecl for the element {http://www.res
tfulwebservices.net/ServiceContracts/2008/01}Country.
    this problem is related to the following location:
                at protected javax.xml.bind.JAXBElement net.restfulwebservices.s
ervicecontracts._2008._01.GetCitiesByCountry.country
                at net.restfulwebservices.servicecontracts._2008._01.GetCitiesBy
Country
    There's no ObjectFactory with an @XmlElementDecl for the element {http://www.res
tfulwebservices.net/ServiceContracts/2008/01}GetCitiesByCountryResult.
        this problem is related to the following location:
                at protected javax.xml.bind.JAXBElement net.restfulwebservices.s
ervicecontracts._2008._01.GetCitiesByCountryResponse.getCitiesByCountryResult
                at net.restfulwebservices.servicecontracts._2008._01.GetCitiesBy
CountryResponse
    There's no ObjectFactory with an @XmlElementDecl for the element {http://www.res
tfulwebservices.net/ServiceContracts/2008/01}City.
        this problem is related to the following location:
                at protected javax.xml.bind.JAXBElement net.restfulwebservices.s
ervicecontracts._2008._01.GetForecastByCity.city
                at net.restfulwebservices.servicecontracts._2008._01.GetForecast
ByCity
    There's no ObjectFactory with an @XmlElementDecl for the element {http://www.res
tfulwebservices.net/ServiceContracts/2008/01}Country.
        this problem is related to the following location:
                at protected javax.xml.bind.JAXBElement net.restfulwebservices.s
ervicecontracts._2008._01.GetForecastByCity.country
                at net.restfulwebservices.servicecontracts._2008._01.GetForecast
ByCity
    There's no ObjectFactory with an @XmlElementDecl for the element {http://www.res
tfulwebservices.net/ServiceContracts/2008/01}GetForecastByCityResult.
        this problem is related to the following location:
                at protected javax.xml.bind.JAXBElement net.restfulwebservices.s
ervicecontracts._2008._01.GetForecastByCityResponse.getForecastByCityResult
                at net.restfulwebservices.servicecontracts._2008._01.GetForecast
ByCityResponse

        at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Buil
der.check(Unknown Source)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(U
nknown Source)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown S
ource)
        at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Sou
rce)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(Unknown Sourc
e)
        at com.sun.xml.internal.ws.model.RuntimeModel$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        ... 11 more

Anyone Can give any suggestions why this exception is occured.Thanks in advance

WSDL FILE

 <wsdl:definitions name="WeatherForecastService" targetNamespace="http://www.restfulwebservices.net/ServiceContracts/2008/01">                
    <wsdl:types>    
    <xsd:schema targetNamespace="http://www.restfulwebservices.net/ServiceContracts/2008/01/Imports">
    <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/WeatherForecastService.svc?xsd=xsd0" namespace="http://www.restfulwebservices.net/ServiceContracts/2008/01"/>
    <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/WeatherForecastService.svc?xsd=xsd3" namespace="http://GOTLServices.FaultContracts/2008/01"/>
    <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/WeatherForecastService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
    <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/WeatherForecastService.svc?xsd=xsd2" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
    <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/WeatherForecastService.svc?xsd=xsd4" namespace="http://www.restfulwebservices.net/DataContracts/2008/01"/>
    </xsd:schema>
    </wsdl:types>
    <wsdl:message name="IWeatherForecastService_GetCitiesByCountry_InputMessage">    
    <wsdl:part name="parameters" element="tns:GetCitiesByCountry"/></wsdl:message>  
    <wsdl:message name="IWeatherForecastService_GetCitiesByCountry_OutputMessage"> 
    <wsdl:part name="parameters" element="tns:GetCitiesByCountryResponse"/></wsdl:message> 
    <wsdl:message name="IWeatherForecastService_GetCitiesByCountry_DefaultFaultContractFault_FaultMessage">
    <wsdl:part name="detail" element="q1:DefaultFaultContract"/></wsdl:message>
    <wsdl:message name="IWeatherForecastService_GetForecastByCity_InputMessage"><wsdl:part name="parameters" element="tns:GetForecastByCity"/></wsdl:message>
    <wsdl:message name="IWeatherForecastService_GetForecastByCity_OutputMessage">   
    <wsdl:part name="parameters" element="tns:GetForecastByCityResponse"/></wsdl:message>  
    <wsdl:message name="IWeatherForecastService_GetForecastByCity_DefaultFaultContractFault_FaultMessage">  
    <wsdl:part name="detail" element="q2:DefaultFaultContract"/></wsdl:message>
    <wsdl:portType name="IWeatherForecastService"><wsdl:operation name="GetCitiesByCountry">
    <wsdl:input wsaw:Action="GetCitiesByCountry" message="tns:IWeatherForecastService_GetCitiesByCountry_InputMessage"/>
    <wsdl:output wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IWeatherForecastService/GetCitiesByCountryResponse" message="tns:IWeatherForecastService_GetCitiesByCountry_OutputMessage"/>
    <wsdl:fault wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IWeatherForecastService/GetCitiesByCountryDefaultFaultContractFault" name="DefaultFaultContractFault" message="tns:IWeatherForecastService_GetCitiesByCountry_DefaultFaultContractFault_FaultMessage"/></wsdl:operation>
    <wsdl:operation name="GetForecastByCity">
    <wsdl:input wsaw:Action="GetForecastByCity" message="tns:IWeatherForecastService_GetForecastByCity_InputMessage"/>
    <wsdl:output wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IWeatherForecastService/GetForecastByCityResponse" message="tns:IWeatherForecastService_GetForecastByCity_OutputMessage"/>
    <wsdl:fault wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IWeatherForecastService/GetForecastByCityDefaultFaultContractFault" name="DefaultFaultContractFault" message="tns:IWeatherForecastService_GetForecastByCity_DefaultFaultContractFault_FaultMessage"/>
    </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="BasicHttpBinding_IWeatherForecastService" type="tns:IWeatherForecastService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="GetCitiesByCountry"><soap:operation soapAction="GetCitiesByCountry" style="document"/>
    <wsdl:input><soap:body use="literal"/></wsdl:input>
    <wsdl:output><soap:body use="literal"/></wsdl:output>
    <wsdl:fault name="DefaultFaultContractFault"><soap:fault name="DefaultFaultContractFault" use="literal"/></wsdl:fault></wsdl:operation>   
    <wsdl:operation name="GetForecastByCity"><soap:operation soapAction="GetForecastByCity" style="document"/>
    <wsdl:input><soap:body use="literal"/></wsdl:input>
    <wsdl:output><soap:body use="literal"/></wsdl:output>
    <wsdl:fault name="DefaultFaultContractFault"><soap:fault name="DefaultFaultContractFault" use="literal"/></wsdl:fault></wsdl:operation>   
    </wsdl:binding>
    <wsdl:service name="WeatherForecastService">
    <wsdl:port name="BasicHttpBinding_IWeatherForecastService" binding="tns:BasicHttpBinding_IWeatherForecastService">
    <soap:address location="http://www.restfulwebservices.net/wcf/WeatherForecastService.svc"/>
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>

XSD FILE0

<xs:schema elementFormDefault="qualified" targetNamespace="http://www.restfulwebservices.net/ServiceContracts/2008/01">
<xs:import schemaLocation="http://www.restfulwebservices.net/wcf/WeatherForecastService.svc?xsd=xsd2" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<xs:import schemaLocation="http://www.restfulwebservices.net/wcf/WeatherForecastService.svc?xsd=xsd4" namespace="http://www.restfulwebservices.net/DataContracts/2008/01"/>
<xs:element name="GetCitiesByCountry">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="Country" nillable="true" type="xs:string"/>    
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetCitiesByCountryResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="GetCitiesByCountryResult" nillable="true" type="q1:ArrayOfstring"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetForecastByCity">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="City" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="Country" nillable="true" type="xs:string"/>   
</xs:sequence>
</xs:complexType>
</xs:element> 
<xs:element name="GetForecastByCityResponse">
<xs:complexType><xs:sequence><xs:element minOccurs="0" name="GetForecastByCityResult" nillable="true" type="q2:Weather"/>
</xs:sequence>
</xs:complexType> 
</xs:element>
</xs:schema> 

XSD FILE1

<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/">
<xs:element name="anyType" nillable="true" type="xs:anyType"/>
<xs:element name="anyURI" nillable="true" type="xs:anyURI"/>
<xs:element name="base64Binary" nillable="true" type="xs:base64Binary"/>
<xs:element name="boolean" nillable="true" type="xs:boolean"/>
<xs:element name="byte" nillable="true" type="xs:byte"/>
<xs:element name="dateTime" nillable="true" type="xs:dateTime"/>
<xs:element name="decimal" nillable="true" type="xs:decimal"/>
<xs:element name="double" nillable="true" type="xs:double"/>
<xs:element name="float" nillable="true" type="xs:float"/>
<xs:element name="int" nillable="true" type="xs:int"/>
<xs:element name="long" nillable="true" type="xs:long"/>
<xs:element name="QName" nillable="true" type="xs:QName"/>
<xs:element name="short" nillable="true" type="xs:short"/>
<xs:element name="string" nillable="true" type="xs:string"/>
<xs:element name="unsignedByte" nillable="true" type="xs:unsignedByte"/>
<xs:element name="unsignedInt" nillable="true" type="xs:unsignedInt"/>
<xs:element name="unsignedLong" nillable="true" type="xs:unsignedLong"/>
<xs:element name="unsignedShort" nillable="true" type="xs:unsignedShort"/>
<xs:element name="char" nillable="true" type="tns:char"/>
<xs:simpleType name="char">
<xs:restriction base="xs:int"/></xs:simpleType>
<xs:element name="duration" nillable="true" type="tns:duration"/>
<xs:simpleType name="duration">
<xs:restriction base="xs:duration">
<xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?"/><xs:minInclusive value="-P10675199DT2H48M5.4775808S"/>
<xs:maxInclusive value="P10675199DT2H48M5.4775807S"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="guid" nillable="true" type="tns:guid"/>
<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}"/>
</xs:restriction>
</xs:simpleType>
<xs:attribute name="FactoryType" type="xs:QName"/>
<xs:attribute name="Id" type="xs:ID"/>
<xs:attribute name="Ref" type="xs:IDREF"/>
</xs:schema>

XSD FILE2

 <xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays">   
 <xs:complexType name="ArrayOfstring">
 <xs:sequence>
 <xs:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="xs:string"/>
 </xs:sequence>
 </xs:complexType><xs:element name="ArrayOfstring" nillable="true" type="tns:ArrayOfstring"/>
 </xs:schema>

XSD FILE3

<xs:schema elementFormDefault="qualified" targetNamespace="http://GOTLServices.FaultContracts/2008/01">
<xs:import schemaLocation="http://www.restfulwebservices.net/wcf/WeatherForecastService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xs:complexType name="DefaultFaultContract">
<xs:sequence>
<xs:element name="ErrorId" type="xs:int"/>
<xs:element name="ErrorMessage" nillable="true" type="xs:string"/>
<xs:element name="CorrelationId" type="ser:guid"/>
</xs:sequence>
</xs:complexType>
<xs:element name="DefaultFaultContract" nillable="true" type="tns:DefaultFaultContract"/>
</xs:schema>

Solution

  • Some JAXB versions doesn't support same element name and type name to be in XSD.

    you should have two different names for element and the type.

         <xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays">   
     <xs:complexType name="ArrayOfstring">
     <xs:sequence>
     <xs:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="xs:string"/>
     </xs:sequence>
     </xs:complexType><xs:element name="ArrayOfstring" nillable="true" type="tns:ArrayOfstring"/>
     </xs:schema>
    

    In above XSD both Complex type name and element type name is "ArrayOfstring". It may cause the above exception. Please try to generate web service client by changing XSD as follows.

        <xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays">   
     <xs:complexType name="ArrayOfstringtype">
     <xs:sequence>
     <xs:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="xs:string"/>
     </xs:sequence>
     </xs:complexType><xs:element name="ArrayOfstring" nillable="true" type="tns:ArrayOfstringtype"/>
     </xs:schema>
    

    Need to Change all the other XSD files too.