axis2 webservice下实现AXIOM service的完整步骤

原理:

AXIOM,即OMElement(也称为 OM)

AXIS2会将Web service的方法中的参数,返回值中的自定义对象、数组、List等类型统一映射为OMElement类型

我们需要做的:  编写OMElement与类型之间的转换方法

1.先编写OMElement与类型之间的转换方法的util class

web service和client 都调用这个类型转换工具类,实现OMElement向基本类型和用户类的转换,以及反方向的基本类型和用户类向OMElement的转换
public class OMElementStudent1Util {
public static void main(String[] args) throws AxisFault {  
 }   
///////////////////////object to OMElement////////////////////////////////   
public static OMElement plainString2OMElement(String targetNameSpace,String prefix,String titleString,String retString){
// 获取OMNamespace
OMFactory fac=OMAbstractFactory.getOMFactory();
OMNamespace omNs=fac.createOMNamespace(targetNameSpace,prefix);       
OMElement resp=fac.createOMElement(titleString,omNs);
resp.setText(retString);
return resp;
}


public static OMElement object2OMElement_pull(Object obj){
XMLStreamReader reader = BeanUtil.getPullParser(obj);
StreamWrapper parser = new StreamWrapper(reader);
StAXOMBuilder stAXOMBuilder
=OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), parser);
OMElement element = stAXOMBuilder.getDocumentElement();
return element;
}


public static OMElement object2OMElement(Object[] oArray,String type){
OMElement omElement=
BeanUtil.getOMElement(new QName("root"),oArray,new QName(type),false,null);
return omElement;
}

//////////////////////////OMElement to basic type/////////////////////////////////
public static String OM2PlainString(OMElement in){
return in.getText().trim();
}


public static List OM2ListString(OMElement in){
Iterator iterator = in.getChildElements();         
List<String> list = new ArrayList<String>();
while (iterator.hasNext()) {
OMNode omNode = (OMNode) iterator.next();             
if (omNode.getType() == OMNode.ELEMENT_NODE) {
// 如果子对象也是element节点类型的话(其他还有文本节点, CDATA节点等)
OMElement omElement = (OMElement) omNode;
if (omElement.getLocalName().equals("String")) { //看是否是对应类型
list.add(omElement.getText().trim());  //获得返值
} // end of if
} // end of if
} // end of while
return list;
}

//////////////////////////OMElement to business Object type////////////////////////////////   
public static Object OM2ObjectPrint(OMElement in) throws AxisFault{
Iterator iterator = in.getChildElements();
Student1 stu = new Student1();
List<Student1>
list = new ArrayList<Student1>();
while (iterator.hasNext()) {
System.out.println("--------------------------------");
OMNode omNode = (OMNode) iterator.next();
System.out.println("omNode.getType:"+omNode.getType());
if (omNode.getType() == OMNode.ELEMENT_NODE) {
OMElement omElement = (OMElement) omNode;
System.out.println("omElement.getLineNumber:"+omElement.getLineNumber());
System.out.println("omElement.getLocalName:"+omElement.getLocalName());
System.out.println("omElement.getText:"+omElement.getText());
if (omElement.getLocalName().toLowerCase().equals("age")) {
stu.setAge(Integer.parseInt(omElement.getText()));
}
if (omElement.getLocalName().toLowerCase().equals("id")) {
stu.setId(Integer.parseInt(omElement.getText()));
}
if (omElement.getLocalName().toLowerCase().equals("name")) {
stu.setName(omElement.getText());
}
}
}
return stu;
}


public static Object OM2Object1(OMElement in) throws AxisFault{
//从element获取其子对象的迭代器
Iterator iterator = in.getChildElements();
Student1 stu = new Student1();
while (iterator.hasNext()) {
OMNode omNode = (OMNode) iterator.next();
if (omNode.getType() == OMNode.ELEMENT_NODE) {
OMElement omElement = (OMElement) omNode;
if (omElement.getLocalName().toLowerCase().equals("age")) {
stu.setAge(Integer.parseInt(omElement.getText()));
}
if (omElement.getLocalName().toLowerCase().equals("id")) {
stu.setId(Integer.parseInt(omElement.getText()));
}
if (omElement.getLocalName().toLowerCase().equals("name")) {
stu.setName(omElement.getText());
}
}
}
return stu;
}  

public static Object OM2Object2(OMElement in) throws AxisFault{
//从element获取其子对象的迭代器
Iterator iterator = in.getChildElements();
Student1 stu = new Student1();         
while (iterator.hasNext()) {
OMNode omNode = (OMNode) iterator.next();
if (omNode.getType() == OMNode.ELEMENT_NODE) {
OMElement omElement = (OMElement) omNode;
if(omElement.getLocalName().toLowerCase().equals("student1")){
stu=(Student1)BeanUtil.processObject(omElement,Student1.class,null,true,new DefaultObjectSupplier());
}
}
}
return stu;
}


public static List<Student1> 
OM2ObjectList(OMElement in) throws AxisFault{
//从element获取其子对象的迭代器
Iterator iterator = in.getChildElements();
Student1 stu = new Student1();
List<Student1>
sList = new ArrayList<Student1>();
while (iterator.hasNext()) {
OMNode omNode = (OMNode) iterator.next();
if (omNode.getType() == OMNode.ELEMENT_NODE) {
OMElement omElement = (OMElement) omNode;
if(omElement.getLocalName().toLowerCase().equals("student1")){
stu=(Student1)BeanUtil.processObject(omElement,Student1.class,null,true,new DefaultObjectSupplier());
sList.add(stu);
}
}
}
return sList;
}
}

2.AXIOM webservice class

service class
@Service
public class TestOMSpring {
@Autowired
private StuService stuService;   
public OMElement findByIdPlain(OMElement in){
String name=in.getText();    // 获取OMElement形式的参数,转换为常见的类型
// 程序逻辑
Integer id=Integer.parseInt(name);
String retString = stuService.getById(id).getName();
// 生成返值
return OMElementStudent1Util.plainString2OMElement(
"http://test.machome.com","hw","findByIdResponse",retString);   }
public OMElement findAllString(OMElement in){
// 程序逻辑
List<Student1> sList = stuService.findAll();
String[] rets = new String[sList.size()];
for(int i=0;i<rets.length;i++)
rets[i]=sList.get(i).getName();
// 生成返值
return  OMElementStudent1Util.object2OMElement(rets,"String");
}


public OMElement findAllList(OMElement in){
// 程序逻辑
List<Student1> sList = stuService.findAll();
List<String> rets = new ArrayList<String>(sList.size());
for(int i=0;i<sList.size();i++)
rets.add(sList.get(i).getName());
// 生成返值
// axiom对List也是转成数组处理的
return  OMElementStudent1Util.object2OMElement(rets.toArray(),"String");   }
public OMElement findByIdObject_pull(OMElement in){
String name=in.getText();    // 获取OMElement形式的参数,转换为常见的类型
// 程序逻辑
Integer id=Integer.parseInt(name);
Student1 retStu = stuService.getById(id);
//生成返值
return OMElementStudent1Util.object2OMElement_pull(retStu);
}


public OMElement findByIdObject(OMElement in){
String name=in.getText();    // 获取OMElement形式的参数,转换为常见的类型
// 程序逻辑
Integer id=Integer.parseInt(name);
Student1 retStu = stuService.getById(id);
// 生成返值
return  OMElementStudent1Util.object2OMElement(new Student1[]{retStu},"Student1");   }
public OMElement findAllObject(OMElement in){
// 程序逻辑
List<Student1> sList = stuService.findAll();
// 生成返值
return  OMElementStudent1Util.object2OMElement(sList.toArray(),"Student1");
}
}

META-INF/services.xml文件:  
注意AXIOM方式的方法,要重新定义方法的messageReceiver为raw方式

<serviceGroup>
<service name="TestOMSpring" scope="application" targetNamespace="http://quickstart.samples/">
<description>
TestOMSpring Service
</description>   <parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<parameter name="SpringBeanName">testOMSpring</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<operation name="findByIdPlain">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="findAllString">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="findAllList">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="findByIdObject_pull">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="findByIdObject">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="findAllObject">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service></serviceGroup >

3.client 端

public class TestOMSpringClient {
private final static String TARGET_NAMESPACE = "http://test.machome.com";
private final static String  PREFIX = "hw";
private final static String  SERVICE_URL =
"http://localhost:8080/axis2/services/TestOMSpring";
public static void main(String[] args) throws AxisFault {
ServiceClient sender = initClient(SERVICE_URL);
getFindByIdPlain(sender,"1");
getFindAllString(sender);
getFindAllList(sender);
System.out.println("---------------------------");
getFindByIdObject_pull(sender,"3");
getFindByIdObject(sender,"2");
getFindAllObject(sender);
}


public static ServiceClient initClient(String serviceUrl) throws AxisFault{
EndpointReference targetEPR=new EndpointReference(serviceUrl);
Options options=new Options();
options.setTo(targetEPR);
ServiceClient sender=new ServiceClient();
sender.setOptions(options);
return sender;
}

    //////////////////////////basic type/////////////////////////////////
public static void getFindByIdPlain(ServiceClient sender,String param) throws AxisFault{
OMElement method=OMElementStudent1Util.plainString2OMElement(
TARGET_NAMESPACE,PREFIX, "findByIdPlain", param);
OMElement result=sender.sendReceive(method);
System.out.println(OMElementStudent1Util.OM2PlainString(result));
}


public static void getFindAllString(ServiceClient sender) throws AxisFault{
OMElement method=OMElementStudent1Util.plainString2OMElement(
TARGET_NAMESPACE,PREFIX, "findAllString", null);
OMElement result=sender.sendReceive(method);
List<String> list = OMElementStudent1Util.OM2ListString(result);
System.out.println("list:");
for(String temp:list)
System.out.println(temp);


public static void getFindAllList(ServiceClient sender) throws AxisFault{
OMElement method=OMElementStudent1Util.plainString2OMElement(
TARGET_NAMESPACE,PREFIX, "findAllList", null);
OMElement result=sender.sendReceive(method);
// 不管是数组还是List,在service端都被做成数组(list.toArray())
List<String> list = OMElementStudent1Util.OM2ListString(result);
System.out.println("list:");
for(String temp:list)
System.out.println(temp);
}

     //////////////////////////business Object type////////////////////////////////
public static void getFindByIdObject_pull(ServiceClient sender,String param) throws AxisFault{
OMElement method=OMElementStudent1Util.plainString2OMElement(
TARGET_NAMESPACE,PREFIX, "findByIdObject_pull", param);
OMElement result=sender.sendReceive(method);
Student1 stu = (Student1)OMElementStudent1Util.OM2Object1(result);
System.out.println(stu.getId()+":"+stu.getName()+":"+stu.getAge());
}
public static void getFindByIdObject(ServiceClient sender,String param) throws AxisFault{
OMElement method=OMElementStudent1Util.plainString2OMElement(
TARGET_NAMESPACE,PREFIX, "findByIdObject", param);
OMElement result=sender.sendReceive(method);
Student1 stu = (Student1)OMElementStudent1Util.OM2Object2(result);
System.out.println(stu.getId()+":"+stu.getName()+":"+stu.getAge());
}
public static void getFindAllObject(ServiceClient sender) throws AxisFault{
OMElement method=OMElementStudent1Util.plainString2OMElement(
TARGET_NAMESPACE,PREFIX, "findAllObject", null);
OMElement result=sender.sendReceive(method);
//OM2ObjectPrint(result);
List<Student1> list = OMElementStudent1Util.OM2ObjectList(result);
System.out.println("list:");
for(Student1 temp:list)
System.out.println(temp.getId()+":"+temp.getName()+":"+temp.getAge());
}
}

执行测试

silly
list:
silly
mac
macy
mac2
list:
silly
mac
macy
mac2
---------------------------
3:macy:20
2:mac:20
list:
1:silly:20
2:mac:20
3:macy:20
4:mac2:20

本文来源:http://blog.csdn.net/x6582026/article/details/52192421


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。