通过配置spring.xml实现通用Webservice调用

  • 写一个第三方服务调用通用类,把Axis参数作为属性定义在类中:
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.rmi.RemoteException;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    import javax.xml.namespace.QName;
    import javax.xml.rpc.ServiceException;
    import org.apache.axis.AxisProperties;
    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;
    package jastar.test;
    public class ThirdPartyService {
    	
    	private String endpoint; // 服务端点
    	private String namespace; // 命名空间
    	private String operationName; // 服务操作名称
    	private QName returnType; // 服务获取的对象类型名称
    	private Map<String, String> params; // 传入参数名称和类型对
    	/**
    	 * 调用Web service并返回返回结果
    	 * 
    	 * @param args 传入的参数对象数组
    	 * @return WebServices 调用结果
    	 * @throws RepairServiceException 
    	 */
    	public Object getCallResult(Object[] args) throws RepairServiceException {
    		
    		AxisProperties.setProperty("http.proxyHost","192.168.1.22");
    		AxisProperties.setProperty("http.proxyPort","808");
    		
    		Object ret = null;
    		Service service = new Service();
    		QName qn = new QName(namespace, operationName);
    		Call call = null;
    		
    		try {
    			call = (Call)service.createCall();
    		} catch (ServiceException e) {
    			throw new MyServiceException("创建Webservice调用对象失败!", e);
    		}
    		call.setOperationName(qn);
    		Set<String> keys = params.keySet();
    		for (Iterator<String> it = keys.iterator(); it.hasNext();) {
    			String paramName = it.next();
    			QName param = new QName(namespace, paramName);
    			QName xmlType =
    				new QName(namespace, (String)params
    					.get(paramName));
    			
    			call.addParameter(param, xmlType,
    				javax.xml.rpc.ParameterMode.IN);
    		}
    		
    		try {
    			call.setTargetEndpointAddress(new URL(endpoint));
    		} catch (MalformedURLException e) {
    			throw new MyServiceException("设置Service服务端失败!", e);
    		}
    		
    		call.setReturnType(returnType);
    		
    		try {
    			ret = call.invoke(args);
    		} catch (RemoteException e) {
    			throw new MyServiceException("调用WebService服务失败!", e);
    		}
    		
    		return ret;
    	}
    	public String getEndpoint() {
    		return endpoint;
    	}
    	public void setEndpoint(String endpoint) {
    		this.endpoint = endpoint;
    	}
    	public String getNamespace() {
    		return namespace;
    	}
    	public void setNamespace(String namespace) {
    		this.namespace = namespace;
    	}
    	public String getOperationName() {
    		return operationName;
    	}
    	public void setOperationName(String operationName) {
    		this.operationName = operationName;
    	}
    	
    	public QName getReturnType() {
    		return returnType;
    	}
    	public void setReturnType(QName returnType) {
    		this.returnType = returnType;
    	}
    	public Map getParams() {
    		return params;
    	}
    	@SuppressWarnings ("unchecked")
    	public void setParams(Map params) {
    		this.params = params;
    	}
    }

  • 将这个ThirdPartService类注入到具体的业务Service中,在具体业务方法中调用ThirdPartyService的getCallResult,从而可以将第三方服务调用方便的封装起来。
  • 在applicationContext.xml中配置服务调用相关参数:
    <bean id="helloSupportService"
    	class="jastar.HelloSupportService">
    	<property name="gisSupportService">
    		<bean
    			class="jastar.ThirdPartySupportService">
    			<property name="endpoint">
    				<value>http://192.168.1.1:8080/Service.asmx</value>
    			</property>
    			<property name="namespace">
    				<value>http://service.jastar</value>
    			</property>
    			<property name="operationName">
    				<value>sayHello</value>
    			</property>
    			<property name="returnType">
    				<util:constant
    					static-field="org.apache.axis.encoding.XMLType.SOAP_STRING" />
    			</property>
    			<property name="params">
    				<map>
    					<entry key="name" value="string" />
    					<entry key="greeting" value="string" />
    				</map>
    			</property>
    		</bean>
    	</property>
    </bean>
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值