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)容
總結(jié)
以上是生活随笔為你收集整理的JDK6 中队web service的支持的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快手营销短信怎么取消
- 下一篇: Servlet3 -- Servlet异