WebService实例

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

使用javaWebService API实现1.服务端接口:packagecom.h3c.itac.webservice;importjavax.jws.WebService;

@WebServicepublicinterfaceIServer {publicString sayHello(String name);

}

2.服务端接口实现:packagecom.h3c.itac.webservice;importjavax.jws.WebService;

@WebService(endpointInterface="com.h3c.itac.webservice.IServer")publicclassServerimplementsIServer {publicString sayHello(String name){

System.out.PRintln("hello "+name);returnname;

}

}

3.发布服务:packagecom.h3c.itac.webservice;importjavax.xml.ws.Endpoint;publicclassPublishServer {publicstaticvoidmain(String[] args){

System.out.println("启动webservice服务!");

Server server=newServer();

Endpoint.publish("http://localhost:9090/web/myWebService", server);

System.out.println("server 启动成功!");

}

}

4.访问http://localhost:9090/web/myWebService,查看WebService信息

5.点击http://localhost:9090/web/myWebService?wsdl 查看生成的wsdl:This XML file does not appear to have any style information associated with it. The document tree is shown below.<!--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.--><definitionsxmlns: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://webservice.itac.h3c.com/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.xmlsoap.org/wsdl/"targetNamespace="http://webservice.itac.h3c.com/"name="ServerService"><types><xsd:schema><xsd:importnamespace="http://webservice.itac.h3c.com/"schemaLocation="http://localhost:9090/web/myWebService?xsd=1"/></xsd:schema></types><messagename="sayHello"><partname="parameters"element="tns:sayHello"/></message><messagename="sayHelloResponse"><partname="parameters"element="tns:sayHelloResponse"/></message><portTypename="Server"><Operationname="sayHello"><inputwsam:Action="http://webservice.itac.h3c.com/Server/sayHelloRequest"message="tns:sayHello"/><outputwsam:Action="http://webservice.itac.h3c.com/Server/sayHelloResponse"message="tns:sayHelloResponse"/></operation></portType><bindingname="ServerPortBinding"type="tns:Server"><soap:bindingtransport="http://schemas.xmlsoap.org/soap/http"style="document"/><operationname="sayHello"><soap:operationsoapAction=""/><input><soap:bodyuse="literal"/></input><output><soap:bodyuse="literal"/></output></operation></binding><servicename="ServerService"><portname="ServerPort"binding="tns:ServerPortBinding"><soap:addresslocation="http://localhost:9090/web/myWebService"/></port></service></definitions>

6.客户端测试:packagecom.h3c.itac.webservice;importjava.net.MalformedURLException;importjava.net.URL;importjavax.xml.namespace.QName;importjavax.xml.ws.Service;importorg.junit.Test;publicclassTestClient {

@TestpublicvoidtestClient()throwsMalformedURLException{

QName qname=newQName("http://webservice.itac.h3c.com/","ServerService");

URL url=newURL("http://localhost:9090/web/myWebService?wsdl");

Service service=Service.create(url, qname);

IServer server= service.getPort(IServer.class);

String sayHello= server.sayHello("zhangsan");

System.out.println("aaa "+sayHello);

}

}

7.输出:1).服务端:

2).客户端:

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航