JAX-WS将bean方法传递给客户机



我用Netbeans做了一个web服务。我试图理解为什么bean的方法不是由客户端生成的。

这是豆子。

package OnlineAuction;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

public class Auction implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private Product product;
    private Timestamp added;
    private Timestamp expire;
    private BigDecimal startingBid;
    private BigDecimal reserve;
    /**
     *
     * @return
     */
    public Long getId() {
        return id;
    }
    /**
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Auction)) {
            return false;
        }
        Auction other = (Auction) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
    @Override
    public String toString() {
        return "OnlineAuction.Auction[ id=" + id + " ]";
    }
    /**
     *
     * @return
     */
    public Product getProduct() {
        return product;
    }
    /**
     *
     * @param product
     */
    public void setProduct(Product product) {
        this.product = product;
    }
    /**
     *
     * @return
     */
    public Timestamp getAdded() {
        return added;
    }
    /**
     *
     * @param added
     */
    public void setAdded(Timestamp added) {
        this.added = added;
    }
    /**
     *
     */
    public void setAddedNow() {
        this.added = getTimestampNow();
    }
    private Timestamp getTimestampNow() {
        java.util.Date date = new java.util.Date();
        return new Timestamp(date.getTime());
    }
    /**
     *
     * @return
     */
    public Timestamp getExpire() {
        return expire;
    }
    /**
     *
     * @param expire
     */
    public void setExpire(Timestamp expire) {
        this.expire = expire;
    }
    /**
     * Sets the expiry time of the auction by adding the hours to the added date.
     * If the added date is not set this function will set the added date to current time.
     * @param hours to be added to the added time of the auction
     */
    public void setExpire(int hours) {
        // Set added time if null
        if (this.added == null) {
            this.setAddedNow();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(this.added);
        cal.add(Calendar.HOUR, hours);
        Date date = cal.getTime();
        this.expire = new Timestamp(date.getTime());
    }
    /**
     *
     * @return
     */
    public BigDecimal getStartingBid() {
        return startingBid;
    }
    /**
     *
     * @param startingBid
     */
    public void setStartingBid(BigDecimal startingBid) {
        this.startingBid = startingBid;
    }
    /**
     *
     * @return
     */
    public BigDecimal getReserve() {
        return reserve;
    }
    /**
     *
     * @param reserve
     */
    public void setReserve(BigDecimal reserve) {
        this.reserve = reserve;
    }

    public boolean isAuctionExpired() {
        boolean expired;
        if (this.getTimestampNow().compareTo(this.expire) > 0) {
            // now is greater than expire date
            expired = true;
        } else {
            // now is less than expire date
            expired = false;
        }
        return expired;
    }
}

下面是JAX-WS为客户端生成的源代码。

package OnlineAuction.client;
import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

/**
 * <p>Java class for auction complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="auction">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="added" type="{http://OnlineAuction/}timestamp" minOccurs="0"/>
 *         &lt;element name="expire" type="{http://OnlineAuction/}timestamp" minOccurs="0"/>
 *         &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}long" minOccurs="0"/>
 *         &lt;element name="product" type="{http://OnlineAuction/}product" minOccurs="0"/>
 *         &lt;element name="reserve" type="{http://www.w3.org/2001/XMLSchema}decimal" minOccurs="0"/>
 *         &lt;element name="startingBid" type="{http://www.w3.org/2001/XMLSchema}decimal" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "auction", propOrder = {
    "added",
    "expire",
    "id",
    "product",
    "reserve",
    "startingBid"
})
public class Auction {
    protected Timestamp added;
    protected Timestamp expire;
    protected Long id;
    protected Product product;
    protected BigDecimal reserve;
    protected BigDecimal startingBid;
    /**
     * Gets the value of the added property.
     * 
     * @return
     *     possible object is
     *     {@link Timestamp }
     *     
     */
    public Timestamp getAdded() {
        return added;
    }
    /**
     * Sets the value of the added property.
     * 
     * @param value
     *     allowed object is
     *     {@link Timestamp }
     *     
     */
    public void setAdded(Timestamp value) {
        this.added = value;
    }
    /**
     * Gets the value of the expire property.
     * 
     * @return
     *     possible object is
     *     {@link Timestamp }
     *     
     */
    public Timestamp getExpire() {
        return expire;
    }
    /**
     * Sets the value of the expire property.
     * 
     * @param value
     *     allowed object is
     *     {@link Timestamp }
     *     
     */
    public void setExpire(Timestamp value) {
        this.expire = value;
    }
    /**
     * Gets the value of the id property.
     * 
     * @return
     *     possible object is
     *     {@link Long }
     *     
     */
    public Long getId() {
        return id;
    }
    /**
     * Sets the value of the id property.
     * 
     * @param value
     *     allowed object is
     *     {@link Long }
     *     
     */
    public void setId(Long value) {
        this.id = value;
    }
    /**
     * Gets the value of the product property.
     * 
     * @return
     *     possible object is
     *     {@link Product }
     *     
     */
    public Product getProduct() {
        return product;
    }
    /**
     * Sets the value of the product property.
     * 
     * @param value
     *     allowed object is
     *     {@link Product }
     *     
     */
    public void setProduct(Product value) {
        this.product = value;
    }
    /**
     * Gets the value of the reserve property.
     * 
     * @return
     *     possible object is
     *     {@link BigDecimal }
     *     
     */
    public BigDecimal getReserve() {
        return reserve;
    }
    /**
     * Sets the value of the reserve property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigDecimal }
     *     
     */
    public void setReserve(BigDecimal value) {
        this.reserve = value;
    }
    /**
     * Gets the value of the startingBid property.
     * 
     * @return
     *     possible object is
     *     {@link BigDecimal }
     *     
     */
    public BigDecimal getStartingBid() {
        return startingBid;
    }
    /**
     * Sets the value of the startingBid property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigDecimal }
     *     
     */
    public void setStartingBid(BigDecimal value) {
        this.startingBid = value;
    }
}

那么为什么我的bean方法isAuctionExpired()不在生成的源中?我可以通过添加auctionExpired属性来欺骗这一点,但这不是我想要的方式。

JAX-WS客户机是使用web服务的WSDL生成的。WSDL只具有将用于服务器和客户机之间通信的类型的属性。因此,您将永远无法在服务器和客户机之间传递方法实现。下面注释中的信息是客户机类生成器在生成客户机时所拥有的全部信息;

@XmlType(name = "auction", propOrder = {
    "added",
    "expire",
    "id",
    "product",
    "reserve",
    "startingBid"
})

该信息取自WSDL。
您必须理解XML用于服务器和客户机之间的通信。因此,来自web服务的代码不能传递给客户端,反之亦然。web服务和客户端可以用两种不同的技术编写。因此,如果客户机是用任何其他语言实现的,那么将java代码传递给客户机就没有任何意义。简而言之,您可以在客户端和服务器之间传递信息,但不能传递行为。

相关内容

  • 没有找到相关文章

最新更新