MyEclipse开发WebService教程

创建一个 webService 工程。

0_1295240435Quin.gif

0_1295240443kRqs.gif

2. 创建一个普通 Java 类

view plain copy to clipboard
  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
  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. }



3. 创建 webService 服务端

0_1295240450IbWw.gif

0_12952404550fTJ.gif

0_12952404603qNv.gif

HelloJaxwsDelegate.java 的源代码如下:

view plain copy to clipboard
  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
  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. }

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

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

0_1295240489xQtQ.gif

0_12952404924Tzd.gif

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

0_1295240465ioHM.gif

0_1295240469oMaL.gif

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

0_1295240474JbbN.gif

0_1295240479FpYO.gif

0_12952404855oPh.gif

7. 创建测试类

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

view plain copy to clipboard
  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
  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. }



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

0_12952405026w0p.gif

8. 运行测试类

运行结果如下图所示:

0_1295240497Jum2.gif


你可能感兴趣的:(java,webservice,开发)