【问题标题】:How to change xml element names of complex types for custom classes in java webservice如何在java webservice中更改自定义类的复杂类型的xml元素名称
【发布时间】:2016-02-06 08:01:23
【问题描述】:

我使用 eclipse 创建了一个简单的 java webserivce 来解释我的问题。

产品类别:

public class Product {

    public int productID;
    public String productName;

}

行业分类:

public class Industry{


        public int industryID;

        public String industryName;

        Product[] products;


        public Product[] getProducts()
        {
            return this.products;
        }
        public void setProducts(Product[] arr)
        {

            this.products = arr;
        }

}

我创建了一个 web 服务类,它将返回一个带有 Product 类的产品数组的行业。

public class IndustryService  {
    /**
     * @param industryID
     * @return industry object
     */



    public Industry getIndustryData(int industryID){

        Product product1 = new Product();
        product1.productID = 712;
        product1.productName = "Sensor Light";

        Product product2 = new Product();
        product2.productID = 1774;
        product2.productName = "Light Beamer";

        Product [] products = new Product[] { product1, product2 };



        Industry industry = new Industry();
        industry.industryID = 2311;
        industry.industryName = "Test";
        industry.setProducts(products);

        return industry;
    }

}

Web 服务正常工作,我可以生成 WSDL:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://server.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://server.com" xmlns:intf="http://server.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://server.com" xmlns="http://www.w3.org/2001/XMLSchema">
   <element name="getIndustryData">
    <complexType>
     <sequence>
      <element name="industryID" type="xsd:int"/>
     </sequence>
    </complexType>
   </element>
   <element name="getIndustryDataResponse">
    <complexType>
     <sequence>
      <element name="getIndustryDataReturn" type="impl:Industry"/>
     </sequence>
    </complexType>
   </element>
   <complexType name="Product">
    <sequence>
     <element name="productID" type="xsd:int"/>
     <element name="productName" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
   <complexType name="ArrayOfProduct">
    <sequence>
     <element maxOccurs="unbounded" minOccurs="0" name="item" type="impl:Product"/>
    </sequence>
   </complexType>
   <complexType name="Industry">
    <sequence>
     <element name="products" nillable="true" type="impl:ArrayOfProduct"/>
     <element name="industryID" type="xsd:int"/>
     <element name="industryName" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
  </schema>
 </wsdl:types>

   <wsdl:message name="getIndustryDataResponse">

      <wsdl:part element="impl:getIndustryDataResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="getIndustryDataRequest">

      <wsdl:part element="impl:getIndustryData" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:portType name="IndustryService">

      <wsdl:operation name="getIndustryData">

         <wsdl:input message="impl:getIndustryDataRequest" name="getIndustryDataRequest">

       </wsdl:input>

         <wsdl:output message="impl:getIndustryDataResponse" name="getIndustryDataResponse">

       </wsdl:output>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="IndustryServiceSoapBinding" type="impl:IndustryService">

      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

      <wsdl:operation name="getIndustryData">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="getIndustryDataRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="getIndustryDataResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="IndustryServiceService">

      <wsdl:port binding="impl:IndustryServiceSoapBinding" name="IndustryService">

         <wsdlsoap:address location="http://localhost:8080/IIIII/services/IndustryService"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>

如何在我的 java 代码中更改元素名称?和复杂的类型名称? 请看下文。

“ArrayOfProducts”和“item”标签由 wsdl 生成器生成。如何更改这些名称?

【问题讨论】:

    标签: java xml eclipse web-services wsdl


    【解决方案1】:

    我建议使用 java 的 JAXB 命令 xjc 从 XSD 文件生成 java 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多