如何在java代码中使用WSDL文件中的用户定义数据类型?

如何在java代码中使用WSDL文件中的用户定义数据类型?,java,web-services,wsdl,Java,Web Services,Wsdl,我正在尝试使用wsimport工具使用远程web服务。它生成必要的客户端代码。因为我发现该方法的返回类型是用户使用xml复杂类型定义的。如何在java代码中使用这种复杂类型。下面是我尝试的代码 import net.restfulwebservices.servicecontracts._2008._01.WeatherForecastService; import net.restfulwebservices.servicecontracts._2008._01.IWeatherForecas

我正在尝试使用wsimport工具使用远程web服务。它生成必要的客户端代码。因为我发现该方法的返回类型是用户使用xml复杂类型定义的。如何在java代码中使用这种复杂类型。下面是我尝试的代码

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();
        }
    }
}
在此ArrayOfstring中,是用户定义的类型。我找到一个名为ArrayOfString的文件,其中包含以下代码

    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;
    }
* * * *列表中允许以下类型的对象 *{@link String} * * */ 公共列表getString(){ if(字符串==null){ string=newarraylist(); } 返回此.string; } 当我执行这段代码时,我得到了以下异常

<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> 
javax.xml.ws.WebServiceException
位于com.sun.xml.internal.ws.model.RuntimeModel.createJAXBContext(未知
(来源)
位于com.sun.xml.internal.ws.model.RuntimeModel.postProcess(未知源
)
位于com.sun.xml.internal.ws.modeler.RuntimeModeler.buildRuntimeModel(Unkn
(自有来源)
在com.sun.xml.internal.ws.client.ServiceContextBuilder.processAnnotatio上
ns(未知源)
位于com.sun.xml.internal.ws.client.ServiceContextBuilder.completeServiceC
ontext(未知源)
位于com.sun.xml.internal.ws.client.WSServiceDelegate.processServiceContex
t(未知来源)
位于com.sun.xml.internal.ws.client.WSServiceDelegate.createEndpointIFBase
代理(未知源)
在com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(未知)上
(行政长官)
位于javax.xml.ws.Service.getPort(未知源)
在net.restfulwebservices.servicecontracts.\u 2008.\u 01.WeatherForecastServ
getBasicHttpBindingIWeatherForecastService(WeatherForecastService.java:50)
位于WeatherForecastClient.main(WeatherForecastClient.java:12)
原因:com.sun.xml.internal.bind.v2.runtime.illegalannotations异常:5 c
非法声明例外数量
元素没有带有@XmlElementDecl的ObjectFactory{http://www.res
tfulwebservices.net/ServiceContracts/2008/01}国家/地区。
此问题与以下位置有关:
位于受保护的javax.xml.bind.JAXBElement net.restfulwebservices.s
服务合同。_2008。_01.GetCitiesByCountry.country
在net.restfulwebservices.servicecontracts.\u 2008.\u 01.GetCitiesBy
国
元素没有带有@XmlElementDecl的ObjectFactory{http://www.res
tfulwebservices.net/ServiceContracts/2008/01}GetCitiesByCountryResult。
此问题与以下位置有关:
位于受保护的javax.xml.bind.JAXBElement net.restfulwebservices.s
服务合同。_2008。_01.GetCitiesByCountryResponse.getCitiesByCountryResult
在net.restfulwebservices.servicecontracts.\u 2008.\u 01.GetCitiesBy
国家回应
元素没有带有@XmlElementDecl的ObjectFactory{http://www.res
tfulwebservices.net/ServiceContracts/2008/01}城市。
此问题与以下位置有关:
位于受保护的javax.xml.bind.JAXBElement net.restfulwebservices.s
服务合同。_2008。_01.GetForecastByCity.city
在net.restfulwebservices.servicecontracts.\u 2008.\u 01.GetForecast
拜城
元素没有带有@XmlElementDecl的ObjectFactory{http://www.res
tfulwebservices.net/ServiceContracts/2008/01}国家/地区。
此问题与以下位置有关:
位于受保护的javax.xml.bind.JAXBElement net.restfulwebservices.s
服务合同。_2008。_01.GetForecastByCity.country
在net.restfulwebservices.servicecontracts.\u 2008.\u 01.GetForecast
拜城
元素没有带有@XmlElementDecl的ObjectFactory{http://www.res
tfulwebservices.net/ServiceContracts/2008/01}GetForecastByCityResult。
此问题与以下位置有关:
位于受保护的javax.xml.bind.JAXBElement net.restfulwebservices.s
服务合同。_2008。_01.GetForecastByCityResponse.getForecastByCityResult
在net.restfulwebservices.servicecontracts.\u 2008.\u 01.GetForecast
副城市响应
位于com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Buil
订单检查(未知来源)
位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(U
(不知道来源)
位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl。(未知S)
(来源)
位于com.sun.xml.internal.bind.v2.ContextFactory.createContext(未知的Sou
(rce)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源)
位于java.lang.reflect.Method.invoke(未知源)
位于com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(未知源代码
(e)
位于com.sun.xml.internal.ws.model.RuntimeModel$1.run(未知源)
位于java.security.AccessController.doPrivileged(本机方法)
... 还有11个
任何人都可以给出发生此异常的任何建议。提前感谢

WSDL文件

<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文件0

 <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文件1

<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>

XSD文件2

     <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文件3

    <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>

一些JAXB版本不支持XSD中的相同元素名和类型名

元素和类型应有两个不同的名称


在上面的XSD中,两者都是复杂的
     <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>
    <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>