WebService 之 EJB容器模型
---WEB服务
WEB服务市一种分布式技术,它与EJB最大的不同是,WEB服务属于行业规范,可以跨平台及语言.而EJB属于java平台规范.理论上可以跨平台,但是实现起来比较复杂.所以其应用范围局限了java平台.看上去是竞争关系,其实不是WEB服务:偏重这个系统对外提供什么功能.EJB :偏重如何使用这个组件组装这些功能.JavaEE 为web service 提供了两种不同的编程模型.EJB容器模型 web容器模型 下面用最新的JAX-WS 2.2.7 (JAVA API for XML-base WEB Service )来介绍EJB 容器模型的 webservice开发
package com.sh.bean;import java.io.Serializable;public class Order implements Serializable{private static final long serialVersionUID = 7401607038300453322L;private String orderid;private String name;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;}}
package com.sh.service;import java.util.List;import com.sh.bean.Order;public interface OrderService {public String getUserName(String name);public Order getOrder(String orderid);public List<Order> getOrders();}
package com.sh.service.impl;import java.util.ArrayList;import java.util.List;import javax.ejb.Remote;import javax.ejb.Stateless;import javax.jws.WebMethod;import javax.jws.WebService;import com.sh.bean.Order;import com.sh.service.OrderService;//发布为 webservice 服务@WebService(targetNamespace="http://ws.sh.cn",//namespacename="OrderService", //protype nameserviceName="OrderServiceBean" //Service Name)@Stateless@Remote(OrderService.class)public class OrderServiceBean implements OrderService {//@WebMethod //标注后 会将此方法 暴露给webservice@Overridepublic String getUserName(String name) {// TODO Auto-generated method stubreturn name;}@Overridepublic Order getOrder(String orderid) {Order order =new Order();order.setOrderid(orderid);order.setName("小燕子");return order;}@Overridepublic List<Order> getOrders() {// TODO Auto-generated method stubList<Order> person=new ArrayList<Order>();Order order1 =new Order();order1.setOrderid("001");order1.setName("小2");Order order2 =new Order();order2.setOrderid("002");order2.setName("小3");person.add(order1);person.add(order2);return person;}}
<?xml version="1.0" encoding="UTF-8"?><!-- ====================================================================== 2013-2-25 下午1:53:33 project description Bin ====================================================================== --><project name="WebService" basedir="."> <description> description </description><!-- 设置项目原目录 --><property name="src.dir" value="${basedir}\src" /><!-- 获取环境变量 --><property environment="env"/><property name="jboss.home" value="${env.JBOSS_HOME}"/><property name="jboss.server.config" value="default"/><property name="build.dir" value="${basedir}\build"/> <!-- 引入 jboss client 下的 所有jar --><path id="build.classpath"><fileset dir="${jboss.home}\client"><include name="*.jar"/></fileset><!-- 讲编译过后的路径加入到 path中去 方便 接口和实现的引用 --><pathelement location="${build.dir}"/></path> <target name="prepare" description="创建build目录"><delete dir="${build.dir}"/> <mkdir dir="${build.dir}"/> </target> <!-- - - - - - - - - - - - - - - - - - target: compile - - - - - - - - - - - - - - - - - --> <target name="compile" depends="prepare" description="编译"> <javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" includes="com/**"> <classpath refid="build.classpath"/> </javac> </target><!-- ================================= target: ejbjar ================================= --> <target name="ejbjar" depends="compile" description="创建EJB发布包"> <jar jarfile="${basedir}\${ant.project.name}.jar"> <fileset dir="${build.dir}"> <include name="**/*.class"/> </fileset> </jar> </target><!-- ================================= target: deploy ================================= --> <target name="deploy" depends="ejbjar" description="发布EJB"> <copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy"/> </target><!-- - - - - - - - - - - - - - - - - - target: undeploy - - - - - - - - - - - - - - - - - --> <target name="undeploy" description="卸载EJB"> <delete file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar"/> </target></project>
<?xml version="1.0" encoding="UTF-8"?><project name="WSClient" default="wsclientgen" basedir="."><property name="src.dir" value="${basedir}/src"/><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://localhost:8989/WebService/OrderServiceBean/OrderService?wsdl"sourcedestdir="${src.dir}" package="com.sh.ws.client" keep="true"verbose="true" destdir="${basedir}/bin" xendorsed="true"/></target></project>
package junit.test;import java.util.List;import org.junit.BeforeClass;import org.junit.Test;import com.sh.ws.client.Order;import com.sh.ws.client.OrderService;import com.sh.ws.client.OrderServiceBean;public class WSTest {@Testpublic void getUserName() {//通过 发布的 服务对象bean 和 端口 来得到 服务接口类 //和 JAVA中 接口的实例化 需要 通过实现类来 初始化 OrderServiceBean serice=new OrderServiceBean();//接口 OrderService orderService=serice.getOrderServicePort();//调用方法System.out.println(orderService.getUserName("西游降魔"));}@Testpublic void getOrder() {OrderServiceBean serice=new OrderServiceBean();OrderService orderService=serice.getOrderServicePort();Order order =orderService.getOrder("999");System.out.println(order.getOrderid()+"\"+order.getName());}@Testpublic void getOrders() {OrderServiceBean serice=new OrderServiceBean();OrderService orderService=serice.getOrderServicePort();List<Order> orders=orderService.getOrders();for (Order order2 : orders) {System.out.println(order2.getOrderid()+"\"+order2.getName());}}}