有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JAXWS wsimport不会保留toString()?

为什么wsimport不保留我的模型类的toString()方法? 因此,我的web服务中有一个模型类:

package webservice;
public class Book {
    private String title;
    private int pages;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getPages() {
        return pages;
    }
    public void setPages(int pages) {
        this.pages = pages;
    }
    @Override
    public String toString() {
        return "Book [title=" + title + ", pages=" + pages + "]";
    }
}

现在,我想为WS-client生成必要的构件:

wsimport -keep http://localhost:8080/LibraryWs/BookWebServiceService?wsdl

并获得结果类:

package webservice;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

/**
 * <p>Java class for book complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="book">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="pages" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *         &lt;element name="title" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "book", propOrder = {
    "pages",
    "title"
})
public class Book {

    protected int pages;
    protected String title;

    /**
     * Gets the value of the pages property.
     * 
     */
    public int getPages() {
        return pages;
    }

    /**
     * Sets the value of the pages property.
     * 
     */
    public void setPages(int value) {
        this.pages = value;
    }

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

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

因此没有toString:)我在哪里可以找到关于此行为的规范,或者如何让jax ws在我的WSDL中显式地包含该方法?谢谢


共 (1) 个答案

  1. # 1 楼答案

    这实际上是一个愚蠢的问题,因为在WSDL中,只转换类成员变量,而不转换方法。它们被编码为complexType。 我想说,如果您想要公开一个方法,它必须进入@WebService并(可选地)用@WebMethod注释。否则,只有Web服务使用的类的属性才会进入WSDL。 所以这里http://localhost:8080/LibraryWs/BookWebServiceService?xsd=1我们将有:

    <?xml version='1.0' encoding='UTF-8'?><!  Published by JAX-WS RI (http://jax-ws.java.net). RI's version is Metro/2.3.1-b419 (branches/2.3.1.x-7937; 2014-08-04T08:11:03+0000) JAXWS-RI/2.2.10-b140803.1500 JAXWS-API/2.2.11 JAXB-RI/2.2.10-b140802.1033 JAXB-API/2.2.12-b140109.1041 svn-revision#unknown.  ><xs:schema xmlns:tns="http://webservice/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://webservice/">
    
    <xs:element name="addBook" type="tns:addBook"/>
    <xs:element name="addBookResponse" type="tns:addBookResponse"/>
    <xs:element name="getAllBooks" type="tns:getAllBooks"/>
    <xs:element name="getAllBooksResponse" type="tns:getAllBooksResponse"/>
    <xs:complexType name="getAllBooks">
    <xs:sequence/>
    </xs:complexType>
    
    <xs:complexType name="getAllBooksResponse">
    <xs:sequence>
    <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    </xs:complexType>
    
    <xs:complexType name="addBook">
    <xs:sequence>
    <xs:element name="arg0" type="tns:book" minOccurs="0"/>
    </xs:sequence>
    </xs:complexType>
    
    <xs:complexType name="book">
    <xs:sequence>
    <xs:element name="pages" type="xs:int"/>
    <xs:element name="title" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    </xs:complexType>
    
    <xs:complexType name="addBookResponse">
    <xs:sequence/>
    </xs:complexType>
    </xs:schema>