首页 > 代码库 > MyEclipse开发WebService教程
MyEclipse开发WebService教程
. 创建一个 webService 工程。
2. 创建一个普通 Java 类
view plaincopy to clipboard
package com.hmw.jaxws;
/**
* 提供 WebService 服务的测试类<br>
* <b>注意:</b>该类必须有一个无参构造方法,否则无法使用 MyEclipse 的向导为此类创建 jax-ws 的 webService 服务端类<br>
* <b>创建日期</b>:2011-1-17
* @author <a href="mailto:hemingwang0902@126.com" mce_href="mailto:hemingwang0902@126.com">何明旺</a>
*/
public class HelloJaxws {
public String sayHello(String userName) {
return userName + ",您好,这是我的第一个 jax-ws 程序。";
}
public int add(int i, int j) {
return i + j;
}
}
[java] view plaincopy
package com.hmw.jaxws;
/**
* 提供 WebService 服务的测试类<br>
* <b>注意:</b>该类必须有一个无参构造方法,否则无法使用 MyEclipse 的向导为此类创建 jax-ws 的 webService 服务端类<br>
* <b>创建日期</b>:2011-1-17
* @author <a href="mailto:hemingwang0902@126.com" mce_href="mailto:hemingwang0902@126.com">何明旺</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 plaincopy to clipboard
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);
}
}
[java] view plaincopy
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 plaincopy to clipboard
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));
}
}
[java] view plaincopy
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. 运行测试类
运行结果如下图所示:
MyEclipse开发WebService教程