您的位置:首页 > 编程语言 > Java开发

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

2010-05-27 09:04 411 查看
写一个第三方服务调用通用类,把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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: