Web-Service返回一个我无法使用JAVA的对象

时间:2014-03-12 12:59:41

标签: java web-services soap

我正在创建一个Web服务的客户端 - " www.webservicex.net/uszip.asmx"返回所有城市,并发布该州的邮政编码(我将使用该信息进行天气广播节目)。

ipSoap.getInfoByState()返回一个对象GetInfoByStateResult(gibs fron now on)。现在的问题是我无法访问gibs所拥有的内容。 getContent()方法返回一个对象列表但它没有允许我获取所需数据的方法(名称和拉链)。

如何从响应中获取城市名称和拉链?

这是我的代码:

public class Main {

    public static void main(String[] args){
        USZip ip = new USZip();
        USZipSoap ipSoap = ip.getUSZipSoap();
        GetInfoByStateResult gibs = ipSoap.getInfoByState("NJ"); //New York
        List<Object> listOfCities = gibs.getContent();
               /* problem here, getContent is always size 1, and i can't see what's inside in it. gibs.getIndex(0).toString returns: [NewDataSet: null].
*/

        }
}

GetCityByResult位于底部

/**
 * <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="GetInfoByCityResult" minOccurs="0">
 *           &lt;complexType>
 *             &lt;complexContent>
 *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                 &lt;sequence>
 *                   &lt;any/>
 *                 &lt;/sequence>
 *               &lt;/restriction>
 *             &lt;/complexContent>
 *           &lt;/complexType>
 *         &lt;/element>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "getInfoByCityResult"
})
@XmlRootElement(name = "GetInfoByCityResponse")
public class GetInfoByCityResponse {

    @XmlElement(name = "GetInfoByCityResult")
    protected GetInfoByCityResponse.GetInfoByCityResult getInfoByCityResult;

    /**
     * Gets the value of the getInfoByCityResult property.
     * 
     * @return
     *     possible object is
     *     {@link GetInfoByCityResponse.GetInfoByCityResult }
     *     
     */
    public GetInfoByCityResponse.GetInfoByCityResult getGetInfoByCityResult() {
        return getInfoByCityResult;
    }

    /**
     * Sets the value of the getInfoByCityResult property.
     * 
     * @param value
     *     allowed object is
     *     {@link GetInfoByCityResponse.GetInfoByCityResult }
     *     
     */
    public void setGetInfoByCityResult(GetInfoByCityResponse.GetInfoByCityResult value) {
        this.getInfoByCityResult = 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;any/>
     *       &lt;/sequence>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "content"
    })
    public static class GetInfoByCityResult {

        @XmlMixed
        @XmlAnyElement(lax = true)
        protected List<Object> content;

        /**
         * Gets the value of the content 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 content property.
         * 
         * <p>
         * For example, to add a new item, do as follows:
         * <pre>
         *    getContent().add(newItem);
         * </pre>
         * 
         * 
         * <p>
         * Objects of the following type(s) are allowed in the list
         * {@link String }
         * {@link Object }
         * 
         * 
         */
        public List<Object> getContent() {
            if (content == null) {
                content = new ArrayList<Object>();
            }
            return this.content;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

Web服务似乎返回非常通用的XML,如架构中的any元素所示。

根据JAXB documentation

  

JAXB将任何此类元素绑定到Object,并在解组期间,   遇到的所有元素都被解组到相应的JAXB中   对象(如有必要,包括JAXBElements)并置于此处   领域。如果它遇到无法解组的元素,DOM   而是生产元素。

这意味着如果GetInfoByCityResult包含符合您为其生成Java类的模式的元素,则将返回这些类的实例;否则将返回原始DOM元素。

恕我直言,服务提供商使用这样的通用模式有点松懈 - 为了使用XML,除了通用的元素显示之外,你需要能够将意义附加到那些元素上,如果他们没有在模式中定义是不可能的。

在您的情况下,这意味着您需要调试代码并查看Web服务返回的元素,然后编写这些元素的代码(希望它们不会更改)。