Java动态调用webService,axis2动态调用webService
程序员文章站
2022-05-01 22:35:33
...
Java动态调用webService
axis2动态调用webService
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2015年12月15日 17:26:30 星期二
http://fanshuyao.iteye.com/
附件为代码压缩包
package com.chinagas.org.webservice;
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.soap.SOAP11Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
/**
* webservice 调用封装
*/
public class WebServiceInvoker {
/**
* 构建webservice请求参数
* @param nameSpace 命名空间
* @param params
* @param paramValues
* @param paramPrefix 参数前缀
* @param method
* @param wsMethodPrefix 调用的方法前缀
* @return
*/
private static OMElement buildParam(String nameSpace,String[] params, String[] paramValues,
String paramPrefix, String method, String wsMethodPrefix) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(nameSpace, wsMethodPrefix); // 设置方法名前缀
OMNamespace omNsParam = (paramPrefix==null?null:fac.createOMNamespace(nameSpace, paramPrefix)); //设置参数前缀
OMElement data = fac.createOMElement(method, omNs); // 获得要调用的方法名
for(int i=0;i<params.length;i++){
OMElement inner = fac.createOMElement(params[i], omNsParam); //注意这里,看参数有没有前缀,很重要2015-12-11
inner.setText(paramValues[i]); // 输入参数
data.addChild(inner); // 将该参数加入要调用的方法节点
}
return data;
}
/**
*
* @param targetAirline EndpointReference对象
* @param nameSpace 命名空间,可以在http//……?wsdl中找到
* @param method 方法名
* @return
*/
private static Options buildOptions(EndpointReference targetAirline, String nameSpace, String method) {
Options options = new Options();
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setAction(nameSpace + method); // 设置调用的命名空间加方法
options.setTo(targetAirline);
return options;
}
public static OMElement createHeaderOMElement(String nameSpace, String wsHeaderNameSpace,
String wsHeaderPrefix, String userName, String password){
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace(
wsHeaderNameSpace, wsHeaderPrefix);
OMElement header = factory.createOMElement("RequestSOAPHeader",
namespace);
OMElement usernameOM = factory.createOMElement("user", namespace);
OMElement passwordOM = factory.createOMElement("password", namespace);
usernameOM.setText(userName);
passwordOM.setText(password);
header.addChild(usernameOM);
header.addChild(passwordOM);
return header;
}
/**
* 获得返回数据(XML格式)
* @param targetAirline EndpointReference对象
* @param sender ServiceClient对象
* @param nameSpace 命名空间
* @param wsHeaderNameSpace Header命名空间
* @param wsHeaderPrefix Header命名空间前缀
* @param userName 用户名,允许为空
* @param password 密码,允许为空
* @param params 传递的参数名
* @param paramValues 传递的参数值
* @param paramPrefix 传递的参数前缀
* @param method ws调用方法名
* @param wsMethodPrefix ws调用方法名前缀
* @return XML格式的string
*/
private static String getResultByCode(EndpointReference targetAirline, ServiceClient sender, String nameSpace, String wsHeaderNameSpace,
String wsHeaderPrefix, String userName, String password,
String[] params, String[] paramValues, String paramPrefix,
String method, String wsMethodPrefix) {
try {
sender.setOptions(buildOptions(targetAirline, nameSpace, method));
sender.addHeader(createHeaderOMElement(nameSpace, wsHeaderNameSpace,
wsHeaderPrefix, userName, password));
OMElement result = sender.sendReceive(buildParam(nameSpace,
params, paramValues, paramPrefix,
method, wsMethodPrefix));
return result.toString();
} catch (Exception e) {
e.printStackTrace();
return "调用出错!";
}
}
/**
* 调用 WebService
* @param url 请求的url
* @param nameSpace 命名空间
* @param wsHeaderNameSpace header命名空间
* @param wsHeaderPrefix header前缀
* @param userName 用户名
* @param password 密码
* @param params 参数
* @param paramValues 参数的值
* @param paramPrefix 参数前缀
* @param method 调用的方法名
* @param wsMethodPrefix 调用的方法前缀
* @return
*/
public static Document invoke(String url, String nameSpace, String wsHeaderNameSpace,
String wsHeaderPrefix, String userName, String password,
String[] params, String[] paramValues,String paramPrefix,
String method, String wsMethodPrefix) {
try {
EndpointReference targetAirline = new EndpointReference(url);
ServiceClient sender = new ServiceClient();
return DocumentHelper.parseText(getResultByCode(targetAirline, sender, nameSpace, wsHeaderNameSpace,
wsHeaderPrefix, userName, password,
params, paramValues, paramPrefix, method, wsMethodPrefix));
} catch (DocumentException e) {
System.out.println("无返回体");
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
/*try {
sender.cleanupTransport();//如果有超时问题,可以使用该代码
} catch (AxisFault e) {
e.printStackTrace();
}*/
}
}
}
使用方法如下:
Document doc = WebServiceInvoker.invoke(pushConfig.getPushUrl(),
pushConfig.getWsNameSpace(),
pushConfig.getWsHeaderNameSpace(),
pushConfig.getWsHeaderPrefix(),
pushConfig.getUserName(),
pushConfig.getUserPassword(),
new String[]{pushConfig.getParam()},
new String[]{jsonString},
pushConfig.getParamPrefix(),
pushConfig.getWsMethodName(),
pushConfig.getWsMethodPrefix()
);
自定义的一个配置实体类:
package com.chinagas.org.beans;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
//
public class PushConfig{
//ID(通过序列:SEQ_PUSH_ID生成)
private Long pushId;
//推送名称
private String pushName;
//系统名称
private String systemName;
//推送地址
private String pushUrl;
//推送类型(ws,resful)
private String pushType;
//ws命名空间
private String wsNameSpace;
//ws调用方法名
private String wsMethodName;
//用户名
private String userName;
//密码
private String userPassword;
//请求参数
private String param;
//json类型)
private String jsonType;
//可用标识
private String enableFlag;
//
private Date createTime;
//
private Long createBy;
//
private Date modifyTime;
//
private Long modifyBy;
//备注
private String remark;
//头(Header)命名空间
private String wsHeaderNameSpace;
//头(Header)前缀
private String wsHeaderPrefix;
//方法前缀
private String wsMethodPrefix;
//参数前缀
private String paramPrefix;
//返回参数
private String paramReturn;
public Long getPushId() {
return this.pushId;
}
public void setPushId(Long pushId) {
this.pushId = pushId;
}
public String getPushName() {
return pushName;
}
public void setPushName(String pushName) {
this.pushName = pushName;
}
public String getSystemName() {
return this.systemName;
}
public void setSystemName(String systemName) {
this.systemName = systemName;
}
public String getPushUrl() {
return this.pushUrl;
}
public void setPushUrl(String pushUrl) {
this.pushUrl = pushUrl;
}
public String getPushType() {
return this.pushType;
}
public void setPushType(String pushType) {
this.pushType = pushType;
}
public String getWsNameSpace() {
return wsNameSpace;
}
public void setWsNameSpace(String wsNameSpace) {
this.wsNameSpace = wsNameSpace;
}
public String getWsMethodName() {
return wsMethodName;
}
public void setWsMethodName(String wsMethodName) {
this.wsMethodName = wsMethodName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public String getJsonType() {
return this.jsonType;
}
public void setJsonType(String jsonType) {
this.jsonType = jsonType;
}
public String getEnableFlag() {
return this.enableFlag;
}
public void setEnableFlag(String enableFlag) {
this.enableFlag = enableFlag;
}
@DateTimeFormat
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Long getCreateBy() {
return createBy;
}
public void setCreateBy(Long createBy) {
this.createBy = createBy;
}
@DateTimeFormat
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
public Long getModifyBy() {
return modifyBy;
}
public void setModifyBy(Long modifyBy) {
this.modifyBy = modifyBy;
}
public String getRemark() {
return this.remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getWsHeaderNameSpace() {
return wsHeaderNameSpace;
}
public void setWsHeaderNameSpace(String wsHeaderNameSpace) {
this.wsHeaderNameSpace = wsHeaderNameSpace;
}
public String getWsHeaderPrefix() {
return wsHeaderPrefix;
}
public void setWsHeaderPrefix(String wsHeaderPrefix) {
this.wsHeaderPrefix = wsHeaderPrefix;
}
public String getWsMethodPrefix() {
return wsMethodPrefix;
}
public void setWsMethodPrefix(String wsMethodPrefix) {
this.wsMethodPrefix = wsMethodPrefix;
}
public String getParamPrefix() {
return paramPrefix;
}
public void setParamPrefix(String paramPrefix) {
this.paramPrefix = paramPrefix;
}
public String getParamReturn() {
return paramReturn;
}
public void setParamReturn(String paramReturn) {
this.paramReturn = paramReturn;
}
@Override
public String toString() {
return "PushConfig [pushId=" + pushId + ", pushName=" + pushName
+ ", systemName=" + systemName + ", pushUrl=" + pushUrl
+ ", pushType=" + pushType + ", wsNameSpace=" + wsNameSpace
+ ", wsMethodName=" + wsMethodName + ", userName=" + userName
+ ", userPassword=" + userPassword + ", param=" + param
+ ", jsonType=" + jsonType + ", enableFlag=" + enableFlag
+ ", createTime=" + createTime + ", createBy=" + createBy
+ ", modifyTime=" + modifyTime + ", modifyBy=" + modifyBy
+ ", remark=" + remark + ", wsHeaderNameSpace="
+ wsHeaderNameSpace + ", wsHeaderPrefix=" + wsHeaderPrefix
+ ", wsMethodPrefix=" + wsMethodPrefix + ", paramPrefix="
+ paramPrefix + ", paramReturn=" + paramReturn + "]";
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2015年12月15日 17:26:30 星期二
http://fanshuyao.iteye.com/
上一篇: 农夫的水罐,乔治和母老虎,猎人的猎物
下一篇: 对于冷笑话好评如潮
推荐阅读
-
PHP调用JAVA的WebService简单实例
-
免费webservice测试工具(java调用webservice接口)
-
免费webservice测试工具(java调用webservice接口)
-
WebService的创建和部署以及通过反射动态调用WebService
-
动态webservice调用接口并读取解析返回结果
-
C#动态webservice调用接口
-
PHP调用JAVA的WebService简单实例
-
axis2通过城市名称调用.net写的asmx WebService查询天气实例
-
动态调用动态语言之Java脚本API 脚本JavaJavaScriptjrubyRuby
-
java swing调用webservice实现qq在线查询是否在线