基于Axis2的自动化可配置的Web Service开发

1 篇文章 0 订阅
1 篇文章 0 订阅

 

 

基于Axis2Tomcat的自动化可配置Web Service开发

 

 

目录

一、背景介绍...3

二、自动创建流程...3

1. Web Service Request.3

2. Web Service名称获取... 3

3. Web Service代码生成... 3

4. Web Service代码编译... 4

5. Web Service调用... 4

三、开发准备...4

1. Install Tomcat.4

2. Install Axis2.5

3. Tomcat设定...5

4. Axis2设定...5

四、Web Service配置文件...6

1. Simple Service Properties.6

2. Service Header.7

3. RequestParameters.7

4. ServiceOperations.7

5. ResponseOutput.8

五、Service代码开发...9

六、Web Service开发实例...9

1. simpleService.properties.10

2. login.xml10

3. beeCommission.xml13

4. javaMethod.xml14

七、注意事项...16

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

一、背景介绍

         为了加快Tomcat环境下基于Axis2引擎的WebService开发,将开发Web Service的处理如参数解析,方法调用和结果返回进行了封装,生成一个Axis2Service.jar包,实现了Web Service的输入,处理和输出的可配置,简化了WebService的开发工作,并使用Axis2hotupdatehotdeploy功能,直接从WebServicepropertiesXML配置文件生成WebService代码,自动编译成class发布,简化了WebService的开发,并可部署相应的URL转换Servlet,缩短了WebService的调用URL

二、自动创建流程

1. Web Service Request

    用户或系统Server发出Web Service Request请求,包括Request URL以及相应的参数

例如http://10.139.8.167:8080/TestService/beeCommission?

strXMLParameter=<?xml version='1.0' encoding= 'UTF-8'?><requestversion='1.0'><beeCommissionCommand><Parameters><sessionID>112843584</sessionID><startDate>20130101</startDate><endDate></endDate></Parameters></beeCommissionCommand></request>  

这是XML格式的RequestParameter;

http://10.139.8.167:8080/TestService/javaMethod?userId=leoliao&orderTime=20130326111259

这是String格式的RequestParameter

2. Web Service名称获取

    从服务器上响应上述URLServlet收到的的Request URI中获取Web Service的名称。例如上述Web Service Request中,获得Service Name为:TestService

3. Web Service代码生成

 Web Service的配置文件分为两种:

一种是XML文件,是全配置的Web Service,输入,执行操作和输出都可配置;

另一种是properties文件,是简单配置Web Service,直接将Web Service Mapping到指定的java方法。

    根据Tomcat路径下conf目录中WSConfigure活页夹中所有的web serviceconfigure XML文件及properties文件,依次加载每一个XML文件到WebService对象,加载每一个properties文件到SimpleWebService对象,并生成相应的public java 方法,加上importAxis2Service jar包中的语句,组成Web Servicejava文件,放在Axis2缺省发布class文件作为web service的目录下。

    每个Web Service配置文件都会有Debug的开关,如果所有的Web Service的配置文件中的Debug都是关闭的,则Web Service的自动生成和编译就不会运行。(Web Service Debug完成,正式发布后,应该把所有配置文件中的Debug都关掉。)

例如,上述例子中,读取Web Service XMLconfigure文件的路径为:E:\java\Tomcat7022\conf\WSConfigure

生成的Web Service代码文件为TestService.java,存放路径为:

E:\java\Tomcat7022\webapps\axis2\WEB-INF\pojo

 

4. Web Service代码编译

调用Java runtime,执行javac,编译生成的Web Service java代码,生成class文件,被自动发布为Web Servicejava代码里面的每一个公共方法,都将发布成一个Web Service方法。

5. Web Service调用

   转换短的Web Service调用URL,并处理所有参数,生成新的Web Service URL然后把调用Redirect到新的Web Service URL,完成Web Service调用。

   例如:

http://10.139.8.167:8080/TestService/javaMethod?userId=leoliao&orderTime=20130326111259

将被转换为:

http://10.139.8.167:8080/axis2/services/TestService/javaMethod?userId=leoliao&orderTime=20130326111259

、开发准备

1. Install Tomcat

  Install JDK set System VariableJAVA_HOME

  Install Tomcat  apache-tomcat-7.0.22.exe (Tomcat7.exe)

安装完毕后,在IE中输入http://localhost:8080应能看到如下Tomcat的启动界面

如果无法看到此界面,说明Tomcat安装不成功,请检查相关配置及安装文件并重新安装直至成功。

                             

2. Install Axis2

  Downloadaxis2-1.6.1-bin.zip,unzip,set System variable AXIS2_HOME

  Download axis2.war,copy toTomcat7 webapps

Tomcat会自动解压axis2.war,在webapps目录中生成一个axis2的活页夹,所有axis2 Web Service引擎的应用文件都在这个目录下。

Axis2安装完毕,在IE中输入http://localhost:8080/axis应能看到如下Axis2的主界面:

如果看不到这个界面,说明Axis2安装不成功,请检查相关设置,确保Axis2安装成功。

 

3. Tomcat设定

  取得Axis2Service.jar包,放入tomcatlib目录中。

  tomcatconf目录下的web.xml,增加TestService(要开发的WebService的名称)的servlet注册:

<servlet>

    <servlet-name>TestService</servlet-name>

    <servlet-class>com.foxconn.cmmsg.itss.URLTransfer</servlet-class>

</servlet>

mapping到对所有以TestService开始的URL,也就是TestService所包含的所有API访问路径,都会由这个Servlet来处理:

<servlet-mapping>

        <servlet-name>TestService</servlet-name>

       <url-pattern>/TestService/*</url-pattern>

</servlet-mapping>

4. Axis2设定

  使用Axis2用最简单的缺省POJO方式发布Web Service,避免打包成aar文件再发布的繁琐步骤:

直接把编译好的TestService.class文件放到axis2\WEB-INF\pojo目录中

如果没有pojo目录,则建立该目录;

如果需要使用其他目录名称,请修改axis2\WEB-INF\conf\axis2.xml中:

<deployer extension=".class"directory="pojo"class="org.apache.axis2.deployment.POJODeployer"/>

缺省的pojo为其他的目录名。

  修改axis2\WEB-INF\conf\axis2.xml中:

<parametername="hotdeployment">true</parameter>

启用热发布功能,Tomcat不需要重新启动就可以自动发布Web Service

  修改axis2\WEB-INF\conf\axis2.xml中:

<parameter name="hotupdate">true </parameter>

启用热更新功能,Tomcat不需要重新启动就可以更新已发布成功的Web Service

四、Web Service配置文件

使用Axis2Servic.jar包所包含的框架进行Web Service的开发,最主要的工作就是配置Web ServiceXMLproperties configure文件。这些configure文件缺省路径在Tomcatconf目录下的WSConfigure文件夹:

 

1.Simple Service Properties

Web ServiceProperties configure配置文件比较简单:

例如simpleService.properties内容如下:

sevice.version =1.0  service版本

service.ame=simpleService  service名称

service.debug=true  是否打开debug

service.datasource=hiverService 数据源JNDI名称

service.classfolder=WSClass  加载类文件的活页夹

Mapping2JavaMethod=ServiceImpl.simpleService  service mapping到的java类和公开静态方法名称

Parameters=para1,para2,para3  service的参数列表

Web ServiceXMLconfigure配置文件主要分四部分,依次介绍如下:

2. Service Header

<?xml version="1.0" encoding="UTF-8"?>

<service name="JavaMethod" tag="JavaMethod"version="1.0"

         debug="true"datasource="hiver" classfolder="WSClass">

         <errormessageList>

<errormessage>Success.</errormessage>

            <errormessage>Your accountis locked.</errormessage>

            <errormessage>Fail to pairyour account.</errormessage>

            <errormessage>API version isincorrect.</errormessage>

         </errormessageList>

这里定义了WebService方法的名称,显示的TagAPI版本,是否打开debug,使用的数据源以及如果使用Java方法,引用的javaclass的活页夹。

errormessageList部分可以有一条或多条,是针对此web service定义的error message,顺序位置为对应的errorcode

每个Web Service方法都必须有。

3.RequestParameters

<requestParameters format="String">

                   <requestParameter>

                            <name>userId</name>

                            <tag>userId</tag><!--tag-->

                            <type>text</type><!--type-->

                            <required>Yes</required><!--isRequired-->

                            <mapping>_userid</mapping><!--mappingto field -->

                   </requestParameter>

        ….

这里定义了Web Service方法RequestParameter的格式,可以有XMLJSONSTRING(通过URL QueryString)三种格式;随后是各个RequestParameter的具体定义,可以有0到多个RequestParameter

RequestParameter定义中:

nameparameter的名字,

tagparameterXMLJSONURL中输入时的tag,通常和name保持一致,

typeparameter的类型,可以是textnumericdoublefloatintegerintdigital

requiredparameter是否必须,如果为1,y,t,yestrue,则此parameter不许为空,

mappingparameter作为下一步ServiceOperation输入参数时的参数名称。

4. ServiceOperations

    <serviceOperations name="{call sp_checkSession(?,?,?) }"

                   format="sp">

                   <operationParameter>

                            <name>_userid</name>

                            <mode>IN</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>userId</mapping>

                   </operationParameter>

这里定义了Web Service方法要进行的ServiceOperation,格式可以是SPname就是调用SP的语句,包括sp名称;或者java,则name就是要执行的java方法的包名及类名;如果此java方法有参数,则name@@结尾。

下面ServiceOperations的各个operationParameter定义。可以有0到多个参数:

nameparameter的名字,

modeparameter的模式,INOUTINOUT

typeparameter的类型,因为此类型可能要对应到数据库参数类型,所以有CHAR,VARCHAR,FLOAT,DOUBLE,INTEGER,BIGINT,JAVA方法,可以都填VARCHAR

preprocessparameter的预处理,目前有以下几种:

trim

tolowercase

touppercase

md5

sha1sha-1

base64

decodebase64 decode),

uuid(随机UUID字符串,value为长度,如果value为空,缺省长度为16),

hex(随机16进制字符串,value为长度,如果value为空,缺省长度为16),

random(随机字符串,value为长度,如果value为空,缺省长度为16),

ip

format(?),其中?为一整数,对value取证到?位,

hex(?),其中?为一整数,对value转换到16进制?位。

mappingparameter mappingRequest Parameter的名字,此parameter会取相应的RequestParamter的值;

如果mapping字符串包含.,则为mappingjava方法取值,如果mapping字符串中不包含(…),则为Java方法无参调用;否则,为有参java方法调用,mapping字符串中方法名后括号中的字符串为参数列表,多个参数以@@分隔,参数名 mappingRequestParamter取值。

5.ResponseOutput

<responseOutput format="xml">

         <outputParameter>

                   <name>Success</name>

                   <tag>Success</tag>

                   <mapping>NULL</mapping>

         </outputParameter>

这里定义的是Web Service方法返回的格式,有XMLJSONSTRING,以及参数,可以有1到多个参数,其中参数nameerrorcode的若值为0,表示成功,其余值表示失败:

Outputparameter定义中:

name parameter名,

tagparameter显示在XMLJSONString中的tag,通常和name保持一致,多级tag中间加.,例如<output><time>这种tag,应设定为output.time,

mappingparametermappingServiceParameterRequestParameter的名字,

如果mapping字符串为不含.则为参数取值,NULL表示空值,其余表示mappingService ParameterRequest Parameter取值;

mapping字符串包含.,则为mappingjava方法取值,如果mapping字符串中不包含(…),则为Java方法无参调用;否则,为有参java方法调用,mapping字符串中方法名后括号中的字符串为参数列表,多个参数以@@分隔,参数名 mappingRequestParamterServiceParameter取值。

五、Service代码开发

    由于web service的代码已经根据配置文件自动生成了,所以这里的代码开发实际上指的是为了实现web service的特定功能,需要通过反射机制调用的一些java方法的实现。

    例如,<serviceOperationsname="ServiceImpl.SampleService@@"

                   format="java">

此处定义Web ServiceserviceOperationsformatjava,那么我们就需要实现其name中指定的class及方法,如本例中,要实现ServiceImpl class中的SampleService方法。具体方法及参数定义可见前面Web Service XML Configure文件的解释。

再比如:<outputParameter>

                            <name>sellCountList</name>

                            <tag>NULL</tag>

                            <mapping>ServiceImpl.getBeeSellCountList</mapping>

                   </outputParameter>

此处定义此输出参数mappingServiceImpl类中的getBeeSellCountList方法,那么我们就需要实现ServiceImpl class中的getBeeSellCountList方法。具体方法及参数定义可见前面Web Service XML Configure文件的解释。

要注意的是,定义在指定class中的方法都必须是静态方法,而且参数顺序要和Web Service XML configure中定义一致。同一个class中的方法名及调用参数必须唯一。这些class文件的缺省路径在E:\java\Tomcat7022\conf\WSClass,也可以在web serviceconfigure文件中指定为conf目录下不同的活页夹作为class folder

六、Web Service开发实例

此处给出一个simpleServiceproperties文文件实例和三个实际的Web ServiceXML configure 文件的实例。

1. simpleService.properties

sevice.version=1.0

service.name=simpleService

service.debug=true

service.datasource=hiverService

service.classfolder=WSClass

Mapping2JavaMethod= ServiceImpl.simpleService

Parameters=para1,para2,para3

2. login.xml

<?xml version="1.0" encoding="UTF-8"?>

<service name="login" tag="loginCommand"version="1.0" debug="true" datasource="hiver"classfolder="WSClass">

         <errormessageList>

           <errormessage>Your account is locked.</errormessage>

           <errormessage>Fail to pair your account.</errormessage>

           <errormessage>API version is incorrect.</errormessage>

         </errormessageList>      

<!--request parameter -->

         <requestParametersformat="xml">

                   <requestParameter>

                            <name>name</name>

                            <tag>name</tag><!--tag-->

                            <type>text</type><!--type-->

                            <required>Yes</required><!--isRequired-->

                            <mapping>myaccount</mapping><!--mappingto field -->

                   </requestParameter>

                   <requestParameter>

                            <name>password</name>

                            <tag>password</tag>

                            <type>text</type>

                            <required>Yes</required>

                            <mapping>psw</mapping>

                   </requestParameter>

         </requestParameters>

         <serviceOperations name="{callsp_login(?,?,?,?,?,?,?,?,?,?,?) }"

                   format="sp">

                   <operationParameter>

                            <name>myaccount</name>

                            <mode>IN</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>name</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>psw</name>

                            <mode>IN</mode>

                            <type>varchar</type>

                            <preprocess>md5</preprocess>

                            <mapping>password</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>mysessionid</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>mysessionid</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>myaccountname</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>Name</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>myemail</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>email</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>myphone</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>phone</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>myareas</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>areas</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>myWMD</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>wmd</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>myjoindate</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>joindate</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>myGender</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>Gender</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>errorcode</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>errorcode</mapping>

                   </operationParameter>

         </serviceOperations>

         <responseOutputformat="xml">

                   <outputParameter>

                            <name>sessionID</name>

                            <tag>sessionID</tag>

                            <mapping>mysessionid</mapping>

                   </outputParameter>

                   <outputParameter>

                            <name>userInfo</name>

                            <tag>NULL</tag>

<mapping>com.hiver.service.nda.ServiceUtility.getUserInfo(myaccountname@@myemail@@myphone@@myareas@@myWMD@@myjoindate@@myGender)</mapping>

                   </outputParameter>

         </responseOutput>

</service>

3.beeCommission.xml

<?xml version="1.0" encoding="UTF-8"?>

<service name="beeCommssion"tag="beeCommssionCommand" version="1.0" debug="true"datasource="hiver" classfolder="WSClass">

         <errormessageList>

           <errormessage>Your account is locked.</errormessage>

           <errormessage>Fail to pair your account.</errormessage>

           <errormessage>API version is incorrect.</errormessage>

         </errormessageList>      

         <!--request parameter -->

         <requestParameters format="xml">

                   <requestParameter>

                            <name>SessionID</name>

                            <tag>sessionID</tag><!--tag-->

                            <type>text</type><!--type-->

                            <required>Yes</required><!--isRequired-->

                            <mapping>_sessionid</mapping><!--mappingto field -->

                   </requestParameter>

                   <requestParameter>

                            <name>StartDate</name>

                            <tag>startDate</tag><!--tag-->

                            <type>text</type><!--type-->

                            <required>Yes</required><!--isRequired-->

                            <mapping>StartDate</mapping><!--mappingto field -->

                   </requestParameter>

                   <requestParameter>

                            <name>EndDate</name>

                            <tag>endDate</tag><!--tag-->

                            <type>text</type><!--type-->

                            <required>Yes</required><!--isRequired-->

                            <mapping>EndDate</mapping><!--mappingto field -->

                   </requestParameter>

         </requestParameters>

         <serviceOperations name="{call sp_checkSession(?,?,?) }"

                   format="sp">

                   <operationParameter>

                            <name>_sessionid</name>

                            <mode>IN</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>SessionID</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>beeid</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>beeid</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>errorcode</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>errorcode</mapping>

                   </operationParameter>

         </serviceOperations>

         <responseOutput format="xml">

                   <outputParameter>

                            <name>beeCommission</name>

                            <tag>NULL</tag>

                            <mapping>ServiceImpl.getBeeCommission(beeid@@StartDate@@EndDate)</mapping>

                   </outputParameter>

         </responseOutput>

</service>

4. javaMethod.xml

<?xml version="1.0"encoding="UTF-8"?>

<servicename="JavaMethod" tag="JavaMethod" version="1.0"

         debug="true"datasource="hiver" classfolder="WSClass">

         <errormessageList>

            <errormessage>Your account islocked.</errormessage>

            <errormessage>Fail to pair youraccount.</errormessage>

            <errormessage>API version isincorrect.</errormessage>

         </errormessageList>

 <!--request parameter -->

         <requestParametersformat="String">

                   <requestParameter>

                            <name>userId</name>

                            <tag>userId</tag><!--tag -->

                            <type>text</type><!--type -->

                            <required>Yes</required><!--isRequired -->

                            <mapping>_userid</mapping><!--mapping to field -->

                   </requestParameter>

                   <requestParameter>

                            <name>orderTime</name>

                            <tag>orderTime</tag>

                            <type>text</type>

                            <required>Yes</required>

                            <mapping>_ordertime</mapping>

                   </requestParameter>

         </requestParameters>

         <serviceOperationsname="ServiceImpl.SampleService@@"

                   format="java">

                   <operationParameter>

                            <name>_userid</name>

                            <mode>IN</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>userId</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>_ordertime</name>

                            <mode>IN</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>orderTime</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>errorcode</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>errorcode</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>JavaReturn</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>_userid</mapping>

                   </operationParameter>

                   <operationParameter>

                            <name>JavaTime</name>

                            <mode>OUT</mode>

                            <type>varchar</type>

                            <preprocess>trim</preprocess>

                            <mapping>_ordertime</mapping>

                   </operationParameter>

         </serviceOperations>

         <responseOutputformat="xml">

                   <outputParameter>

                            <name>Success</name>

                            <tag>Success</tag>

                            <mapping>NULL</mapping>

                   </outputParameter>

                   <outputParameter>

                            <name>Return</name>

                            <tag>Out.Return</tag>

                            <mapping>JavaReturn</mapping>

                   </outputParameter>

                   <outputParameter>

                            <name>Time</name>

                            <tag>Out.Time</tag>

                            <mapping>JavaTime</mapping>

                   </outputParameter>

         </responseOutput>

</service>

七、注意事项

1. 数据源加载失败的话,会缺省加载tomcat conf目录下的jdbc_ws.properties

2. 定义在Web Service properties中作为Service mappingXML配置文件中作为parameter mappingJava方法,必须是publicstatic,返回字符串;

3. 定义在XML配置文件中作为Service Operation mappingjava方法,七返回类型为List<String>,其值依次为各个Service Operation输出参数的取值。

4. Tomcat conf目录下WSConfigure活页夹中的所有propertiesxml文件,都会被当成service配置文件而加载,只要其中有一个Debugtrue,整个Service就会重新生成,编译和自动部署发布。当系统正式运行后,可以把所有的service中的Debug配置的改为false,以提高Web Service的访问效率。

 

全部代码及测试工程可到我的资源中下载。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dumbbellyang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值