axis1传递简单对象,对象内只有基本类型,如:String,int等
1.创建一个javaBean,类名为User。
package com.devtek;
public class User {
private String name="llc";
private int age = 25;
public String toString() {
return "name " + name + " age " + age ;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2. 服务端编写。
package com.devtek;
public class ServiceServer {
// 参数和返回值不包含对象
public String getName(String name) {
if (name == null) {
name = "";
}
return "你好" + name + ",欢迎来到Web服务的世界!";
}
public String getUserName(User user) {
return user.getName();
}
public User getUser(String name, String password) {
return new User();
}
// 返回简单的对象(只包含基本的类型)
public User getUser(User user) {
System.out.println(user.getName());
return user;
}
}
3. 发布服务,在server-confing.wsdd文件中进行配置。
<?xml version="1.0" encoding="UTF-8" ?>
<deployment name="defaultClientConfig"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler"
xmlns="http://xml.apache.org/axis/wsdd/">
<globalConfiguration name="defaultClientConfig">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1"
type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session" />
</handler>
<handler name="Handler2"
type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request" />
<parameter name="extension" value=".jwr" />
</handler>
</requestFlow>
</globalConfiguration>
<handler name="URLMapper"
type="java:org.apache.axis.handlers.http.URLMapper" />
<handler name="LocalResponder"
type="java:org.apache.axis.transport.local.LocalResponder" />
<handler name="Authenticate"
type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
<transport name="http" type="">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1" type="URLMapper" />
<handler name="Handler2"
type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
</requestFlow>
</transport>
<transport name="local" type="">
<responseFlow name="ResponseFlow1" type="">
<handler name="Handler1" type="LocalResponder" />
</responseFlow>
</transport>
<!--这里配置了一个Web Service,如果有多个Web Service,就按这种格式在下面增加即可-->
<service name="User" provider="java:RPC">
<!-- 作用域 -->
<parameter name="scope" value="Request" />
<!-- 发布的webservice类名 -->
<parameter name="className" value="com.devtek.ServiceServer" />
<!-- 发布webservice方法,value="*" '*'代表所有, 如果不想发布所有的方法,可以在
value 中,每个方法用空格分开,如发布两个方法:value="getUserName getUser"
-->
<parameter name="allowedMethods" value="*" />
<!-- 对象映射,一对象为参数或返回值 -->
<beanMapping languageSpecificType="java:com.devtek.User"
qname="ns1:User" xmlns:ns1="com.devtek" />
<!-- 类型映射,将对象进行序列化(out)和反序列化(in) -->
<typeMapping languageSpecificType="java:com.devtek.User"
qname="ns1:User" xmlns:ns1="com.devtek"
deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"/>
<namespace>com.devtek</namespace>
</service>
</deployment>
4. 客户端调用。
package com.devtek;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
public class Client {
public static void main(String[] args) {
try {
String endpoint = "http://127.0.0.1:8089/axis1/webservices/User?wsdl";
Service service = new Service();
Call call = null;
QName qn = new QName("http://127.0.0.1:8089/axis1/webservices/User",
"getUser");
call = (Call) service.createCall();
// 注册序列化和反序列化类
call.registerTypeMapping(User.class, qn,
new BeanSerializerFactory(User.class, qn),
new BeanDeserializerFactory(User.class, qn));
// 设置返回类型
call.setReturnType(qn, User.class);
call.setOperationName(qn);
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.addParameter("user", qn, javax.xml.rpc.ParameterMode.IN);
User user = new User();
User ret = (User) call.invoke(new Object[] { user });
System.out.println("return value is " + ret);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
http://www.iteye.com/topic/193363