加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice 小记

发布时间:2020-12-16 22:41:16 所属栏目:安全 来源:网络整理
导读:需求: 实现在A系统展示B数据库中的数据,利用spring3.0 + axis2? 实现思路: 客户端:发送xml 参数,请求服务器端方法;接收来自服务器端的数据,保存到本地数据库。 服务器端:执行方法,返回数据到客户端。 实现过程客户端:package com.thunisoft.sy.webServic

需求:

实现在A系统展示B数据库中的数据,利用spring3.0 + axis2?

实现思路:

客户端:发送xml 参数,请求服务器端方法;接收来自服务器端的数据,保存到本地数据库。

服务器端:执行方法,返回数据到客户端。





实现过程客户端:
package com.thunisoft.sy.webService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.thunisoft.sy.dao.impl.SyDaoImpl;
import flex.messaging.io.ArrayList;


public class NPService {
private Logger logger = LoggerFactory.getLogger(NPService.class);


private SyDaoImpl syDao;
private NPDao npDao;
private String url;



/**
* 客户端构建xml参数,请求服务器端方法
* @return
*/
public String buildXML() {
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
StringBuffer sb = new StringBuffer();
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
sb.append("<params>");
sb.append("<ksrq>").append(today).append("</ksrq>");
sb.append("<jsrq>").append(today).append("</jsrq>");
sb.append("<fybh>").append("-1").append("</fybh>");
sb.append("<jglx>1,2,3,4</jglx>");
sb.append("</params>");
logger.info("调用传入参数:" + sb.toString());
Service service = new Service();
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName("ajNum");  //服务器端方法名,call.addParameter(
"queryParam",// 服务器端方法参数
org.apache.axis.encoding.XMLType.XSD_STRING,//参数类型
javax.xml.rpc.ParameterMode.IN);   //不是很懂,猜想是表示输入属性,表示queryParam是输入参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);  //服务器端返回数据类型
call.setUseSOAPAction(true);  
// call.setSOAPActionURI("http://vms.service.business.vms.thunisoft.com/insertVisitorInfo");
Object [] obj = new Object[] {sb.toString()};
String result = (String) call.invoke(obj);
logger.info("服务器端接口返回结果:" + result);
return result;
} catch (Exception ex) {
logger.error("请求服务器端方法失败",ex);
}

return null;
}

/**
* 客户端解析服务器端返回的xml结果,将数据保存到本地数据库
*/
@SuppressWarnings("unchecked")
public void execute() {
String xml = buildXML();
if (StringUtils.isNotBlank(xml)) {
Document doc = null;
try {
// 读取并解析XML文档
doc = DocumentHelper.parseText(xml); // 将字符串转为XML
Element root = doc.getRootElement(); // 获取根节点
Iterator<Element> records = root.elementIterator("fy"); // /获取根节点下的子节点body
List<String> list = new ArrayList();
// 遍历body节点
while (records.hasNext()) {
Element record = (Element) records.next();
String fybh = record.elementTextTrim("fybh");
String fyid = syDao.convertToFyid(fybh);
String xsajs = convert(record.elementTextTrim("xsajs"));
String yjajs = convert(record.elementTextTrim("yjajs"));
String wjajs = convert(record.elementTextTrim("wjajs"));
String jcajs = convert(record.elementTextTrim("jcajs"));
String today = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
StringBuffer sb = new StringBuffer(
"INSERT INTO DB_SY.T_NPAJXX(D_RQ,C_FYID,N_XSAJS,N_YJAJS,N_WJAJS,N_JCAJS) VALUES (");
sb.append("cast('" + today + "' as timestamp)").append(",");
sb.append("'").append(fyid).append("'").append(",");
sb.append(xsajs).append(",");
sb.append(yjajs).append(",");
sb.append(wjajs).append(",");
sb.append(jcajs);
sb.append(")");
list.add(sb.toString());
}
npDao.insertNPAjxx(list);
} catch (DocumentException e) {
logger.error("解析服务器端查询结果失败" + e);
}
}


}

private String convert(String value) {
if (StringUtils.isEmpty(value) || "NULL".equals(value.toUpperCase())) {
            return String.valueOf(0);
        }
return value;
}


public SyDaoImpl getSyDao() {
return syDao;
}


public void setSyDao(SyDaoImpl syDao) {
this.syDao = syDao;
}


public NPDao getNpDao() {
return npDao;
}


public void setNpDao(NPDao npDao) {
this.npDao = npDao;
}


public String getUrl() {
return url;
}


public void setUrl(String url) {
this.url = url;
}

}



客户端文件cong.properties 部分配置

#服务器端接口url
npWebService.url=http://172.16.215.216:8080/fy2000Interface/services/fy2000Service?wsdl

#每天执行时间
npTodayService.cron=*/20 * * * * ?

#注 :客户端请求服务器端的url规则为,服务器端项目发布路径/web.xml中<url-pattern>的值/server-config.wsdd中自定义服务service name的值?wsdl



客户端文件applicationContetxt.xml?部分配置

<!-- 每五分钟调用一下服务器端方法-->
<bean id="NPService" class="com.thunisoft.sy.webService.NPService">
<property name="syDao">
<ref bean="syDao" />
</property>
<property name="npDao">
<ref bean="npDao" />
</property>
<property name="url">
<value>${npWebService.url}</value>
</property>
</bean>
? ? <bean id="NPServiceTask"
? ? ? ? class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
? ? ? ? <property name="targetObject" ref="NPService" />
? ? ? ? <property name="targetMethod" value="execute" />
? ? ? ? ?<property name="concurrent" value="false" />
? ? </bean>
? ? <bean id="NPServiceTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
? ? ? ? <property name="jobDetail" ref="NPServiceTask" />
? ? ? ? <property name="cronExpression">
? ? ? ? <value>${npTodayService.cron}</value>
? ? ? ? </property>
? ? </bean>

?<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
? ? ? ? <property name="triggers">
? ? ? ? ? ? <list>
? ? ? ? ? ? ? ?<!-- ?<ref bean="spglJcsjTrigger" />
? ? ? ? ? ? ? ? <ref bean="reloadCronTrigger" /> -->
? ? ? ? ? ? ? ? <ref bean="NPServiceTrigger" />
? ? ? ? ? ? ? ? <!-- <ref bean="sfgkTodayServiceTrigger" />?
? ? ? ? ? ? ? ? <ref bean="sfgkMonthServiceTrigger" />
? ? ? ? ? ? ? ? <ref bean="sfgkYearServiceTrigger" />
? ? ? ? ? ? ? ? <ref bean="sfgkPreYearServiceTrigger" />
? ? ? ? ? ? ? ? <ref bean="checkFzbHdTrigger" />
? ? ? ? ? ? ? ? <ref bean="xfTodayTrigger" />
? ? ? ? ? ? </list>
? ? ? ? </property>
? ? </bean>



实现过程服务器端:
package com.thunisoft.webservice.service;


import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;


import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;


import com.thunisoft.summer.util.cache.CacheManager;
import com.thunisoft.webservice.cache.cacheManager;
import com.thunisoft.webservice.dao.Ify2000;
import com.thunisoft.webservice.daoImpl.Fy2000Impl;
import com.thunisoft.webservice.service.util.getBean;




public class Fy2000Service  extends ServletEndpointSupport  implements IFy2000Service{

private Ify2000 fyimpl;
private Logger log = Logger.getLogger(Fy2000Service.class);

/**
* 根据客户端请求参数执行方法,将查询结果返回
* @return
*/
public  String ajNum(String queryParam){
Map<String,String> csmap = new HashMap<String,String>();
Map<Integer,Integer> jcmap = new HashMap<Integer,Integer>();
Map<Integer,Integer> xsmap = new HashMap<Integer,Integer> yjmap = new HashMap<Integer,Integer> wjmap = new HashMap<Integer,Integer>();
ApplicationContext springcontext = getBean.getYouBean();
fyimpl= (Fy2000Impl) springcontext.getBean("fy2000");
csmap = fyimpl.readXml(queryParam);
String kssj = csmap.get("kssj");
String jssj = csmap.get("jssj");
jcmap = fyimpl.getJcMap(kssj,jssj);
xsmap = fyimpl.getXsMap(kssj,jssj);
yjmap = fyimpl.getYjMap(kssj,jssj);   
wjmap = fyimpl.getWjMap(kssj,jssj);
Map<Integer,Integer> fymap=null;
try {
fymap = ((cacheManager)CacheManager.getCache("fycache")).getFyvalueMap();
} catch (Exception e) {
log.error("获取法院缓存数据失败",e);
}
StringBuffer sb  = new StringBuffer();
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
sb.append("<result>");
for (Entry<Integer,Integer> enter:fymap.entrySet()) {
Integer fydm = enter.getKey();
if(null!=fydm){
Integer fyid = enter.getValue();
sb.append("<fy>");
sb.append("<fybh>"+fydm+"</fybh>");
sb.append("<xsajs>"+xsmap.get(fyid)+"</xsajs>");
sb.append("<yjajs>"+yjmap.get(fyid)+"</yjajs>");
sb.append("<wjajs>"+wjmap.get(fyid)+"</wjajs>");
sb.append("<jcajs>"+jcmap.get(fyid)+"</jcajs>");
sb.append("</fy>");
}
}
sb.append("</result>");

return sb.toString();
}
public Ify2000 getFyimpl() {
return fyimpl;
}
public void setFyimpl(Ify2000 fyimpl) {
this.fyimpl = fyimpl;
} 

}



服务器端文件applicationContext.xml?部分配置

<!-- 获得bean对象 工具类 -->
? ? <bean id = "applicationContextUtils" class="com.thunisoft.webservice.service.util.getBean"></bean>
? ?
? ? <!-- 法院基础数据写入缓存 -->
<bean id ="fymapCacheManager" class="com.thunisoft.summer.util.cache.CacheProxy">
<property name="proxyName" value="fycache"></property>
<property name="cacheImpl">
<value>com.thunisoft.webservice.cache.cacheManager</value>
</property>
<property name="initMethod" value="init">
</property>
<property name="writeMethod">
<list>
<value>reload</value>
</list>
</property>
</bean>

<!-- 缓存管理 -->
<bean id = "cacheManager" class="com.thunisoft.summer.util.cache.CacheManager" abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="cache">
<list>
<ref local="fymapCacheManager"/>
</list>
</property>
</bean>


服务器端文件web.xml?部分配置

?<!--axis 需要引入的 Servlet -->?
? ? ? ?<servlet>?
? ? ? ? ? ? ? <servlet-name>axis</servlet-name>?
? ? ? ? ? ? ? <servlet-class>?
? ? ? ? ? ? ? ? ? ? ?org.apache.axis.transport.http.AxisServlet?
? ? ? ? ? ? ? </servlet-class>?
? ? ? ? ? ? ? <load-on-startup>2</load-on-startup>?
? ? ? ?</servlet>?


? ? ? ?<servlet-mapping>?
? ? ? ? ? ? ? <servlet-name>axis</servlet-name>?
? ? ? ? ? ? ? <url-pattern>/services/*</url-pattern>?
? ? ? ?</servlet-mapping>?


服务器端 server-config.wsdd 配置文件,文件放在web 项目的WebRoot/WEN-INF 下

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">


<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" />

<!-- 系统服务 --> ?
? ? <service name="AdminService" provider="java:MSG"> ?
? ? ? ? <parameter name="allowedMethods" value="AdminService" /> ?
? ? ? ? <parameter name="enableRemoteAdmin" value="false" /> ?
? ? ? ? <parameter name="className" value="org.apache.axis.utils.Admin" /> ?
? ? ? ? <namespace>http://xml.apache.org/axis/wsdd/</namespace> ?
? ? </service> ?
? ? <service name="Version" provider="java:RPC"> ?
? ? ? ? <parameter name="allowedMethods" value="getVersion" /> ?
? ? ? ? <parameter name="className" value="org.apache.axis.Version" /> ?
? ? </service> ?
? ??
<!-- 自定义服务 -->
<service name="fy2000Service" provider="java:RPC"> <parameter name="className" value="com.thunisoft.webservice.service.Fy2000Service" /> <parameter name="allowedMethods" value="*" /> </service> <transport name="http"> <requestFlow> <handler type="URLMapper" /> </requestFlow> </transport> </deployment>?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读