coutoperator 2011-04-23
Web服务也是一种分布式技术,他与EJB最大的不同是Web服务属于行业规范,可以跨平台及语言,而EJB属于java平台的规范,尽管理论上可以跨平台,但是实现起来很复杂,javaEE为webservice提供了两种不同的编程模型,EJB容器模型和Web容器模型,下面是以JAX-WS2.x规范介绍webservice的开发
1.首先编写服务器端代码,新建一个java工程,并将JBoss的client目录下的所有jar包引入到工程
实体类Order如下
package com.lamp.bean; import java.io.Serializable; public class Order implements Serializable{ private static final long serialVersionUID = 9138560163090982706L; private String orderId; private String name; public Order(){} public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
接口类OrderService
package com.lamp.service; import java.util.List; import com.lamp.bean.Order; public interface OrderService { public Order getOrder(String orderId); public List<Order> getOrders(); public String getUsername(String name); }
实现类
package com.lamp.service.impl; import java.util.ArrayList; import java.util.List; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.jws.WebService; import com.lamp.bean.Order; import com.lamp.service.OrderService; @WebService(targetNamespace="http://ws.lamp.com", name="OrderService", serviceName="OrderServiceBean") @Stateless @Remote(OrderService.class) public class OrderServiceBean implements OrderService { public Order getOrder(String orderId) { Order order = new Order(); order.setOrderId(orderId); order.setName("张三"); return order; } public List<Order> getOrders() { List<Order> orders = new ArrayList<Order>(); Order order = new Order(); order.setOrderId("01"); order.setName("李四"); Order order2 = new Order(); order2.setOrderId("02"); order2.setName("王五"); orders.add(order); orders.add(order2); return orders; } public String getUsername(String name) { return name; } }
用工程打包成jar部署到JBoss的部署目录下
接着我们开发一个客户端,新建一个java工程,并在其根目录下新建一个lib文件夹,将JAX-WS的jar文件拷贝到lib文件夹,同时也将其引入到项目的构建路径中
在项目的根目录下新建一个build.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project name="WSClient" default="wsclientgen" basedir="."> <property name="src.dir" value="${basedir}/src"></property> <path id="build.classpath" description="设置类路径"> <fileset dir="${basedir}/lib"> <include name="*.jar"/> </fileset> </path> <target name="wsclientgen" description="生成webservice客户端辅助代码"> <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport" classpathref="build.classpath"/> <wsimport wsdl="http://127.0.0.1:8080/WebService/OrderServiceBean?wsdl" sourcedestdir="${src.dir}" package="com.lamp.ws.client" keep="true" verbose="true" destdir="${basedir}/bin"/> </target> </project>
通过运行ant,我们得到了客户端类
编写一个测试类来测试
package junit.test; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import com.lamp.ws.client.Order; import com.lamp.ws.client.OrderService; import com.lamp.ws.client.OrderServiceBean; public class WSTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void getUsername(){ OrderServiceBean service = new OrderServiceBean(); OrderService orderService = service.getOrderServicePort(); System.out.println(orderService.getUsername("张三")); } @Test public void getOrders(){ OrderServiceBean service = new OrderServiceBean(); OrderService orderService = service.getOrderServicePort(); List<Order> orders = orderService.getOrders(); for(Order order:orders){ System.out.println(order.getName()); } } }
运行测试,我们能在控制台看到我们所需的结果