Java Axis2调用WebService接口异常解决方案

最近写用Axis2写WebService客户端,在Windows环境下运行正常,在Linux环境下包如下异常:

Caused by: java.lang.NullPointerException
    at org.apache.axiom.locator.DefaultOMMetaFactoryLocator.<init>(DefaultOMMetaFactoryLocator.java:64)

Caused by: java.lang.ExceptionInInitializerError
	at org.apache.axis2.description.AxisDescription.<init>(AxisDescription.java:68)
	at org.apache.axis2.engine.AxisConfiguration.<init>(AxisConfiguration.java:148)
	at org.apache.axis2.deployment.DeploymentEngine.populateAxisConfiguration(DeploymentEngine.java:639)
	at org.apache.axis2.deployment.FileSystemConfigurator.getAxisConfiguration(FileSystemConfigurator.java:116)
	at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:68)
	at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContextFromFileSystem(ConfigurationContextFactory.java:184)
	at org.apache.axis2.client.ServiceClient.configureServiceClient(ServiceClient.java:150)
	at org.apache.axis2.client.ServiceClient.<init>(ServiceClient.java:143)
	at org.apache.axis2.client.ServiceClient.<init>(ServiceClient.java:244)
	at org.apache.axis2.rpc.client.RPCServiceClient.<init>(RPCServiceClient.java:48)
	at cn.tisson.common.utils.AxisClientUtil.send(AxisClientUtil.java:79)
	at cn.tisson.sag.services.channel.shcharge.AccountRechargeServiceImpl.doPersonAccountRecharge(AccountRechargeServiceImpl.java:135)
	... 9 more
Caused by: java.lang.NullPointerException
	at org.apache.axiom.locator.DefaultOMMetaFactoryLocator.<init>(DefaultOMMetaFactoryLocator.java:64)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at java.lang.Class.newInstance0(Class.java:355)
	at java.lang.Class.newInstance(Class.java:308)
	at org.apache.axiom.om.OMAbstractFactory.<clinit>(OMAbstractFactory.java:95)
	... 21 more

请求WebService接口的代码如下:

/**   
 * @Project: Axis2 
 * @Title: AxisClientUtil.java 
 * @Package: com.bestpay.webservice.client 
 * @Description: WebService调用类, 通过Axis2实现 
 * @Author: weizhi2018
 * @CreateDate: 2013-7-18 10:27:09 
 * @UpdateUser:    
 * @UpdateDate:    
 * @UpdateRemark:  
 * @Copyright: 2013  
 * @Version: V1.0   
 */

package com.bestpay.webservice.client;
//restful
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.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.engine.DefaultObjectSupplier;
import org.apache.axis2.rpc.client.RPCServiceClient;

/**
 * @ClassName AxisClientUtil
 * @Description WebService调用类
 * @Author weizhi2018
 * @Date 2013-7-18 10:27:09
 * 
 */
public class AxisClientUtil {

	// 请求的WebService地址(不带"?wsdl")
	private String url;

	// WSDL文件的命名空间名,即<wsdl:definitions targetNamespace="">元素的targetNamespace属性值
	private String namespace;

	//接口地址的用户名和密码
	private String authuser;
	private String authpass;
	
	/**
	 * @param url    WSDL地址
	 * @param namespace WSDL命名空间
	 */
	public AxisClientUtil(String url, String namespace) {
		this.namespace = namespace;
		this.url = url;
	}
	
	/**
	 * @param url
	 * @param namespace
	 * @param authuser
	 * @param authpass
	 */
	public AxisClientUtil(String url, String namespace,String authuser,String authpass) {
		this.namespace = namespace;
		this.url = url;
		this.authuser = authuser;
		this.authpass = authpass;
	}
	
	/** 
	 * @Title send 
	 * @Description 发送WebService请求
	 * @Author weizhi2018 
	 * @param @param method  请求WebService服务的方法的名字
	 * @param @param opAddEntryArgs 请求的入参数组
	 * @param @return 返回请求处理结果    
	 * @return String     
	 * @throws 
	 */
	public String send(String method, Object[] opAddEntryArgs) {
		String result = null;

		RPCServiceClient serviceClient;
		try {
			serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();
			
			// 请求的URL
			EndpointReference targetEPR = new EndpointReference(this.url);
			options.setTo(targetEPR);
			
			//验证属性
			options.setUserName(this.authuser);
			options.setPassword(this.authpass);
			
			// 调用的WebService的方法名字,QName对象
			QName opAddEntry = new QName(this.namespace, method);

			// 参数数组,按顺序组织数组,不能为null
			if (opAddEntryArgs == null) {
				opAddEntryArgs = new Object[]{};
			}

			// 指定返回值的类型
			Class[] classes = new Class[]{String.class};

			// 发送WebService请求
			result = (String) serviceClient.invokeBlocking(opAddEntry,
					opAddEntryArgs, classes)[0];
		} catch (AxisFault e) {
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/** 
	 * @Title send 
	 * @Description 此接口参数固定,专为上海平台接口而设
	 * @Author weizhi2018 
	 * @param @param method
	 * @param @param in0 参数1
	 * @param @param in1 参数2
	 * @param @return     
	 * @return String     
	 * @throws 
	 */
	public String send(String method, String in0,String in1) {
		String result = null;

		RPCServiceClient serviceClient;
		try {
			serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();

			// 请求的URL
			EndpointReference targetEPR = new EndpointReference(this.url);
			options.setTo(targetEPR);
			OMFactory fac = OMAbstractFactory.getOMFactory();
			
			//创建命名空间Om对象
			OMNamespace omNs = fac.createOMNamespace(this.namespace, "tns");
			//创建请求方法OM对象
			OMElement omEleMethod = fac.createOMElement(method, omNs);
			
			//创建请求参数1
			OMElement omIn0 = fac.createOMElement("in0", omNs);
			omIn0.addChild(fac.createOMText(omIn0, in0));
			//创建请求参数2
			OMElement omIn1 = fac.createOMElement("in1", omNs);
			omIn1.addChild(fac.createOMText(omIn1, in1));
			
			omEleMethod.addChild(omIn0);
			omEleMethod.addChild(omIn1);
			// 发送WebService请求
			OMElement omResult = serviceClient.sendReceive(omEleMethod);
			
			// 指定返回值的类型
			Class[] classes = new Class[]{String.class};
			
			result = (String) BeanUtil.deserialize(omResult, classes, new DefaultObjectSupplier())[0];
		} catch (AxisFault e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toString();
	}
}
依赖jar包:

Java Axis2调用WebService接口异常解决方案_第1张图片

分析原因:经询问,对方WebService接口是用Axis-1.4.2发布的,而我这个是用Axis2写的客户端,下面换成Axis-1.4.2。代码如下:

/**   
 * @Project: Axis 
 * @Title: AxisClientUtil.java 
 * @Package: com.bestpay.webservice.axis 
 * @Description: TODO 
 * @Author: weizhansong  [email protected]
 * @CreateDate: 2013-8-6 下午02:05:07 
 * @Copyright: 2013  
 * @Version: V1.0   
 */

package com.bestpay.webservice.axis;

import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

/**
 * @ClassName AxisClientUtil
 * @Description TODO
 * @Author weizhi2018
 * @Date 2013-8-6 下午02:05:07
 * 
 */

public class AxisClientUtil {

	// 请求的WebService地址(不带"?wsdl")
	private String url;

	/**
	 * @param url
	 *            WSDL地址
	 */
	public AxisClientUtil(String url) {
		this.url = url;
	}

	/**
	 * @Title send
	 * @Description 发送WebService请求
	 * @Author weizhi2018
	 * @param @param method 请求WebService服务的方法的名字
	 * @param @param opAddEntryArgs 请求的入参数组
	 * @param @return 返回请求处理结果
	 * @return String
	 * @throws
	 */
	public String send(String method, Object[] param) {
		String result = null;
		Call call;
		try {
			Service service = new Service();
			call = (Call) service.createCall();

			// 设置访问点
			call.setTargetEndpointAddress(this.url);

			// 设置操作名
			call.setOperationName(method);

			// 设置入口参数
			call.addParameter("in0", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("in1", XMLType.XSD_STRING, ParameterMode.IN);

			// 设置返回参数类型
			call.setReturnType(XMLType.XSD_STRING);
			
			result = (String) call.invoke(param);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
 
依赖jar包:

Java Axis2调用WebService接口异常解决方案_第2张图片


用此代码在Windows和Linux环境下均不再报错。可以解决问题,不过开头的那个报错原因还是没有定位到。



你可能感兴趣的:(java,webservice,解决方案,axis2,axis)