你的位置:首页 > Java教程

[Java教程]WebService 实例


使用Java WebService API实现
1.服务端接口:
package com.h3c.itac.webservice;import javax.jws.WebService;@WebService public interface IServer {  public String sayHello(String name);}

2.服务端接口实现:
package com.h3c.itac.webservice;import javax.jws.WebService;@WebService(endpointInterface="com.h3c.itac.webservice.IServer")public class Server implements IServer {  public String sayHello(String name){    System.out.println("hello "+name);    return name;  }}


 

3.发布服务:
package com.h3c.itac.webservice;import javax.public class PublishServer {  public static void main(String[] args){    System.out.println("启动webservice服务!");    Server server=new Server();    Endpoint.publish("http://localhost:9090/web/myWebService", server);    System.out.println("server 启动成功!");  }}

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


 

6.客户端测试:
package com.h3c.itac.webservice;import java.net.MalformedURLException;import java.net.URL;import javax.import javax.import org.junit.Test;public class TestClient {  @Test  public void testClient() throws MalformedURLException{    QName qname=new QName("http://webservice.itac.h3c.com/","ServerService");    URL url = new URL("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).客户端: