webservice调用天气服务

常见服务:http://www.webxml.com.cn/zh_cn/support.aspx

可能用到缺少的ArrayOfString.java文件

package com.test.wes.weather;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>ArrayOfString complex type的 Java 类。
 * 
 * <p>以下模式片段指定包含在此类中的预期内容。
 * 
 * <pre>
 * &lt;complexType name="ArrayOfString">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="string" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArrayOfString", propOrder = {
    "string"
})
public class ArrayOfString {

    @XmlElement(nillable = true)
    protected List<String> string;

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

}
View Code

wsimport用法

用法: wsimport [options] <WSDL_URI>

其中 [options] 包括:
  -b <path>                 指定 jaxws/jaxb 绑定文件或附加模式
                            (每个 <path> 都必须具有自己的 -b)
  -B<jaxbOption>            将此选项传递给 JAXB 模式编译器
  -catalog <file>           指定用于解析外部实体引用的目录文件
                            支持 TR9401, XCatalog 和 OASIS XML 目录格式。
  -d <directory>            指定放置生成的输出文件的位置
  -encoding <encoding>      指定源文件所使用的字符编码
  -extension                允许供应商扩展 - 不按规范
                            指定功能。使用扩展可能会
                            导致应用程序不可移植或
                            无法与其他实现进行互操作
  -help                     显示帮助
  -httpproxy:<host>:<port>  指定 HTTP 代理服务器 (端口默认为 8080)
  -keep                     保留生成的文件
  -p <pkg>                  指定目标程序包
  -quiet                    隐藏 wsimport 输出
  -s <directory>            指定放置生成的源文件的位置
  -target <version>         按给定的 JAXWS 规范版本生成代码
                            默认为 2.2, 接受的值为 2.0, 2.1 和 2.2
                            例如, 2.0 将为 JAXWS 2.0 规范生成兼容的代码
  -verbose                  有关编译器在执行什么操作的输出消息
  -version                  输出版本信息
  -wsdllocation <location>  @WebServiceClient.wsdlLocation 值
  -clientjar <jarfile>      创建生成的 Artifact 的 jar 文件以及
                            调用 Web 服务所需的 WSDL 元数据。
  -generateJWS              生成存根 JWS 实现文件
  -implDestDir <directory>  指定生成 JWS 实现文件的位置
  -implServiceName <name>   生成的 JWS 实现的服务名的本地部分
  -implPortName <name>      生成的 JWS 实现的端口名的本地部分

扩展:
  -XadditionalHeaders              映射标头不绑定到请求或响应消息不绑定到
                                   Java 方法参数
  -Xauthfile                       用于传送以下格式的授权信息的文件:
                                   http://username:password@example.org/stock?wsdl
  -Xdebug                          输出调试信息
  -Xno-addressing-databinding      允许 W3C EndpointReferenceType 到 Java 的绑定
  -Xnocompile                      不编译生成的 Java 文件
  -XdisableAuthenticator           禁用由 JAX-WS RI 使用的验证程序,
                                   将忽略 -Xauthfile 选项 (如果设置)
  -XdisableSSLHostnameVerification 在提取 wsdl 时禁用 SSL 主机名
                                   验证

示例:
  wsimport stock.wsdl -b stock.xml -b stock.xjb
  wsimport -d generated http://example.org/stock?wsdl

平时的用法

wsimport -encoding utf-8 -p com.test.wes.weather -s . http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

先把文件下载下来WeatherWS.asmx

 再WeatherWS.asmx里

 <s:element ref="s:schema"/><s:any/>全部替换为​ <s:any minOccurs="2" maxOccurs="2"/>


wsimport -encoding utf-8 -p com.test.wes.weather -s . WeatherWS.asmx

运行上面的句子,生成了下面代码

 


package com.test.wec;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.test.wes.phone.MobileCodeWSSoap;
import com.test.wes.weather.ArrayOfString;
import com.test.wes.weather.WeatherWS;
import com.test.wes.weather.WeatherWSSoap;
 
 

public class ServiceClient {
 
    public static void main(String[] args) throws IOException {
        //创建WSDL的URL,注意不是服务地址
//        URL url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");
//        
//        //创建服务名称
//        //1.namespaceURI - 命名空间地址
//        //2.localPart - 服务视图名
//        QName qname = new QName("http://WebXml.com.cn/", "qqOnlineWebService");
//        
//        //创建服务视图
//        //参数解释:
//        //1.wsdlDocumentLocation - wsdl地址
//        //2.serviceName - 服务名称
//        Service service = Service.create(url, qname);
//        //获取服务实现类
////        QqOnlineWebServiceSoap qqonlineSsSoap =service.getPort(QqOnlineWebServiceSoap.class);
////        //调用查询方法
////        String result = qqonlineSsSoap.qqCheckOnline("1565465");
////        System.out.println(result);
        
        WeatherWS factory = new WeatherWS();
        //根据工厂创建一个WeatherWSSoap对象
        WeatherWSSoap weatherWSSoap = factory.getWeatherWSSoap();
        //调用WebService提供的getWeather方法获取南宁市的天气预报情况
        ArrayOfString weatherInfo = weatherWSSoap.getSupportCityString("广东");
        List<String> lstWeatherInfo = weatherInfo.getString();
        //遍历天气预报信息
        for (String s : lstWeatherInfo) {
            System.out.print(s+"|");
        }
        System.out.println();
        
        ArrayOfString weatherInfo1 = weatherWSSoap.getWeather("广州", "");
        List<String> lstWeatherInfo1 = weatherInfo1.getString();
        //遍历天气预报信息
        for (String s : lstWeatherInfo1) {
            System.out.print(s+"|");
        }
        System.out.println();
        
    }
}

原文地址:https://www.cnblogs.com/tk55/p/11298435.html