如何调用wsdl生成本地代理类:
通过wsdl生成本地代理类,有很多方式,既可以通过wsdl2java命令手动生成,也可以通过eclipse的axis2插件、xfire插件等。但是通过项目的实践,觉得尽量还是通过axis2的wsdl2java的命令生成,会省去很多麻烦。比如说,如果你拥有很多的wsdl文件,他们又是相互联系的,这样你通过eclipse的插件生成时,会出现错误。因为它只能一下加载一个wsdl文件。这个问题在项目初期,让我吃了很多的苦头。。。。。
Axis2是一个比较常用的WebService引擎,大家可以通过到http://ws.apache.org/axis2/下载,其中其中axis2-1.4.1-bin.zip文件中包含了Axis2中所有的jar文件,以及命令工具, axis2-1.4.1-war.zip文件则用于将WebService发布到Web容器中,网上有很多axis2教程,在这里不再多说。
天气预报调用实例:
下面的一个实例是调用天气预报的一个例子,没有采用wsdl生成本地代理类的方式,采用的是通过http请求直接访问服务端点的方法:
步骤是:
1、利用soap向webservice endpoint进行请求,取回请求结果
2、通过dom4J解析返回的xml流,得到所要的信息。
通过这个例子相信大家,会对”webserviice是基于soap协议”的这句话有更深刻的理解,另外在使用dom4J解析xml返回流的过程中,遇到了些麻烦,需要额外引入jaxen-1.1.1.jar包,否则程序会报org/jaxen/JaxenException的错误。并且在XML包含命名空间时,定位元素,需要按照xpath语法来写。
下面是类的源代码:
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 类作用调用webservice得到天气预报服务
* @author qsw-Myonlystar 2010-1-13上午09:59:45
*/
public class Weather {
/**
* 获取soap请求头,并替换其中的标志符号为用户的输入符号
* @param city 用户输入城市名
* @return 用户将要发送给服务器的soap请求
*/
private static String getSoapRequest(String city) {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body> <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
+ "<theCityName>" + city
+ "</theCityName> </getWeatherbyCityName>"
+ "</soap:Body></soap:Envelope>");
return sb.toString();
}
/**
* 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流
* @param city 用户输入的城市名称
* @return 服务器端返回的输入流,供客户端读取
* @throws Exception
*/
public static InputStream getSoapInputStream(String city) throws Exception {
try {
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
URL url = new URL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction",
"http://WebXml.com.cn/getWeatherbyCityName");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
InputStream is = conn.getInputStream();
//System.out.println(is.toString());
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 通过dom4j对服务器端返回的XML进行解析
* @param city 用户输入的城市名称
* @return 符串 用,分割
*/
public static String getWeather(String city) {
Document document=null;
SAXReader reader = new SAXReader();
String s="";
Map map=new HashMap();
map.put("design", "http://WebXml.com.cn/");
reader.getDocumentFactory().setXPathNamespaceURIs(map);
try {
InputStream is = getSoapInputStream(city);//得到输入流
document=reader.read(is);//将输入流转化为document
String t=document.asXML();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List nodes = document.selectNodes("//design:string");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
String text=elm.getText();
//System.out.println("fsffs"+text);
s=s+elm.getText()+"\n";
}
return s;
}
/**
* 测试函数
* @param args
*/
public static void main(String args[]){
Weather w=new Weather();
System.out.println(w.getWeather("泰安"));
}
}
另外向大家推荐一个比较好的网站,http://www.webxml.com.cn/zh_cn/index.aspx。上面又很多好的webservice可以供我们调用。