Web Services是由企業發佈的完成其特定商務需求的在線應用服務,其餘公司或應用軟件可以經過Internet來訪問並使用這項在線服務。java
Web Service的關鍵技術和規則:web
1.XML:描述數據的標準方法.apache
2.SOAP:表示信息交換的協議(簡單對象訪問協議).api
3.WSDL:Web服務描述語言.瀏覽器
4.UDDI:通用描述、發現與集成,他是一種獨立於平臺,基於XML語言的用於在互聯網上描述商務的協議。tomcat
1、利用JDK web服務api實現,這裏使用基於SOAP message的Web Service:服務器
1.首先建立一個Web Services項目,做爲Web services Endpoint.app
2.建立一個HelloService.java類框架
[c-sharp] view plaincopyprint?eclipse
package com.yjpeng.hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class HelloService {
@WebMethod
public String sayHello(String message){
return "Hello ," + message;
}
public static void main(String[] args) {
//create and publish an endPoint
HelloService hello = new HelloService();
Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);
}
}
package com.yjpeng.hello; import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.ws.Endpoint;@WebService public class HelloService { @WebMethod public String sayHello(String message){ return "Hello ," + message; } public static void main(String[] args) { //create and publish an endPoint HelloService hello = new HelloService(); Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello); } }
3.使用apt編譯HelloService.java(例如: apt -d bin(bin存放編譯後的文件目錄) scr/com/yjpeng/hello/HelloService.java)後,會生成jaxws目錄。
4.使用java com.yjpeng.hello.HelloService執行HelloService.java文件,在瀏覽器中輸入http://localhost:8080/helloService?wsdl出現以下圖
5.使用wsimport命令生成客戶端:wsimport -p com.yjpeng.webservice -keep http://localhost:8080/helloService?wsdl 這時會在當前目錄中生成以下文件:
6.編寫好客戶端文件HelloClient.java
[c-sharp] view plaincopyprint?
package com.yjpeng.hello;
import com.yjpeng.webservice.HelloServiceService;
public class HelloClient {
public static void main(String[] args) {
HelloServiceService helloServiceService = new HelloServiceService();
com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort();
System.out.println(helloService.sayHello("你好"));
}
}
package com.yjpeng.hello; import com.yjpeng.webservice.HelloServiceService; public class HelloClient { public static void main(String[] args) { HelloServiceService helloServiceService = new HelloServiceService(); com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort(); System.out.println(helloService.sayHello("你好")); } }
運行結果在控制檯輸出 hello,你好 證實利用JDK web服務API實現web service成功!
2、使用xfire框架,我這裏使用的是MyEclipse集成的xfire進行測試,利用xfire開發WebService能夠有三種方法:
a.一種是從JavaBean中生成。
b.一種是從wsdl文件中生成。
c.一種是本身創建webservice。
具體實現步驟以下:
1.用Myeclipse創建webService工程(注意:Web Service&J2EE Details中的Framework選XFire),目錄結構以下:
2.建立IHello.java接口
[c-sharp] view plaincopyprint?
package com.yjpeng.hello;
public interface IHello {
public String sayHello(String message);
}
package com.yjpeng.hello; public interface IHello { public String sayHello(String message); }
3.建立IHelloImpl.java實現IHello.java接口
[c-sharp] view plaincopyprint?
package com.yjpeng.hello;
public class IHelloImpl implements IHello {
public String sayHello(String message) {
return message;
}
}
package com.yjpeng.hello; public class IHelloImpl implements IHello { public String sayHello(String message) { return message; } }
4.修改Service.xml文件,加入如下代碼
<service>
<!-- 爲該Service起一個名字 -->
<name>HelloService</name>
<!-- service的接口類 -->
<serviceClass>
com.yjpeng.hello.IHello
</serviceClass>
<!-- service的接口實現類-->
<implementationClass>
com.yjpeng.hello.IHelloImpl
</implementationClass>
<!-- wsdl的樣式-->
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
5.把該Web Service項目部署到tomcat裏面啓動tomcat在瀏覽器地址欄輸入http://localhost:8080/TestWebServices/services/Hello?wsdl 出現以下圖
6.而後在展開HelloService後面的wsdl能夠看到
7.建立一個客戶端HelloClient.java類
[c-sharp] view plaincopyprint?
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.yjpeng.hello.IHello;
public class HelloClient {
public static void main(String[] args) {
Service s = new ObjectServiceFactory().create(IHello.class);
XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String url="http://192.168.122.128:8080/TestWebServices/services/HelloService";
IHello hello;
try {
hello = (IHello)xf.create(s, url);
System.out.println(hello.sayHello("你好"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
//這個是在java端調用.net寫的遠程Web Service 若是調用本機寫的只須要把URL中的地址換成本機能訪問的地址便可
Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"});
System.out.println(o.length);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.net.MalformedURLException; import java.net.URL; import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.client.Client; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.yjpeng.hello.IHello; public class HelloClient { public static void main(String[] args) { Service s = new ObjectServiceFactory().create(IHello.class); XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); String url="http://192.168.122.128:8080/TestWebServices/services/HelloService"; IHello hello; try { hello = (IHello)xf.create(s, url); System.out.println(hello.sayHello("你好")); } catch (MalformedURLException e) { e.printStackTrace(); } try { //這個是在java端調用.net寫的遠程Web Service 若是調用本機寫的只須要把URL中的地址換成本機能訪問的地址便可 Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl")); Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"}); System.out.println(o.length); } catch (Exception e) { e.printStackTrace(); } } }
運行HelloClient.java類能夠輸出 你好 1.
3、使用axis1.4開發webservice方法
首先下載axis1.4包和tomcat服務器,並將解壓後的axis1.4包下面的webapps下的axis目錄複製到tomcat服務器的webapps文件夾中。axis支持三種Web Service的部署和開發,分別爲:
a.Dynamic Invocation Interface(DII)
b.Stubs 方式
c.Dynamic Proxy方式
1.編寫DII(Dynamic Invocation Interface)方式Web Service
a.編寫服務程序HelloClient.java
[c-sharp] view plaincopyprint?
public class HelloClient
{
public String getName(String name){
return "hello," + name;
}
}
public class HelloClient { public String getName(String name){ return "hello," + name; } }
b.將HelloClient.java文件拷貝到axis_home下,重命名爲HelloClient.jws.
c.訪問連接http://localhost:8080/axis/HelloClient.jws?wsdl頁面顯示axis自動生成的wsdl文件
d.編寫訪問服務的客戶端TestHelloClient.java須要導入相應的axis.jar包,在下載的axis的WEB-INF/lib/目錄下。
[c-sharp] view plaincopyprint?
package com.yjpeng.webservice;
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestHelloClient {
public static void main(String[] args){
try{
String endpoint = "http://localhost:8080/axis/HelloClient.jws";
Service service = new Service();
Call call = (Call)service.createCall();
call.setOperationName(new QName(endpoint, "getName"));
call.setTargetEndpointAddress(new URL(endpoint));
String result = (String) call.invoke(new Object[]{"張三"});
System.out.println(result);
}catch (Exception e) {
e.printStackTrace();
}
}
}
package com.yjpeng.webservice; import java.net.URL; import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class TestHelloClient { public static void main(String[] args){ try{ String endpoint = "http://localhost:8080/axis/HelloClient.jws"; Service service = new Service(); Call call = (Call)service.createCall(); call.setOperationName(new QName(endpoint, "getName")); call.setTargetEndpointAddress(new URL(endpoint)); String result = (String) call.invoke(new Object[]{"張三"}); System.out.println(result); }catch (Exception e) { e.printStackTrace(); } } }
運行TestHelloClient.java在控制檯輸出hell,張三,測試成功.