Java6 WebService学习
生活随笔
收集整理的這篇文章主要介紹了
Java6 WebService学习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考了網絡上中資料,自己學習實踐了一個Java6 WebService.
首先,建立一個WebService:
?
package?garfield;import?javax.jws.WebService;
import?javax.xml.ws.Endpoint;
@WebService
public?class?MyJ6WebService?{
????????public?String?SayHello(String?strName)?{
????????????????return?"Hello?,"+strName+"!";
????????}
????????public?static?void?main(String[]?args)?{
????????????????//發布WebService,注意如果提示:Exception?in?thread?"main"?com.sun.xml.internal.ws.server.ServerRtException:?Server?Runtime?Error:?java.net.BindException:?Address?already?in?use:?bind
????????????????//則需要修改一下發布端口
????????????????Endpoint.publish("http://localhost:8030/garfield.MyJ6WebService",?new?MyJ6WebService());
????????????????System.out.println("WebService?was?published?success?!");
????????}
}
啟動Tomcat,然后運行,系統會輸出:WebService?was?published?success?!,表示WebService已啟動。
?
可以在瀏覽器中輸入地址:http://localhost:8030/garfield.MyJ6WebService?wsdl
顯示WebService信息:
?
<?xml?version="1.0"?encoding="UTF-8"??>?-?<!--??Published?by?JAX-WS?RI?at?http://jax-ws.dev.java.net.?RI's?version?is?JAX-WS?RI?2.2.4-b01.?
??-->?
-?<!--??Generated?by?JAX-WS?RI?at?http://jax-ws.dev.java.net.?RI's?version?is?JAX-WS?RI?2.2.4-b01.?
??-->?
-?<definitions?xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"?xmlns:wsp="http://www.w3.org/ns/ws-policy"?xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"?xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"?xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"?xmlns:tns="http://garfield/"?xmlns:xsd="http://www.w3.org/2001/XMLSchema"?xmlns="http://schemas.xmlsoap.org/wsdl/"?targetNamespace="http://garfield/"?name="MyJ6WebServiceService">
-?<types>
-?<xsd:schema>
??<xsd:import?namespace="http://garfield/"?schemaLocation="http://localhost:8030/garfield.MyJ6WebService?xsd=1"?/>?
??</xsd:schema>
??</types>
-?<message?name="SayHello">
??<part?name="parameters"?element="tns:SayHello"?/>?
??</message>
+?<message?name="SayHelloResponse">
??<part?name="parameters"?element="tns:SayHelloResponse"?/>?
??</message>
-?<portType?name="MyJ6WebService">
-?<operation?name="SayHello">
??<input?wsam:Action="http://garfield/MyJ6WebService/SayHelloRequest"?message="tns:SayHello"?/>?
??<output?wsam:Action="http://garfield/MyJ6WebService/SayHelloResponse"?message="tns:SayHelloResponse"?/>?
??</operation>
??</portType>
-?<binding?name="MyJ6WebServicePortBinding"?type="tns:MyJ6WebService">
??<soap:binding?transport="http://schemas.xmlsoap.org/soap/http"?style="document"?/>?
-?<operation?name="SayHello">
??<soap:operation?soapAction=""?/>?
-?<input>
??<soap:body?use="literal"?/>?
??</input>
-?<output>
??<soap:body?use="literal"?/>?
??</output>
??</operation>
??</binding>
-?<service?name="MyJ6WebServiceService">
-?<port?name="MyJ6WebServicePort"?binding="tns:MyJ6WebServicePortBinding">
??<soap:address?location="http://localhost:8030/garfield.MyJ6WebService"?/>?
??</port>
??</service>
??</definitions>
?
?
在WebService啟動的狀態下,在命令行中輸入:
?
E:\Temp>wsimport?-p?garfield.garfieldj6wsclient?-keep?http://localhost:8030/garfield.MyJ6WebService?wsdl
parsing?WSDL...
generating?code...
compiling?code...
E:\Temp>
注意,一定要對應你發布的WebService信息!
?
然后系統會自動生成相關的接口文件,如下:
?
新建Java工程,將輸出文件引入,然后建立測試類:
?
package?xxh;import?garfield.garfieldj6wsclient.*;
public?class?WebClient?{
????????/**??
?????????*?@param?args??
?????????*/??
????????public?static?void?main(String[]?args)?{???
?????????????//創建一個客戶端服務對象?
????????????MyJ6WebService?myJ6WS?=?new?MyJ6WebServiceService().getMyJ6WebServicePort();?
????????????//調用服務方法,并得到方法返回值?
????????????String?strTest?=?myJ6WS.sayHello("Garfield");?
????????????//打印服務的返回值?
????????????System.out.println(strTest);?
????????}???
}
?
?在WebService運行情況下,運行客戶端測試程序,輸出:
Hello ,Garfield!
?
?
轉載于:https://www.cnblogs.com/GarfieldTom/archive/2012/09/14/2684361.html
總結
以上是生活随笔為你收集整理的Java6 WebService学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小波同态滤波 matlab,matlab
- 下一篇: 迭代器是神马东西