基於AXIS的web service:
1 比如要建一個Server.java類的web service
public class Server {
public String printInfo(String name){
return "Hello,"+name;
}
}
2 把Server.java改為Server.Jws放到
…\Tomcat 5.5\webapps\axis中,重啟伺服器
3 訪問http://localhost:8080/axis/Server.jws?wsdl
4 在cmd中輸入 cd D:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\axis\WEB-INF
輸入命令:Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/Server.jws?wsdl
5 找到…\Tomcat 5.5\webapps\axis\WEB-INF下生成的localhost文件夾復制到工程中
6 建一個Client端的類進行測試:
public class Client {
public static void main(String arg[]) throws ServiceException, RemoteException{
ServerService ss=new ServerServiceLocator();
Server s=ss.getServer();
System.out.println("............"+s.printInfo("shiyou"));
}
}
⑵ Java客戶端調用Webservice介面求代碼
客戶端獲得自定義對象包路徑必須和伺服器端相同,當然在客戶端也可以不用建該對象,可以將客戶端的自定義對象打成jar包,然後在客戶端引用。
猜想用反射也可以實現改對象,但目前沒有寫相關例子。
<p>importjava.io.Serializable;</p><p>{
/**
*客戶端必須有與伺服器端相同的自定義對象
*/
=1L;
privateStringid;
privateStringname;
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}</p>
[java]viewplain
packageclient;
importpo.Hello;
{
/*
[java]viewplain
*該方法名必須和服務介面一致
[java]viewplain
*/
[java]viewplain
publicHelloexample();
[java]viewplain
<preclass="java"name="code">packageclient;
importjava.net.MalformedURLException;//importjava.net.URL;
//importorg.codehaus.xfire.client.Client;
importorg.codehaus.xfire.client.XFireProxyFactory;
importorg.codehaus.xfire.service.Service;
importorg.codehaus.xfire.service.binding.ObjectServiceFactory;
importpo.Hello;
publicclassServicesClient{
publicstaticvoidmain(String[]arg)throwsMalformedURLException,
Exception{
Stringxml="http://localhost:8080/web/services/HeloWebService";
=newObjectServiceFactory();
ServiceserviceModel=objectServiceFactory
.create(IClientHelloManager.class);
=newXFireProxyFactory();
IClientHelloManagerservice=(IClientHelloManager)xFireProxyFactory
.create(serviceModel,xml);
HellolHello=service.example();
System.out.println(lHello.getId());
System.out.println(lHello.getName());
//Clientclient=newClient(newURL(
//"http://localhost:8080/web/services/HeloWebService?wsdl"));
//Object[]rsult=client.invoke("example",newObject[]{"hello"});
//Hellohello=(Hello)rsult[0];
//System.out.println();
}
}</pre>
<pre></pre>
<pre></pre>
<pre></pre>
⑶ java webservice生成的客戶端代碼 需要哪些jar包
1、安裝環境 J2SE SDK 1.4,Tomcat 5.0,eclipse 3.2。
2、下載Axis安裝包。
3、將Axis相關包文件放在回WEB-INF\lib目錄下。
4、Axis可選答的包:activation.jar; mail.jar; xmlsec-1.4.Beta1.jar拷貝到WEB-INF目錄下,生成客戶端時候需要使用。
Axis支持三種web service的部署和開發,分別為:
1、Dynamic Invocation Interface (DII)
2、Dynamic Proxy方式
3、Stubs方式
建議我們使用Stubs方式,因此我就主要就介紹一下第三種方式。
注意,Java源代碼是放在D:\workspace\test\目錄下,Axis相關包文件放在D:\workspace\test\WEB-INF目錄下。