AXIS第二课:工程应用中的AXIS发布方法

工程应用当中的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配置:此配置加在<web-app>节点下。

<!-- AXIS配置 -->
    <listener>
        <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
    </listener>
   
<servlet>
    <servlet-name>AxisServlet</servlet-name>
    <display-name>Apache-Axis Servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.AxisServlet
    </servlet-class>
</servlet>

<servlet>
    <servlet-name>AdminServlet</servlet-name>
    <display-name>Axis Admin Servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.AdminServlet
    </servlet-class>
    <load-on-startup>100</load-on-startup>
</servlet>

<servlet>
    <servlet-name>SOAPMonitorService</servlet-name>
    <display-name>SOAPMonitorService</display-name>
    <servlet-class>
        org.apache.axis.monitor.SOAPMonitorService
    </servlet-class>
    <init-param>
      <param-name>SOAPMonitorPort</param-name>
      <param-value>5001</param-value>
    </init-param>
    <load-on-startup>100</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>SOAPMonitorService</servlet-name>
    <url-pattern>/SOAPMonitor</url-pattern>
</servlet-mapping>

<!-- uncomment this if you want the admin servlet -->
<!--
<servlet-mapping>
    <servlet-name>AdminServlet</servlet-name>
    <url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>
-->

    <session-config>
        <!-- Default to 5 minute session timeouts -->
        <session-timeout>5</session-timeout>
    </session-config>

    <!-- currently the W3C havent settled on a media type for WSDL;
   
http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
    for now we go with the basic 'it's XML' response -->
<mime-mapping>
    <extension>wsdl</extension>
     <mime-type>text/xml</mime-type>
</mime-mapping>

<mime-mapping>
    <extension>xsd</extension>
    <mime-type>text/xml</mime-type>
</mime-mapping>

3,编写wsdd文件

  deploy.wsdd文件,此文件跟上面的类文件放在同一个包下,内容如下:

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="UserEndpoint" provider="java:RPC">
<parameter name="className" value="UserEndpoint"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>

4、发布服务:

 在DOS下转换目录到上面的包目录,执行以下命令:

java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd

你会发现目录下多了一个server-config.wsdd文件,这就是AXIS的配置文件,以后所有的服务发布描述都会在里面找到。(当然你可以不用写deploy.wsdd,而是直接创建并修改它,以后新加入的web service也可以在里面添加而不用执行以上命令)。

server-config.wsdd文件的标准内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<deployment xmlns="
http://xml.apache.org/axis/wsdd/"
xmlns:java="
http://xml.apache.org/axis/wsdd/providers/java">
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<service name="Document" provider="java:RPC">
<parameter name="className" value="com.miracle.dm.service.DocumentEndpoint"/>
<parameter name="allowedMethods" value="*"/>  
       
<beanMapping qname="ns:DocumentInfo" xmlns:ns="urn:beanservice"
             languageSpecificType="java:com.miracle.dm.doc.document.model.DocumentInfo"/>
    <beanMapping qname="ns:DocExtpropInfo" xmlns:ns="urn:beanservice"
             languageSpecificType="java:com.miracle.dm.doc.document.model.DocExtpropInfo"/>
            
    <beanMapping qname="ns:DocAttachmentInfo" xmlns:ns="urn:beanservice"
             languageSpecificType="java:com.miracle.dm.doc.document.model.DocAttachmentInfo"/>
            
    <beanMapping qname="ns:DoccatRelatInfo" xmlns:ns="urn:beanservice"
             languageSpecificType="java:com.miracle.dm.doc.document.model.DoccatRelatInfo"/>
            
    <typeMapping qname="ns:DataHandler" xmlns:ns="urn:beanservice"
          languageSpecificType="java:javax.activation.DataHandler"
          serializer="org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory"
          deserializer="org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory"
          encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"/>
                  
</service>

<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>

</deployment>

其中的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_("
[email protected]");
        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_("
[email protected]");
        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文件里面加入你的业务代码了

你可能感兴趣的:(java,apache,应用服务器,Web,servlet)