http://www.micmiu.com/soa/webservice/jax-ws-demo/
目錄:
[一]、概述 Java API for XML Web Services (JAX-WS)是Java程序設計語言一個用來創建Web服務的API。
在服務器端,用戶只需要通過Java語言定義遠程調用所需要實現的接口SEI(service endpoint interface),并提供相關的實現,通過調用JAX-WS的服務發布接口就可以將其發布為WebService接口。
在客戶端,用戶可以通過JAX-WS的API創建一個代理(用本地對象來替代遠程的服務)來實現對于遠程服務器端的調用。
當然 JAX-WS 也提供了一組針對底層消息進行操作的API調用,你可以通過Dispatch 直接使用SOAP消息或XML消息發送請求或者使用Provider處理SOAP或XML消息。
JAX-WS2.0 (JSR 224 )是Sun新的web services協議棧,是一個完全基于標準的實現。在binding層,使用的是the Java Architecture for XML Binding (JAXB, JSR 222 ),在parsing層,使用的是the Streaming API for XML (StAX, JSR 173 ),同時它還完全支持schema規范。
JAX-WS與JAX-RPC的區別?可參見:http://java.sun.com/xml/faq.html#JAX-WS-and-JAX-RPC-difference
JAX-WS一些參考資料:
JAX-RPC 2.0 renamed to JAX-WS 2.0 The Java web service Tutorial? javax.jws.WebService
[二]、實驗環境
java version “1.6.0_18″、Eclipse3.7 maven構建項目:mvn archetype:create -DgroupId=com.micmiu.jaxws.demo -DartifactId=jaxws-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[三]、服務端的實現
1.最基本的實例
編寫接口代碼:HelloService.java
查看源代碼 打印幫助
1 package com.micmiu.jaxws.demo;
4 ?* blog http://www.micmiu.com
8 public interface HelloService {
9 ????String sayHello(String userName);
實現接口并添加webservice注釋:HelloServiceImpl.java
查看源代碼 打印幫助
1 package com.micmiu.jaxws.demo.impl;
3 import javax.jws.WebMethod;
4 import javax.jws.WebParam;
5 import javax.jws.WebService;
6 import javax.jws.soap.SOAPBinding;
8 import com.micmiu.jaxws.demo.HelloService;
11 ?* blog http://www.micmiu.com
16 //默認SOAPBinding style = Style.DOCUMENT
18 public class HelloServiceImpl implements HelloService {
21 ????public String sayHello(@WebParam(name = "userName") String userName) {
22 ????????return "hi," + userName + " welcom to www.micmiu.com";
編寫服務端發布代碼:ServerStart.java
查看源代碼 打印幫助
1 package com.micmiu.jaxws.demo;
3 import javax.xml.ws.Endpoint;
5 import com.micmiu.jaxws.demo.impl.HelloServiceImpl;
8 ?* blog http://www.micmiu.com
12 public class ServerStart {
17 ????public static void main(String[] args) {
18 ????????System.out.println("start publish jax-ws ...");
19 ????????HelloService service = new HelloServiceImpl();
20 ????????Endpoint.publish("http://localhost:8082/HelloService", service);
21 ????????System.out.println("publish webservice successful");
運行ServerStart,日志如下:
start publish jax-ws ...
2012-7-12 10:56:41 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
信息: Dynamically creating request wrapper Class com.micmiu.jaxws.demo.impl.jaxws.SayHello
2012-7-12 10:56:42 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
信息: Dynamically creating response wrapper bean Class com.micmiu.jaxws.demo.impl.jaxws.SayHelloResponse
publish webservice successful
瀏覽器打開:http://localhost:8082/HelloService?wsdl 回車顯示如下:
可見服務端已經發布成功。
運用JDK自動的命令: wsgen 生成wsdl文件和異常處理的相關類
ps: 如果webservice中有異常聲明,必須用wsgen生成常處理的相關類之后,才能發布。
按win+R鍵,輸入cmd回車進入命令行界面,切換到target 下創建目錄ws,在執行如下命令:
查看源代碼 打印幫助
1 mkdir ws\bin ws\src ws\wsdl
bin ? ?-> 存放生成的class文件 src ? ?-> 存放生成的源代碼文件 wsdl ?->?存放生成的wsdl 文件
再執行如下命令:
查看源代碼 打印幫助
1 wsgen -cp .;classes/ -r ws/wsdl -s ws/src -d ws/bin -wsdl com.micmiu.jaxws.demo.impl.HelloServiceImpl
生成后的目錄如下:
D:\workspace_dev\jaxws-demo\target>tree /F ws
卷 work 的文件夾 PATH 列表
卷序列號為 2AF7-9BD9
D:\WORKSPACE_DEV\JAXWS-DEMO\TARGET\WS
├─bin
│ └─com
│ └─micmiu
│ └─jaxws
│ └─demo
│ └─impl
│ └─jaxws
│ SayHello.class
│ SayHelloResponse.class
│
├─src
│ └─com
│ └─micmiu
│ └─jaxws
│ └─demo
│ └─impl
│ └─jaxws
│ SayHello.java
│ SayHelloResponse.java
│
└─wsdlHelloServiceImplService.wsdlHelloServiceImplService_schema1.xsd
2.@WebService 指定 endpointInterface 實例
修改接口代碼:HelloService.java
查看源代碼 打印幫助
1 package com.micmiu.jaxws.demo;
3 import javax.jws.WebMethod;
4 import javax.jws.WebParam;
5 import javax.jws.WebService;
6 import javax.jws.soap.SOAPBinding;
7 import javax.jws.soap.SOAPBinding.Style;
10 ?* blog http://www.micmiu.com
15 @SOAPBinding(style = Style.DOCUMENT)
16 public interface HelloService {
18 ????String sayHello(@WebParam(name = "userName") String userName);
修改接口實現類:HelloServiceImpl.java
查看源代碼 打印幫助
1 package com.micmiu.jaxws.demo.impl;
3 import javax.jws.WebService;
5 import com.micmiu.jaxws.demo.HelloService;
8 ?* blog http://www.micmiu.com
12 @WebService(endpointInterface = "com.micmiu.jaxws.demo.HelloService")
13 public class HelloServiceImpl implements HelloService {
14 ????public String sayHello(String userName) {
15 ????????return "hi," + userName + " welcom to www.micmiu.com";
運行ServerStart 啟動程序,可通過運行日志和瀏覽器訪問wsdl文件進行驗證。
[四]、客戶端的實現
1. wsimport 生成客戶端
按win+R鍵,輸入cmd回車進入dos,切到target目錄,然后創建目錄:client、client\bin、client\src
在target 目錄下運行如下命令回車即可生成客戶端文件:
查看源代碼 打印幫助
1 wsimport -s client/src -d client/bin -p com.micmiu.jaxws.client http://localhost:8082/HelloService?wsdl
生成源文件目錄結構如下:
D:\WORKSPACE_DEV\JAXWS-DEMO\TARGET\CLIENT\SRC
└─com└─micmiu└─jaxws└─clientHelloServiceImpl.javaHelloServiceImplService.javaObjectFactory.javapackage-info.javaSayHello.javaSayHelloResponse.java
2.編寫客戶端測試程序 :HelloClient.java
查看源代碼 打印幫助
1 package com.micmiu.jaxws.client;
4 ?* blog http://www.micmiu.com
7 public class HelloClient {
12 ????public static void main(String[] args) {
13 ????????HelloServiceImplService service = new HelloServiceImplService();
14 ????????HelloServiceImpl hello = service.getHelloServiceImplPort();
15 ????????System.out.println("start webservice client ...");
16 ????????System.out.println("send Michael to server ");
17 ????????System.out.println(hello.sayHello("Michael"));
18 ????????System.out.println("test client end.");
運行測試程序,日志如下:
start webservice client ...
send Michael to server
hi,Michael welcom to www.micmiu.com
test client end.
可見客戶端調用成功。
?
?
========================
http://daizh868.iteye.com/blog/1194815/
使用Myeclipse 8.5開發基于JAX-WS的Web service實例
?
?
=======================
https://forums.oracle.com/forums/thread.jspa?threadID=1115210
java.lang.NoClassDefFoundError:? com/sun/xml/ws/spi/ProviderImpl
Hi Before you run this client program from any IDE or command prompt, set wlfullclient.jar in the classpath. To generate this wlfullclient.jar, please refer the notes from my below post. In Eclipse or JBuilder, you can select your project and set this library in classpath. Or in command prompt set it manually. http://forums.oracle.com/forums/thread.jspa?messageID=3977012 Note: I already generated this wlfullclient.jar. And I DO SEE your missing class in this jar file. So the above solution should fix your problem. Thanks Ravi Jegga
總結
以上是生活随笔 為你收集整理的JAX-WS开发webservice示例详解 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。