随笔-109  评论-187  文章-25  trackbacks-0
最近复习了一下AXIS,写了2个小例子


1:  bean
 1package test.soap.vo;
 2
 3import java.io.Serializable;
 4
 5public class User implements Serializable {
 6
 7    /**
 8     * 
 9     */

10    private static final long serialVersionUID = -9043747149109792411L;
11
12    private String name;
13    private String password;
14    public String getName() {
15        return name;
16    }

17    public void setName(String name) {
18        this.name = name;
19    }

20    public String getPassword() {
21        return password;
22    }

23    public void setPassword(String password) {
24        this.password = password;
25    }

26    public String toString() {
27        return new StringBuffer().append(name+":"+password).toString();
28    }

29    
30}

31

2:service
 1package test.soap.service;
 2
 3import test.soap.vo.User;
 4
 5public class UserService {
 6
 7    public User user = null;
 8
 9    public UserService() {
10        super();
11        
12    }

13
14    public String getUser(User u) {
15        System.out.println("user.name=" + user.getName());
16        System.out.println("user.password=" + user.getPassword());
17        return "000000000000000000000";
18
19    }

20    public String getUser(){
21        System.out.println("get user no arg");
22        return "abc";
23    }

24    public String getUser(String i){
25        System.out.println(i);
26        return "123";
27    }

28
29}

30

client 调用代码:
 1        String wsdlURL = "http://localhost:8080/axis/services/soapObjSvc?wsdl";
 2        String nameSpaceUri ="http://localhost:8080/axis/services/soapObjSvc";
 3        org.apache.axis.client.Service  service = new org.apache.axis.client.Service();
 4        Call     call    = (Call) service.createCall();
 5        QName    qn      = new QName("urn:BeanService""User");
 6        User u = new User();
 7        u.setName("111111111111");
 8        u.setPassword("22222222222");
 9        call.registerTypeMapping(User.class, qn,
10                          new org.apache.axis.encoding.ser.BeanSerializerFactory(User.class, qn),        
11                          new org.apache.axis.encoding.ser.BeanDeserializerFactory(User.class, qn));        
12            String result;
13            try {
14                call.setOperationName( new QName(nameSpaceUri, "getUser") );
15                call.setTargetEndpointAddress( new URL(wsdlURL));
16                
17//                call.addParameter( "u", qn, ParameterMode.IN );
18//                call.setReturnType( org.apache.axis.encoding.XMLType.XSD_STRING );
19
20//本行程序能够调用,传递的是string参数                
21                result = (String) call.invoke( new Object[] {"abcdefg"} );
22//本行对象调用不通过,传递的是USER对象              
23                result = (String) call.invoke( new Object[] {u} );
24            }
 catch (Exception e) {
25                e.printStackTrace();
26                result = "1Error : " + e;
27                
28            }

29            
30            System.out.println(result);
31

4:server-config.wsdd
 1<?xml version="1.0" encoding="UTF-8"?>
 2<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 3 <globalConfiguration>
 4  <parameter name="adminPassword" value="admin"/>
 5  <parameter name="enableNamespacePrefixOptimization" value="true"/>
 6  <parameter name="disablePrettyXML" value="true"/>
 7  <parameter name="sendXsiTypes" value="true"/>
 8  <parameter name="sendMultiRefs" value="true"/>
 9  <parameter name="sendXMLDeclaration" value="true"/>
10  <requestFlow>
11   <handler type="java:org.apache.axis.handlers.JWSHandler">
12    <parameter name="scope" value="session"/>
13   </handler>
14   <handler type="java:org.apache.axis.handlers.JWSHandler">
15    <parameter name="scope" value="request"/>
16    <parameter name="extension" value=".jwr"/>
17   </handler>
18  </requestFlow>
19 </globalConfiguration>
20 <handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler">
21  <parameter name="wsdlURL" value="/axis/SOAPMonitorService-impl.wsdl"/>
22  <parameter name="serviceName" value="SOAPMonitorService"/>
23  <parameter name="namespace" value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl"/>
24  <parameter name="portName" value="Demo"/>
25 </handler>
26 <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
27 <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
28 <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
29 <service name="AdminService" provider="java:MSG">
30  <parameter name="allowedMethods" value="AdminService"/>
31  <parameter name="enableRemoteAdmin" value="false"/>
32  <parameter name="className" value="org.apache.axis.utils.Admin"/>
33  <namespace>http://xml.apache.org/axis/wsdd/</namespace>
34 </service>
35 <service name="Version" provider="java:RPC">
36  <parameter name="allowedMethods" value="getVersion"/>
37  <parameter name="className" value="org.apache.axis.Version"/>
38 </service>
39 <service name="SOAPMonitorService" provider="java:RPC">
40  <parameter name="allowedMethods" value="publishMessage"/>
41  <parameter name="scope" value="Application"/>
42  <parameter name="className" value="org.apache.axis.monitor.SOAPMonitorService"/>
43 </service>
44 <service name="soapObjSvc" provider="java:RPC">
45  <parameter name="allowedRoles" value="*"/>
46  <parameter name="allowedMethods" value="*"/>
47  <parameter name="className" value="test.soap.service.UserService"/>
48  <beanMapping languageSpecificType="java:test.soap.vo.User" qname="ns1:User" xmlns:ns1="urn:BeanService"/>
49 </service>
50 <transport name="http">
51  <requestFlow>
52   <handler type="URLMapper"/>
53  </requestFlow>
54  <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
55  <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
56  <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
57  <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
58  <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
59  <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
60 </transport>
61 <transport name="local">
62  <responseFlow>
63   <handler type="LocalResponder"/>
64  </responseFlow>
65 </transport>
66</deployment>
67


5:wsdl
 1  <?xml version="1.0" encoding="UTF-8" ?> 
 2<wsdl:definitions targetNamespace="http://localhost:8080/axis/services/soapObjSvc" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axis/services/soapObjSvc" xmlns:intf="http://localhost:8080/axis/services/soapObjSvc" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="urn:BeanService" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 3<!-- 
 4WSDL created by Apache Axis version: 1.3
 5Built on Oct 05, 2005 (05:23:37 EDT)
 6
 7  --> 
 8<wsdl:types>
 9<schema targetNamespace="urn:BeanService" xmlns="http://www.w3.org/2001/XMLSchema">
10  <import namespace="http://schemas.xmlsoap.org/soap/encoding/" /> 
11<complexType name="User">
12<sequence>
13  <element name="name" nillable="true" type="soapenc:string" /> 
14  <element name="password" nillable="true" type="soapenc:string" /> 
15  </sequence>
16  </complexType>
17  </schema>
18  </wsdl:types>
19<wsdl:message name="getUserResponse">
20  <wsdl:part name="getUserReturn" type="soapenc:string" /> 
21  </wsdl:message>
22<wsdl:message name="getUserResponse2">
23  <wsdl:part name="getUserReturn" type="soapenc:string" /> 
24  </wsdl:message>
25<wsdl:message name="getUserRequest2">
26  <wsdl:part name="u" type="tns1:User" /> 
27  </wsdl:message>
28<wsdl:message name="getUserResponse1">
29  <wsdl:part name="getUserReturn" type="soapenc:string" /> 
30  </wsdl:message>
31  <wsdl:message name="getUserRequest" /> 
32<wsdl:message name="getUserRequest1">
33  <wsdl:part name="i" type="soapenc:string" /> 
34  </wsdl:message>
35<wsdl:portType name="UserService">
36<wsdl:operation name="getUser">
37  <wsdl:input message="impl:getUserRequest" name="getUserRequest" /> 
38  <wsdl:output message="impl:getUserResponse" name="getUserResponse" /> 
39  </wsdl:operation>
40<wsdl:operation name="getUser" parameterOrder="i">
41  <wsdl:input message="impl:getUserRequest1" name="getUserRequest1" /> 
42  <wsdl:output message="impl:getUserResponse1" name="getUserResponse1" /> 
43  </wsdl:operation>
44<wsdl:operation name="getUser" parameterOrder="u">
45  <wsdl:input message="impl:getUserRequest2" name="getUserRequest2" /> 
46  <wsdl:output message="impl:getUserResponse2" name="getUserResponse2" /> 
47  </wsdl:operation>
48  </wsdl:portType>
49<wsdl:binding name="soapObjSvcSoapBinding" type="impl:UserService">
50  <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
51<wsdl:operation name="getUser">
52  <wsdlsoap:operation soapAction="" /> 
53<wsdl:input name="getUserRequest">
54  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://service.soap.test" use="encoded" /> 
55  </wsdl:input>
56<wsdl:output name="getUserResponse">
57  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/services/soapObjSvc" use="encoded" /> 
58  </wsdl:output>
59  </wsdl:operation>
60<wsdl:operation name="getUser">
61  <wsdlsoap:operation soapAction="" /> 
62<wsdl:input name="getUserRequest1">
63  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://service.soap.test" use="encoded" /> 
64  </wsdl:input>
65<wsdl:output name="getUserResponse1">
66  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/services/soapObjSvc" use="encoded" /> 
67  </wsdl:output>
68  </wsdl:operation>
69<wsdl:operation name="getUser">
70  <wsdlsoap:operation soapAction="" /> 
71<wsdl:input name="getUserRequest2">
72  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://service.soap.test" use="encoded" /> 
73  </wsdl:input>
74<wsdl:output name="getUserResponse2">
75  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/services/soapObjSvc" use="encoded" /> 
76  </wsdl:output>
77  </wsdl:operation>
78  </wsdl:binding>
79<wsdl:service name="UserServiceService">
80<wsdl:port binding="impl:soapObjSvcSoapBinding" name="soapObjSvc">
81  <wsdlsoap:address location="http://localhost:8080/axis/services/soapObjSvc" /> 
82  </wsdl:port>
83  </wsdl:service>
84  </wsdl:definitions>


<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" mlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getUserByU soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://10.200.7.104:8080/axis/services/soapObjSvc"
<ns1:arg0 href="#id0"/>
</ns1:getUserByU>
<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:User" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:BeanService">
<name xsi:type="soapenc:string">1111111111</name>
<password xsi:type="soapenc:string">22222222222</password>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:getUserByUResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://10.200.7.104:8080/axis/services/soapObjSvc"><getUserByUReturn xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">abcs</getUserByUReturn></ns1:getUserByUResponse></soapenv:Body></soapenv:Envelope>



其实自定义负责对象 与返回复杂对象使用基本一样的,一定要regist,另外WSDD一定要声明一一下

//  call.addParameter("user",qn,ParameterMode.IN);
//  call.setReturnType(qn,java.lang.String.class);
下面这2句也会有些用处.
以上例子在WEBLOGIC和TOMCAT上都测试过了,没有什么问题

posted on 2007-06-15 15:48 小小程序程序员混口饭吃 阅读(2041) 评论(0)  编辑  收藏 所属分类: java