术语:Oxm: object xml mapping
以前的例子都是传递简单的String什么的,对于一个复杂的类型,在传递的过程中跟简单类型有所不同,实际上axis引擎是将数据通过xml进行转换,然后使用复杂类型axis引擎就显得不是那么智能,需要手动配置了,这个过程我们通常叫他为OXM,就是对象与XML文件映射,他需要通过服务器端得序列化也就是转化成xml,再从客户端反序列化得到pojo类,最后才能正常使用,废话不多说,下面就是一个小的测试例子:
建立服务端PoJo类:
package cn.com.test.ce.pojo;
public class Person {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Web Service服务类:
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.com.test.ce.webservice;
import cn.com.test.ce.pojo.Person;
public class Service
{
public Person getPerson()
{
Person person = new Person();
person.setId(1);
person.setName("张三");
return person;
}
}
:客户端deploy.wsdd文件:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="MyService" provider="java:RPC">
<parameter name="className" value="cn.com.test.ce.webservice.Service"/>
<parameter name="allowedMethods" value="*"/>
<!-- 在服务器端将pojo类转换成xml,也就是序列化 -->
<beanMapping qname="myNS:Person" xmlns:myNS="urn:Service" languageSpecificType="java:cn.com.test.ce.pojo.Person"/>
</service>
</deployment>
客户端测试类:
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.com.test.webservice;
import java.net.URL;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.utils.Options;
import cn.com.test.ce.pojo.Person;
import javax.xml.namespace.QName;
public class Client
{
public static void main(String [] args)
{
try {
//创建服务对象
Service service = new Service();
//通过服务对象创建调用对象
Call call=(Call)service.createCall();
String url = "http://localhost:8080/MyAxisPoJoWebService/services/MyService";
//设置调用的目标终端地址
call.setTargetEndpointAddress(new URL(url));
//设置对象的操作名称
call.setOperationName("getPerson");
//限定名必须与服务器端注册的保持一致
QName qn = new QName( "urn:Service", "Person" );
//客户端反序列化,即把xml转化成java pojo对象
call.registerTypeMapping(Person.class, qn,
new org.apache.axis.encoding.ser.BeanSerializerFactory(Person.class, qn),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(Person.class, qn));
// call.setOperationName( new QName("LogTestService", "testMethod") );
Person res = (Person) call.invoke( new Object[] {} );
System.out.println( res );
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
最后不清楚怎样部署与测试的童鞋请看看我前几篇简单的例子,里面有详细说明。