MyEclipse开发WebService教程

目录(?)[+]

  1. 创建一个普通 Java 类

  2. 创建 webService 服务端

  3. 导入相关的 jar 包并将项目布署到 tomcat启动 tomcat

  4. 创建 webService 客户端项目普通 Java 项目

  5. 创建客户端相关调用类

  6. 创建测试类

  7. 运行测试类

. 创建一个 webService 工程。

2. 创建一个普通 Java 类

view plain copy to clipboard print ?
  1. package com.hmw.jaxws;  

  2. /**

  3. * 提供 WebService 服务的测试类<br>

  4. * <b>注意:</b>该类必须有一个无参构造方法,否则无法使用 MyEclipse 的向导为此类创建 jax-ws 的 webService 服务端类<br>

  5. * <b>创建日期</b>:2011-1-17

  6. * @author <a href="mailto:[email protected]" mce_href="mailto:[email protected]">何明旺</a>

  7. */

  8. publicclass HelloJaxws {

  9. public String sayHello(String userName) {

  10. return userName + ",您好,这是我的第一个 jax-ws 程序。";

  11. }

  12. publicint add(int i, int j) {

  13. return i + j;  

  14. }

  15. }

[java] view plain copy print ?
  1. package com.hmw.jaxws;    

  2. /**

  3. * 提供 WebService 服务的测试类<br>

  4. * <b>注意:</b>该类必须有一个无参构造方法,否则无法使用 MyEclipse 的向导为此类创建 jax-ws 的 webService 服务端类<br>

  5. * <b>创建日期</b>:2011-1-17

  6. * @author <a href="mailto:[email protected]" mce_href="mailto:[email protected]">何明旺</a>

  7. */

  8. publicclass HelloJaxws {    

  9. public String sayHello(String userName) {    

  10. return userName + ",您好,这是我的第一个 jax-ws 程序。";    

  11.    }    

  12. publicint add(int i, int j) {    

  13. return i + j;    

  14.    }    

  15. }    

package com.hmw.jaxws; /** * 提供 WebService 服务的测试类<br> * <b>注意:</b>该类必须有一个无参构造方法,否则无法使用 MyEclipse 的向导为此类创建 jax-ws 的 webService 服务端类<br> * <b>创建日期</b>:2011-1-17 * @author <a href="mailto:[email protected]" mce_href="mailto:[email protected]">何明旺</a> */ public class HelloJaxws { public String sayHello(String userName) { return userName + ",您好,这是我的第一个 jax-ws 程序。"; } public int add(int i, int j) { return i + j; } }


3. 创建 webService 服务端

HelloJaxwsDelegate.java 的源代码如下:

view plain copy to clipboard print ?
  1. package com.hmw.jaxws;  

  2. @javax.jws.WebService(targetNamespace = "http://jaxws.hmw.com/", serviceName =  "HelloJaxwsService", portName = "HelloJaxwsPort", wsdlLocation = "WEB-INF/wsdl/HelloJaxwsService.wsdl")  

  3. publicclass HelloJaxwsDelegate {

  4. com.hmw.jaxws.HelloJaxws helloJaxws = new com.hmw.jaxws.HelloJaxws();

  5. public String sayHello(String userName) {

  6. return helloJaxws.sayHello(userName);

  7. }

  8. publicint add(int i, int j) {

  9. return helloJaxws.add(i, j);

  10. }

  11. }

[java] view plain copy print ?
  1. package com.hmw.jaxws;    

  2. @javax.jws.WebService(targetNamespace = "http://jaxws.hmw.com/", serviceName = "HelloJaxwsService", portName = "HelloJaxwsPort", wsdlLocation = "WEB-INF/wsdl/HelloJaxwsService.wsdl")    

  3. publicclass HelloJaxwsDelegate {    

  4.    com.hmw.jaxws.HelloJaxws helloJaxws = new com.hmw.jaxws.HelloJaxws();    

  5. public String sayHello(String userName) {    

  6. return helloJaxws.sayHello(userName);    

  7.    }    

  8. publicint add(int i, int j) {    

  9. return helloJaxws.add(i, j);    

  10.    }    

  11. }  

package com.hmw.jaxws; @javax.jws.WebService(targetNamespace = "http://jaxws.hmw.com/", serviceName = "HelloJaxwsService", portName = "HelloJaxwsPort", wsdlLocation = "WEB-INF/wsdl/HelloJaxwsService.wsdl") public class HelloJaxwsDelegate { com.hmw.jaxws.HelloJaxws helloJaxws = new com.hmw.jaxws.HelloJaxws(); public String sayHello(String userName) { return helloJaxws.sayHello(userName); } public int add(int i, int j) { return helloJaxws.add(i, j); } }

4. 导入相关的 jar 包,并将项目布署到 tomcat,启动 tomcat.

整个项目的目录结构如下图所示:

5. 创建 webService 客户端项目(普通 Java 项目)。

6. 创建客户端相关调用类

7. 创建测试类

测试类 ClientTest.java 的代码如下:

view plain copy to clipboard print ?
  1. package com.hmw.client.jaxws;

  2. publicclass ClientTest {

  3. publicstaticvoid main(String[] args) {

  4. HelloJaxwsDelegate service = new HelloJaxwsService().getHelloJaxwsPort();

  5. System.out.println(service.sayHello("hemingwang"));

  6. System.out.println("result is: " + service.add(1, 2));

  7. }

  8. }

[java] view plain copy print ?
  1. package com.hmw.client.jaxws;    

  2. publicclass ClientTest {    

  3. publicstaticvoid main(String[] args) {    

  4.        HelloJaxwsDelegate service = new HelloJaxwsService().getHelloJaxwsPort();    

  5.            System.out.println(service.sayHello("hemingwang"));    

  6.            System.out.println("result is: " + service.add(1, 2));    

  7.        }    

  8.    }    

package com.hmw.client.jaxws; public class ClientTest { public static void main(String[] args) { HelloJaxwsDelegate service = new HelloJaxwsService().getHelloJaxwsPort(); System.out.println(service.sayHello("hemingwang")); System.out.println("result is: " + service.add(1, 2)); } }


整个项目的目录结构如下图所示:

8. 运行测试类

运行结果如下图所示:


你可能感兴趣的:(java,服务端,客户端,package,public)