axis2 传递List对象

  • Post author:
  • Post category:其他


下载jar包:axis2-1.5.4-bin.zip

下载war包:axis2-1.5.4-war.zip

路径网上有

服务端目录:

客户端目录:

解压axis2-1.5.4-war.zip,获取axis2.war包,放到tomcat下,启动tomcat

1.tomcat.0\webapps\axis2\WEB-INF,复制该路径下的conf文件夹与services文件夹到,服务端的WEB-INF中

2.在services文件夹下建立对应目录。新建service.xml文件,复制下面service.xml代码

3.注释WEB-INF\conf\axis2.xml 下“<module ref=”addressing”/>” 大概在237行

4.复制axis2-1.5.4-bin.zip解压后lib包下的所有jar到服务端和客户端(后面再筛选必须的jar包)

4.在web.xml中添加

web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<servlet>   
        <servlet-name>AxisServlet</servlet-name>   
        <servlet-class>   
            org.apache.axis2.transport.http.AxisServlet   
        </servlet-class>   
        <load-on-startup>1</load-on-startup>   
    </servlet>   
    <servlet-mapping>   
        <servlet-name>AxisServlet</servlet-name>   
        <url-pattern>/servlet/AxisServlet</url-pattern>   
    </servlet-mapping>   
    <servlet-mapping>   
        <servlet-name>AxisServlet</servlet-name>   
        <url-pattern>*.jws</url-pattern>   
    </servlet-mapping>   
    <servlet-mapping>   
        <servlet-name>AxisServlet</servlet-name>   
        <url-pattern>/services/*</url-pattern>   
    </servlet-mapping>  
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

服务端service.xml代码

<serviceGroup>

	<!--下面定义服务名,当在url访问时用到此服务名-->
	<service name="BOBOService" scope="soapsession">
		<description></description>
		<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>

		<!--ServiceClass指定Java Class的位置,即实现服务的类。  -->
		<parameter name="ServiceClass" locked="xsd:false">
			com.bobo.service.BOBOService
		</parameter>
		<!--operation 与Java Class中方法名对应 -->
		<operation name="receiveUser">
			<messageReceiver
				class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
			<actionMapping>urn:receiveUser</actionMapping>
		</operation>
	</service>

</serviceGroup>

服务端,客户端同样User代码

package com.bobo.entity;

import java.io.Serializable;


public class User implements Serializable {
	private static final long serialVersionUID = 677484458789332877L;
	private int id;
	private String name;
	private String email;
	private String address;
	
	public User(){}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return this.id + "#" + this.name + "#" + this.email + "#" + this.address;
	}
}

客户端BOBOClient代码

package com.bobo.client;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.transport.http.HTTPConstants;

import com.bobo.entity.User;

public class BOBOClient {
	public static void main(String[] args) throws Exception {
		EndpointReference targetEPR = new EndpointReference("http://localhost:8085/Axis2service/services/BOBOService");
		//EndpointReference targetEPR = new EndpointReference("http://localhost:8085/SayHello/services/APIWebService2");


		try {
			Options options = new Options();
			options.setTo(targetEPR);
			
			options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
			options.setProperty(HTTPConstants.CHUNKED, "false");//设置不受限制.
			
			
			ServiceClient sender = new ServiceClient();
			sender.setOptions(options);
			System.out.println("webservice start");
			OMElement sayHello = doSetOMElement();
			
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace omNs = fac.createOMNamespace("", "");
			OMElement method = fac.createOMElement("receiveUser", omNs);
			method.addChild(sayHello);
			
			OMElement result = sender.sendReceive(method);
			System.out.println("webservice end");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *创建调用webservice接口所需的参数OMElement对象。
	 * 
	 * @return
	 */
	public static OMElement doSetOMElement() {
		
		List<User> list = new ArrayList<User>();
		User user1 = new User();
		user1.setId(1);
		user1.setName("chenbo");
		user1.setAddress("aaaa");
		user1.setEmail("ddddddd");
		User user2 = new User();
		user2.setId(2);
		user2.setName("chenbo2");
		user2.setAddress("aaaa2");
		user2.setEmail("ddddddd2");
		list.add(user1);
		list.add(user2);
		String typeName = "computer";
		// 构建 OMFactory 工厂
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace omNamespace = factory.createOMNamespace("http://service.bob.com", "ns");
		OMElement omElement = BeanUtil.getOMElement(new QName("root"), list.toArray(), new QName("user"), false, null);
		omElement.addAttribute("name", typeName, omNamespace);
		return omElement;
		
	}
}

服务端BOBOService代码

package com.bobo.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axis2.AxisFault;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.engine.DefaultObjectSupplier;

import com.bobo.entity.User;

public class BOBOService {

	public OMElement receiveUser(OMElement userEle) throws Exception {

		System.out.println("进入服务端方法!!!!!!!");
		List<User> list = getResults(userEle);

		for (User user : list) {
			System.out.println(user.getName());
		}

		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace("", "");
		OMElement soapMain = fac.createOMElement("sendClient", omNs);

		return soapMain;
	}

	private static List<User> getResults(OMElement element) throws AxisFault {
		if (element == null) {
			return null;
		}

		element = (OMElement) element.getChildElements().next(); // 下个元素

		Iterator iterator = element.getChildElements();
		List<User> list = new ArrayList<User>();
		while (iterator.hasNext()) {
			OMNode omNode = (OMNode) iterator.next();
			if (omNode.getType() == OMNode.ELEMENT_NODE) {
				OMElement omElement = (OMElement) omNode;
				System.out.println(omElement.getLocalName());
				if (omElement.getLocalName().toLowerCase().equals("user")) {
					User User = (User) BeanUtil.processObject(omElement, User.class, null, true, new DefaultObjectSupplier());
					list.add(User);
				}
			}
		}
		return list;
	}

}



版权声明:本文为chenbo19867758原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。