webservice 数据绑定与映射

来源:互联网 发布:java向移动推送原理图 编辑:程序博客网 时间:2024/05/16 13:40

        webservice 中将java转换为 XML  Schema(有别于基本类型的复杂类型定义)的过程称序列化,反之为反序列化。数据绑定和Java/XML是实现这个过程的的两种机制

 

 

一.JAXB数据绑定

  1.如下XML Schema  可以通过  模式编译器 生成 java 类

 

 

1)  命名空间 http://www.example.com/oms  对于java  类中的包 com.example.oms 

2) 元素  billTo 对于java  类中 billTo  属性。

3) items  对应一个 Items   类对象  Items 类中有一个  List属性item  这个 list中装的都是  ItemType对象 

 

 

2. 定义JAXB 值类。通过   模式生成器  将其转换为 XML 模式

 

 

 

/*
* Copyright 2006 Javector Software LLC
*
* Licensed under the GNU General Public License, Version 2 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.gnu.org/copyleft/gpl.html
*
* THE SOURCE CODE AND ACCOMPANYING FILES ARE PROVIDED WITHOUT ANY WARRANTY,
* WRITTEN OR IMPLIED.
*
* The copyright holder provides this software under other licenses for those
* wishing to include it with products or systems not licensed under the GPL.
* Contact licenses@javector.com for more information.
*/
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-20051220-ea3
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2006.01.05 at 10:14:08 PM EST
//


package com.example.oms;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.AccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import com.example.oms.ItemType;
import com.example.oms.SimpleOrder;
import com.example.oms.SimpleOrder.BillTo;
import com.example.oms.SimpleOrder.Items;


/**
 * <p>Java class for simpleOrder element declaration.
 *
 * <p>The following schema fragment specifies the expected content contained within this class.
 *
 * <pre>
 * &lt;element name="simpleOrder">
 *   &lt;complexType>
 *     &lt;complexContent>
 *       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *         &lt;sequence>
 *           &lt;element name="billTo">
 *             &lt;complexType>
 *               &lt;complexContent>
 *                 &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                   &lt;sequence>
 *                     &lt;element name="street" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                     &lt;element name="city" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                     &lt;element name="state" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                     &lt;element name="zip" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                     &lt;element name="phone" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                   &lt;/sequence>
 *                 &lt;/restriction>
 *               &lt;/complexContent>
 *             &lt;/complexType>
 *           &lt;/element>
 *           &lt;element name="items">
 *             &lt;complexType>
 *               &lt;complexContent>
 *                 &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                   &lt;sequence>
 *                     &lt;element name="item" type="{http://www.example.com/oms}ItemType" maxOccurs="unbounded"/>
 *                   &lt;/sequence>
 *                 &lt;/restriction>
 *               &lt;/complexContent>
 *             &lt;/complexType>
 *           &lt;/element>
 *         &lt;/sequence>
 *       &lt;/restriction>
 *     &lt;/complexContent>
 *   &lt;/complexType>
 * &lt;/element>
 * </pre>
 *
 *
 */
//! <example xn="JAXBSimpleOrderRoot">
//! <c>chap05</c><s>customjava</s>
@XmlAccessorType(AccessType.FIELD)
@XmlType(name = "", propOrder = {
    "billTo",
    "items"
})
@XmlRootElement(name = "simpleOrder")
public class SimpleOrder {

    @XmlElement(namespace = "http://www.example.com/oms")
    protected BillTo billTo;
    @XmlElement(namespace = "http://www.example.com/oms")
    protected Items items;
//! </example>

    /**
     * Gets the value of the billTo property.
     *
     * @return
     *     possible object is
     *     {@link BillTo }
     *    
     */
    public BillTo getBillTo() {
        return billTo;
    }

    /**
     * Sets the value of the billTo property.
     *
     * @param value
     *     allowed object is
     *     {@link BillTo }
     *    
     */
    public void setBillTo(BillTo value) {
        this.billTo = value;
    }

    /**
     * Gets the value of the items property.
     *
     * @return
     *     possible object is
     *     {@link Items }
     *    
     */
    public Items getItems() {
        return items;
    }

    /**
     * Sets the value of the items property.
     *
     * @param value
     *     allowed object is
     *     {@link Items }
     *    
     */
    public void setItems(Items value) {
        this.items = value;
    }


    /**
     * <p>Java class for anonymous complex type.
     *
     * <p>The following schema fragment specifies the expected content contained within this class.
     *
     * <pre>
     * &lt;complexType>
     *   &lt;complexContent>
     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       &lt;sequence>
     *         &lt;element name="street" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *         &lt;element name="city" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *         &lt;element name="state" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *         &lt;element name="zip" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *         &lt;element name="phone" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *       &lt;/sequence>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     *
     *
     */
//! <example xn="JAXBSimpleOrder_BillTo">
//! <c>chap05</c><s>customjava</s>
    @XmlAccessorType(AccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "name"
        "street",
        "city",
        "state",
        "zip",
        "phone"
    })
    public static class BillTo {

      @XmlElement(namespace = "http://www.example.com/oms", required = true)
      protected String name;
      @XmlElement(namespace = "http://www.example.com/oms")
      protected String street;
      @XmlElement(namespace = "http://www.example.com/oms")
      protected String city;
      @XmlElement(namespace = "http://www.example.com/oms")
      protected String state;
      @XmlElement(namespace = "http://www.example.com/oms")
      protected String zip;
      @XmlElement(namespace = "http://www.example.com/oms")
      protected String phone;
//! </example>
        /**
         * Gets the value of the street property.
         *
         * @return
         *     possible object is
         *     {@link String }
         *    
         */
        public String getStreet() {
            return street;
        }

        /**
         * Sets the value of the street property.
         *
         * @param value
         *     allowed object is
         *     {@link String }
         *    
         */
        public void setStreet(String value) {
            this.street = value;
        }

        /**
         * Gets the value of the city property.
         *
         * @return
         *     possible object is
         *     {@link String }
         *    
         */
        public String getCity() {
            return city;
        }

        /**
         * Sets the value of the city property.
         *
         * @param value
         *     allowed object is
         *     {@link String }
         *    
         */
        public void setCity(String value) {
            this.city = value;
        }

        /**
         * Gets the value of the state property.
         *
         * @return
         *     possible object is
         *     {@link String }
         *    
         */
        public String getState() {
            return state;
        }

        /**
         * Sets the value of the state property.
         *
         * @param value
         *     allowed object is
         *     {@link String }
         *    
         */
        public void setState(String value) {
            this.state = value;
        }

        /**
         * Gets the value of the zip property.
         *
         * @return
         *     possible object is
         *     {@link String }
         *    
         */
        public String getZip() {
            return zip;
        }

        /**
         * Sets the value of the zip property.
         *
         * @param value
         *     allowed object is
         *     {@link String }
         *    
         */
        public void setZip(String value) {
            this.zip = value;
        }

        /**
         * Gets the value of the phone property.
         *
         * @return
         *     possible object is
         *     {@link String }
         *    
         */
        public String getPhone() {
            return phone;
        }

        /**
         * Sets the value of the phone property.
         *
         * @param value
         *     allowed object is
         *     {@link String }
         *    
         */
        public void setPhone(String value) {
            this.phone = value;
        }

    }


    /**
     * <p>Java class for anonymous complex type.
     *
     * <p>The following schema fragment specifies the expected content contained within this class.
     *
     * <pre>
     * &lt;complexType>
     *   &lt;complexContent>
     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       &lt;sequence>
     *         &lt;element name="item" type="{http://www.example.com/oms}ItemType" maxOccurs="unbounded"/>
     *       &lt;/sequence>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     *
     *
     */
//! <example xn="JAXBSimpleOrder_Items">
//! <c>chap05</c><s>customjava</s>   
    @XmlAccessorType(AccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "item"
    })
    public static class Items {
     
      @XmlElement(namespace = "http://www.example.com/oms")
      protected List<ItemType> item;
     
      /**
       * Gets the value of the item 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 item property.
       *
       * <p>
       * For example, to add a new item, do as follows:
       * <pre>
       *    getItem().add(newItem);
       * </pre>
       *
       *
       * <p>
       * Objects of the following type(s) are allowed in the list
       * {@link ItemType }
       *
       *
       */
      public List<ItemType> getItem() {
        if (item == null) {
          item = new ArrayList<ItemType>();
        }
        return this.item;
      }
     
    }
//! </example>
}

 

1)

 

  

   

原创粉丝点击