1 需要必備的開發環境:tomcat+eclipse+jdk
2 網上下載xfire-distribution-1.2.6.zip包,將裏邊的jar拷貝到工程web的lib下
3 在ide裏邊新建一個web service工程,會自動生成一個services.xml文件,文件中將配置上信息
4 service代碼
4.1創建一個java接口類:
package com.taikang.ws;
public interface IHelloService
{
public String IHelloService(String name);
}
4.2創建實現類:
package com.taikang.ws;
public class HelloServeceImpl implements IHelloService
{
public String IHelloService(String name)
{
// TODO Auto-generated method stub
String ret = "進入服務器,已經獲得信息,name:" + name + ".";
return ret;
}
}
4.3配置service.xml文件內容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<!-- 配置webservice調用的名字 -->
<service>
<name>IHelloService</name>
<serviceClass>com.taikang.ws.IHelloService</serviceClass>
<implementationClass>com.taikang.ws.HelloServeceImpl</implementationClass>
</service>
</beans>
name:(IHelloService)提供客戶端調用接口的名字
serviceClass:實現類
implementionClass:接口
配置完,重啓服務器,輸入網址:
http://10.136.16.161:9080/webservice/services/IHelloService?wsdl
10.136.16.161:本機ip
9080 服務器端口
webservice:服務器中發佈的工程名
services:services.xml文件
IHelloService:調用的接口名字
在服務器啓動情況下,頁面有返回,則services佈置成功
5 測試代碼
新建一個web工程,新建類函數:
public String send(String name)
{
String portType = "IHelloService";
String feResponseXml ="";
String webLogUrl = "http://10.136.16.161:9080/webservice/services/IHelloService?wsdl";
String timeout = null;
try
{
if(timeout==null)
timeout = "60000";
System.setProperty("sun.net.client.defaultConnectTimeout", timeout);
System.setProperty("sun.net.client.defaultReadTimeout", timeout);
URL url = new URL(webLogUrl);
Client client = new Client(url);
client.setTimeout(Integer.parseInt(timeout));
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, timeout);
Object[] result = client.invoke(portType, new Object[] {name});
if(result !=null && result.length>0 && result[0]!=null)
{
feResponseXml = result[0].toString();
}
System.out.println("出參:"+feResponseXml);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(" send error:" + e);
}
return feResponseXml;
}
調用功能類:
package com.taikang.ws;
public class WSClient
{
public static void main(String[] args)
{
System.out.println("start...");
Clientdeal wsc = new Clientdeal();
String retmsg = wsc.send("wang");
System.out.println("retMsg:" + retmsg);
}
}
注:如果調用出現錯誤:找不到服務器提供的方法,原因可能是服務器提供的類方法與service.xml中配置不一樣的問題。