日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JDK6 中队web service的支持

發(fā)布時間:2023/12/15 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JDK6 中队web service的支持 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
JDK6中對Web Service的支持
原文: http://blog.csdn.net/zhongweijian/article/details/8008471

webService是一種跨語言的系統(tǒng)間交互標準。在java中使用webservice根據(jù)服務器端的服務根據(jù)描述生成WSDL文件,并將應用與此WSDL文件一起放入HTTP服務器中,借助服務工具根據(jù)WSDL文件生成客戶端STUB代碼。此代碼的作用是將產(chǎn)生的對象請求信息封裝成標準的SOAP格式數(shù)據(jù),并發(fā)送到服務器端,服務器端根據(jù)接收到的SOAP格式數(shù)據(jù)進行轉(zhuǎn)換,并最終通過反射調(diào)用響應類的響應方法。

?

Jdk 6中通過提供的wsimport工具,集成了WEB service的支持,通過WebService 的annotation來暴露服務的實現(xiàn),并通過Endpoint.publish將服務發(fā)布到指定的地址,客戶端通過wsimport來訪問響應地址的wsdl文件,生成調(diào)用服務器端服務的stub類信息,客戶端即可通過生成的類來調(diào)用服務器的服務了。

?

Usage: wsimport [options] <WSDL_URI>where [options] include:-b <path> specify jaxws/jaxb binding files or additional schemas(Each <path> must have its own -b)-B<jaxbOption> Pass this option to JAXB schema compiler-catalog <file> specify catalog file to resolve external entity referencessupports TR9401, XCatalog, and OASIS XML Catalog format.-d <directory> specify where to place generated output files-extension allow vendor extensions - functionality not specifiedby the specification. Use of extensions mayresult in applications that are not portable ormay not interoperate with other implementations-help display help-httpproxy:<host>:<port> specify a HTTP proxy server (port defaults to 8080)-keep keep generated files-p <pkg> specifies the target package-quiet suppress wsimport output-s <directory> specify where to place generated source files-target <version> generate code as per the given JAXWS spec versionDefaults to 2.2, Accepted values are 2.0, 2.1 and 2.2e.g. 2.0 will generate compliant code for JAXWS 2.0 spec-verbose output messages about what the compiler is doing-version print version information-wsdllocation <location> @WebServiceClient.wsdlLocation value-clientjar <jarfile> Creates the jar file of the generated artifacts along with theWSDL metadata required for invoking the web service.Extensions:-XadditionalHeaders map headers not bound to request or response message toJava method parameters-Xauthfile file to carry authorization information in the formathttp://username:password@example.org/stock?wsdl-Xdebug print debug information-Xno-addressing-databinding enable binding of W3C EndpointReferenceType to Java-Xnocompile do not compile generated Java files-XdisableSSLHostnameVerification disable the SSL Hostname verification while fetchingwsdlsExamples:wsimport stock.wsdl -b stock.xml -b stock.xjbwsimport -d generated http://example.org/stock?wsdl

具體示例如下:

?

1.對外暴露的接口

public interface TestWebService { public String echo(); }

2.服務器端的實現(xiàn)類,并通過@WebService來指定對外提供服務的服務名稱,客戶端生成的類目和包名

? import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService(name="MyTestWS",serviceName="MyWebService",targetNamespace="http://localhost/client") public class WebServiceImpl implements TestWebService{ @Override public String echo() { return "webservice return msg"; } public static void main(String[] args) { Endpoint.publish("http://localhost:8080/MyWebService", new WebServiceImpl()); } }

?

3.然后運行服務器的WebServiceImpl的main函數(shù),暴露服務并將服務注冊到一個http服務地址上,客戶端通過jdk的bin下面的wsimport命令來獲取服務器的wsdl文件并生成客戶端的stub類信息

wsimport -keep http://localhost:8080/MyWebService?wsdl


4.然后在你的路徑上就會生成下面幾個類


5.然后我們編寫客戶端的調(diào)用代碼

import localhost.client.MyWebService; public class WebServiceClient { public static void main(String[] args) { MyWebService myWebService = new MyWebService(); System.out.println(myWebService.getMyTestWSPort().echo()); } }
?


6.執(zhí)行客戶端的調(diào)用代碼,輸出如下:




7.我們看下最終生成客戶端服務調(diào)用的類內(nèi)容

package localhost.client; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; /** * This class was generated by the JAXWS SI. * JAX-WS RI 2.0_02-b08-fcs * Generated source version: 2.0 * */ @WebServiceClient(name = "MyWebService", targetNamespace = "http://localhost/client", wsdlLocation = "http://localhost:8080/MyWebService?wsdl") public class MyWebService extends Service { private final static URL MYWEBSERVICE_WSDL_LOCATION; static { URL url = null; try { url = new URL("http://localhost:8080/MyWebService?wsdl"); } catch (MalformedURLException e) { e.printStackTrace(); } MYWEBSERVICE_WSDL_LOCATION = url; } public MyWebService(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public MyWebService() { super(MYWEBSERVICE_WSDL_LOCATION, new QName("http://localhost/client", "MyWebService")); } /** * * @return * returns MyTestWS */ @WebEndpoint(name = "MyTestWSPort") public MyTestWS getMyTestWSPort() { return (MyTestWS)super.getPort(new QName("http://localhost/client", "MyTestWSPort"), MyTestWS.class); } }


總結(jié)

以上是生活随笔為你收集整理的JDK6 中队web service的支持的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。