通过配置spring.xml实现通用Webservice调用
2010-05-27 09:04
411 查看
写一个第三方服务调用通用类,把Axis参数作为属性定义在类中:
将这个ThirdPartService类注入到具体的业务Service中,在具体业务方法中调用ThirdPartyService的getCallResult,从而可以将第三方服务调用方便的封装起来。
在applicationContext.xml中配置服务调用相关参数:
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>
相关文章推荐
- 通过配置spring.xml实现通用Webservice调用
- Spring AOP详解及通过XML配置实现的实现方法
- spring基于task标签和通过xml配置实现任务的区别
- Spring-通过xml配置文件实现切面(AOP)
- Spring-通过xml配置实现AOP
- [翻译]通过WebService调用SQLXML(SQL Server 2005) (转)
- 用C#通过反射实现动态调用WebService 告别Web引用
- Spring通过@async 实现异步调用
- strtus2 + spring + CXF 实现能访问action的同时还能调用spring发布的Webservice
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
- 在android中使用xml调用webservice,实现自己的单词查询
- Spring+SpringMVC+SpringData+JPA(Hibernate实现)无XML配置
- WebService技术总结(三):项目实战——使用XML,dom4j,Xpath实现远程调用
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- 通过WebService调用SQLXML(SQL Server 2005)
- Spring依赖注入的XML配置文件的实现思想(2)——简单的实现
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 通过ApplicationContextAwareSpring实现手工加载配置的javabean
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换