使用axis1.4实现webservice接口——个人认为对程序员来说非常简单的方式

  1. 下载axis1.4相关jar包,如下:

    链接:http://pan.baidu.com/s/1miF8mOK 密码:cihb

  2. 在myeclipse里新增一个web project项目,目录结构什么的都根据自己的需要设置好。
  3. 在web.xml里添加axis1.4相关配置,如下: 

 1 <listener>
 2 
 3                    <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
 4 
 5          </listener>
 6 
 7         
 8 
 9          <servlet>
10 
11                    <servlet-name>AxisServlet</servlet-name>
12 
13                    <display-name>Apache-Axis Servlet</display-name>
14 
15                    <servlet-class>
16 
17                             org.apache.axis.transport.http.AxisServlet
18 
19                    </servlet-class>              
20 
21                    <load-on-startup>2</load-on-startup>
22 
23          </servlet>
24 
25         
26 
27          <servlet-mapping>
28 
29                    <servlet-name>AxisServlet</servlet-name>
30 
31                    <url-pattern>/servlet/AxisServlet</url-pattern>
32 
33          </servlet-mapping>
34 
35  
36 
37          <servlet-mapping>
38 
39                    <servlet-name>AxisServlet</servlet-name>
40 
41                    <url-pattern>/services/*</url-pattern>
42 
43          </servlet-mapping>
View Code

 

     4.  在WEB-INF目录下添加文件 server-config.wsdd(该文件可以有deploy生成,也可以手动编辑,这里手动编辑):

 

 1 <deployment xmlns="http://xml.apache.org/axis/wsdd/"
 2     xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 3 
 4     <handler type="java:org.apache.axis.handlers.http.URLMapper"
 5         name="URLMapper" />
 6     <!--  -->
 7   <service name="demoServer" provider="java:RPC" style="wrapped" use="literal">
 8        <parameter name="wsdlTargetNamespace" value="http://cn.com.demoServer"/>  
 9        <parameter name="wsdlServiceElement" value="exUmService"/>  
10        <parameter name="schemaQualified" value="http://cn.com.demoServer"/>  
11        <parameter name="wsdlServicePort" value="exUmServiceHttpPort"/>  
12        <parameter name="wsdlPortType" value="exUmServicePortType"/>  
13        <parameter name="typeMappingVersion" value="1.2"/>  
14         
15     <parameter name="className" 
16             value="com.demo.webservice.service.impl.AxisDemoWebserviceImp" />
17         
18     <operation name="testWebsServiceTwo" qname="operNS:testWebsServiceTwo" 
19            xmlns:operNS="http://cn.com.demoServer" 
20            returnQName="retNS:out" 
21            xmlns:retNS="http://cn.com.demoServer" 
22            returnType="rtns:ResponseBeanWS" 
23            xmlns:rtns="http://cn.com.demoServer" soapAction="" >  
24            
25           <parameter qname="pns:RequestBeanWS" xmlns:pns="http://cn.com.demoServer" 
26           type="tns:RequestBeanWS" xmlns:tns="http://cn.com.demoServer"/>  
27         </operation>
28 
29     <parameter name="allowedMethods" value="testWebsServiceTwo" />
30         <parameter name="scope" value="request" />
31         
32         <typeMapping  
33         xmlns:ns="http://cn.com.demoServer"  
34         qname="ns:CxBBSlsRelaBean"  
35         type="java:com.demo.webservice.bean.CxBBSlsRelaBean"  
36         serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"  
37         deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"  
38         encodingStyle=""  
39       />
40       <typeMapping  
41         xmlns:ns="http://cn.com.demoServer"  
42         qname="ns:RequestBeanWS"  
43         type="java:com.demo.webservice.bean.RequestBeanWS"  
44         serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"  
45         deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"  
46         encodingStyle=""  
47       />  
48       <typeMapping  
49         xmlns:ns="http://cn.com.demoServer"  
50         qname="ns:ResponseBeanWS"  
51         type="java:com.demo.webservice.bean.ResponseBeanWS"  
52         serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"  
53         deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"  
54         encodingStyle=""  
55       />
56     </service>    
57     
58     <transport name="http">
59         <requestFlow>
60             <handler type="URLMapper" />
61         </requestFlow>
62     </transport>
63 </deployment>
View Code

 

以上是有复杂bean参数的配置信息,若接口参数是简单的类型(String int等org.apache.axis.encoding.XMLType.XSD定义的基本类型),配置可以写的很简单,如下:

 1 <deployment xmlns="http://xml.apache.org/axis/wsdd/"
 2     xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 3 
 4     <handler type="java:org.apache.axis.handlers.http.URLMapper"
 5         name="URLMapper" />
 6     <!-- 这里是Webservice的实现类相关配置-->
 7     <service name="AgentInfoServer" provider="java:RPC">
 8         <parameter name="className"
 9             value="com.demo.service.service.impl.AgentInfoServer" />
10         <parameter name="allowedMethods" value="*" />
11         <parameter name="scope" value="request" />
12     </service>
13     <transport name="http">
14         <requestFlow>
15             <handler type="URLMapper" />
16         </requestFlow>
17     </transport>
18 
19 </deployment>
View Code

 

      5.  编写Webservice接口java代码(这里根据业务逻辑编写,如需注入数据库操作的dao等,好像不能直接采用spring注入,但可以手动编写通过工厂模式获取spring配置里的bean的方法实现)

 

      6.  如参数是复杂的bean类型,自己定义的javaBean的class应该实现Serializable可序列化接口,参数尽量不要使用Map,经过种种尝试,Map做参数都不能实现,于是放弃~~~~(>_<)~~~~

 1 public class RequestBeanWS implements Serializable {
 2     /**
 3      * 
 4      */
 5     private static final long serialVersionUID = 1L;
 6 
 7 /**这块是自己定义的需要的字段
 8      * 
 9      */
10 
11 
12 }
View Code

 

 

      7.  接口配置和java代码都写完之后,启动tomcat服务,可以在程序里写一个测试的类,使用call方法来测试,也可以在浏览器里输入对应Webservice接口地址来测试。

 1 package com.demo.webservice.client;
 2 
 3 import java.net.URL;
 4 
 5 import javax.xml.namespace.QName;
 6 import javax.xml.rpc.ParameterMode;
 7 
 8 import org.apache.axis.client.Call;
 9 import org.apache.axis.client.Service;
10 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
11 import org.apache.axis.encoding.ser.BeanSerializerFactory;
12 
13 import com.demo.webservice.bean.RequestBeanWS;
14 import com.demo.webservice.bean.ResponseBeanWS;
15 
16 public class WebserviceTestLocal {
17     
18 //    public static void main(String[] args) {
19 //        Endpoint.publish("http://localhost:8080/ab/service/SayHi", new WebserviceSerImp());
20 //        
21 //    }
22     /**
23      * webservice 接口demo测试 client
24      * @author 
25      *
26      */
27     public static void main(String[] args) {         
28         
29         try {
30 //            String endpoint = "http://localhost:8080/ab/services/webservice";
31                 String endpoint = "http://localhost:8080/webservice/services/demoServer";
32             Service service = new Service();
33             Call call;
34             call = (Call)service.createCall();
35             
36             call.setOperationName(new QName("http://cn.com.demoServer", "testWebsServiceTwo"));
37 
38             call.setTargetEndpointAddress(new URL(endpoint));
39                            
40             QName qn2 = new QName("http://cn.com.demoServer", "RequestBeanWS" );    
41             call.registerTypeMapping(RequestBeanWS.class,qn2,  
42                   new BeanSerializerFactory(RequestBeanWS.class, qn2),  
43                   new BeanDeserializerFactory(RequestBeanWS.class, qn2));  
44                           
45             QName qn3 = new QName("http://cn.com.demoServer", "ResponseBeanWS" );    
46             call.registerTypeMapping(ResponseBeanWS.class,qn3,  
47                   new BeanSerializerFactory(ResponseBeanWS.class, qn3),  
48                   new BeanDeserializerFactory(ResponseBeanWS.class, qn3));
49             
50             call.addParameter( "inputs", qn2, ParameterMode.IN );  
51             call.setReturnClass(ResponseBeanWS.class);
52             
53 
54 //            call.setUseSOAPAction(true);
55             
56             RequestBeanWS input = new RequestBeanWS();
57             input.setUserCode("sunny1");
58             input.setServiceN("sunny2");
59             input.setFunctionN("sunny3");
60             input.setPassword("sunny4");
61             
62             ResponseBeanWS res=(ResponseBeanWS)call.invoke(new Object[]{input});
63             System.out.println(res.toString());
64             } catch (Exception e) {
65                 // TODO Auto-generated catch block
66                 e.printStackTrace();
67             }
68         
69     }
70 
71 }
View Code

 

转载于:https://www.cnblogs.com/sunny20082870/p/5583067.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值