AXIS第二课:工程应用中的AXIS的发布方法Stub
工程应用当中的web service的参数和通回值通常都是一个数据Bean类,因此前面介绍的两种发布AXIS的web service方法在工程应用当中并不多见,下面介绍Stub发布方法,开发步骤如下: 1,编写服务端程序UserEndpoint.java import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.springframework.remoting.jaxrpc.ServletEndpointSupport; import com.miracle.dm.framework.organization.dao.hibernate.UserHibernateDAO; import com.miracle.dm.framework.organization.model.User; public class UserEndpoint extends ServletEndpointSupport{ protected Logger logger = LogManager.getLogger(UserEndpoint.class); private UserHibernateDAO userHibernateDAO; protected void onInit(){ userHibernateDAO = (UserHibernateDAO)getWebApplicationContext() .getBean("userHibernateDAO"); } /** * 增加User到数据库 * * @return 返回新增加的数据的ID,失败返回字符串“FAILED” */ public String addUser(User user){ try{ String rev = userHibernateDAO.addUser(user); return rev; }catch(Exception e){ logger.error("use web service to add user failed , user Name is : " + user.getName_()); return "FAILED"; } } /** * 从数据库删除User * * @return 删除成功返回字符串“OK” ,失败返回字符串“FAILED” */ public String delUser(String userId){ try{ userHibernateDAO.delUser(userId); return "OK"; }catch(Exception e){ logger.error("use web service to delete user failed , user Id is : " + userId); return "FAILED"; } } /** * 将参数中的数据更新到数据库 * * @return 成功返回字符串“OK”,失败返回字符串“FAILED” */ public String updateUser(User user){ try{ userHibernateDAO.updateUser(user); return "OK"; }catch(Exception e){ logger.error("use web service to update user failed , user Id is : " + user.getID_()); return "FAILED"; } } } 2,将Axis集成到工程当中 在工程的web.xml配置文件中加入以下AXIS配置:此配置加在节点下。 org.apache.axis.transport.http.AxisHTTPSessionListener AxisServlet Apache-Axis Servlet org.apache.axis.transport.http.AxisServlet AdminServlet Axis Admin Servlet org.apache.axis.transport.http.AdminServlet 100 SOAPMonitorService SOAPMonitorService org.apache.axis.monitor.SOAPMonitorService SOAPMonitorPort 5001 100 AxisServlet /servlet/AxisServlet AxisServlet *.jws AxisServlet /services/* SOAPMonitorService /SOAPMonitor 5 wsdl text/xml xsd text/xml 3,编写wsdd文件 deploy.wsdd文件,此文件跟上面的类文件放在同一个包下,内容如下: 4、发布服务: 在DOS下转换目录到上面的包目录,执行以下命令: java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd 你会发现目录下多了一个server-config.wsdd文件,这就是AXIS的配置文件,以后所有的服务发布描述都会在里面找到。(当然你可以不用写deploy.wsdd,而是直接创建并修改它,以后新加入的web service也可以在里面添加而不用执行以上命令)。 server-config.wsdd文件的标准内容如下: 其中的beanmapping节点都是在此服务中应用到的Bean的配置。 此时你访问http://localhost:8080/工程名/services就可以看到此工程发布的所有的web service 5,生成客户端client stub文件 在浏览器上访问服务器端的服务,可以下载到User.wsdl文件,保存到E盘,通过Axis的相关工具,可以自动从WSDL文件中生成Web Service的客户端代码。 编写一个WSDL2Java.bat文件,其内容如下: set Axis_Lib=D:\software\Axis\axis-bin-1_4\axis-1_4\lib set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% set Output_Path=D:\project\MiracleDataManager\src set Package=com.miracle.dm.service.client %Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% User.wsdl cmd 注意,.bat文件中的路径不能有空格和中文字符,执行这个批处理文件就可以生成client stub. 生成的stub client文件列表为:GenericValue.java,User.java,UserEndpoint.java,UserEndpointService.java,UserEndpointServiceLocator.java,UserSoapBindingStub.java 5,客户端调用例子 这里我使用Junit来测试: package com.miracle.dm.service.test; import com.miracle.dm.service.client.User; import com.miracle.dm.service.client.UserEndpoint; import com.miracle.dm.service.client.UserEndpointService; import com.miracle.dm.service.client.UserEndpointServiceLocator; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; public class TestUserClient extends TestCase { public TestUserClient(String string) { super(string); } public void addUser() throws Exception { UserEndpointService service = new UserEndpointServiceLocator(); UserEndpoint client = service.getUser(); User user = new User(); user.setClass_("U"); user.setName_("annlee"); user.setEmail_("1@163.com"); user.setSeq_(65546); user.setPassword_("password"); user.setEnabled_("Y"); user.setDisplayname_("李飞虎"); user.setDomain("100"); user.setMobile("12345"); String retValue = client.addUser(user); System.out.println(retValue); } public void updateUser() throws Exception{ UserEndpointService service = new UserEndpointServiceLocator(); UserEndpoint client = service.getUser(); User user = new User(); user.setID_("402880ea1b447d08011b447e3e880002"); user.setClass_("U"); user.setName_("annlee"); user.setEmail_("345@163.com"); user.setSeq_(65546); user.setPassword_("password"); user.setEnabled_("Y"); user.setDisplayname_("李飞虎"); user.setDomain("100"); user.setMobile("12345"); String retValue = client.updateUser(user); System.out.println(retValue); } public void delUser() throws Exception{ UserEndpointService service = new UserEndpointServiceLocator(); UserEndpoint client = service.getUser(); String retValue = client.delUser("402880ea1b447d08011b447e3e880002"); System.out.println(retValue); } public static Test suite() { TestSuite suite = new TestSuite(); // suite.addTest(new TestUserClient("addUser")); // suite.addTest(new TestUserClient("updateUser")); suite.addTest(new TestUserClient("delUser")); return suite; } } 至此,整个服务器端和客户端的Web Service框架代码就完成了,剩下的就是在UserEndpoint.java文件里面加入你的业务代码了。