使用JBoss、EJB3.0、开发WebService和客户端:
1、 首先我们先建立Webservice:
第一步:首先建立一个普通的Java项目并且导入G:\javaEEServers\jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\client下的所有*.jar文件
第二步:建立EJB Bean接口如下:
public interface OrderService {
public String getUserName(String name);
public Order getOrder(String orderid);
public List<Order> getOrders();
}
第三步:建立实现这个EJB Bean接口实现类
@Stateless //这里指定Bean的会话状态
@Remote(OrderService.class)//远程接口
//修改WebService中的一些属性方法如下
@WebService(targetNamespace="htttp://www.liyong.com"//名字空间
,name="OrderService"//portType 的name
,serviceName="OrderServiceBean")//修改服务的名称
public class OrderServiceBean implements OrderService {
//需要把某个方法发布为Webservice在其前面加上@WebMethod其他没有加的默认不发布问Webservice方法
@Override
public Order getOrder(String orderid) {
//这里只是模拟数据,可从数据库中得到数据
Order order=new Order();
order.setOrderid(orderid);
order.setName("小李");
return order;
}
@Override
public List<Order> getOrders() {
List<Order> orders=new ArrayList<Order>();
Order order1=new Order();
order1.setOrderid("111");
order1.setName("小张");
Order order2=new Order();
order2.setOrderid("222");
order2.setName("小王");
orders.add(order1);
orders.add(order2);
return orders;
}
@Override
public String getUserName(String name) {
return name;
}
}
第四步:使用ANT来编译、发布我们的EJB Bean build.xml
<?xml version="1.0"?>
<!-- ======================================================================= -->
<!-- EJB3 HelloWorld build file -->
<!-- ======================================================================= -->
<project name="WebService" default="ejbjar" basedir=".">
<property environment="env" />
<property name="app.dir" value="${basedir}" />
<property name="src.dir" value="${app.dir}" />
<property name="jboss.home" value="${env.JBOSS_HOME}" />
<property name="jboss.server.config" value="default" />
<property name="build.dir" value="${app.dir}\build" />
<property name="build.classes.dir" value="${build.dir}\classes" />
<!-- Build classpath -->
<path id="build.classpath">
<fileset dir="${jboss.home}\client">
<include name="*.jar" />
</fileset>
<pathelement location="${build.classes.dir}" />
</path>
<!-- =================================================================== -->
<!-- Prepares the build directory -->
<!-- =================================================================== -->
<target name="prepare" depends="clean">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes.dir}" />
</target>
<!-- =================================================================== -->
<!-- Compiles the source code -->
<!-- =================================================================== -->
<target name="compile" depends="prepare" description="编绎">
<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="com/**">
<classpath refid="build.classpath" />
</javac>
</target>
<target name="ejbjar" depends="compile" description="创建EJB发布包">
<jar jarfile="${app.dir}\HelloWorld.jar">
<fileset dir="${build.classes.dir}">
<include name="com/**/*.class" />
</fileset>
</jar>
</target>
<target name="deploy" depends="ejbjar">
<copy file="${app.dir}\HelloWorld.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" />
</target>
<!-- =================================================================== -->
<!-- Cleans up generated stuff -->
<!-- =================================================================== -->
<target name="clean">
<delete dir="${build.dir}" />
<delete file="${jboss.home}\server\${jboss.server.config}\deploy\HelloWorld.jar" />
</target>
</project>
第五步:发布我们的EJB Bean --->之前开启JBoss服务
第六步:编写我们的WebServiceClient
我们使用wsimport工具的Ant任务类生成辅助类 build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="WebServiceClient" 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
//这里的"http://...."可以再JBoss客户端得到
//http://localhost:8080/jbossws/services
wsdl="http://127.0.0.1:8080/HelloWorld/OrderServiceBean?wsdl"
sourcedestdir="${src.dir}" package="com.foshanshop.ws.client" keep="true"
verbose="true" destdir="${basedir}/bin"/>
</target>
</project>
第七步:编译生成辅助类 这是可以看到我们的Src目录中生成了许多文件
第八步:
我们编写单元测试来调用Webservice的代码
public class Test {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@org.junit.Test
public void getOrder()
{
OrderServiceBean orderServiceBean =new OrderServiceBean();
OrderService order=orderServiceBean.getOrderServicePort();
Order order2=order.getOrder("333");
System.out.println(order2.getOrderid()+order2.getName());
}
}
标签:EJB3.0,WEB,WebService,public,new,order,order2,Order,String From: https://blog.51cto.com/u_16091571/6231962