您的位置:首页 > 数据库

WebService客户端,接收数据解析存入数据库

2015-03-26 09:27 281 查看
WebService客户端,实现的功能是接收数据,按格式解析数据,最后存入相应数据库。

需求:

同步设备信息接口

url:http://localhost:54059/Service1.asmx

函数名:GetDevConfigInfo

参数:无

返回值:string

返回值格式:

设备编号,设备名称,设备类型,设备IP,设备端口号,设备位置,安装时间;设备编号,设备名称,设备类型,设备IP,设备端口号,设备位置,安装时间;

思路分析:通过WSDL文件生成客户端Client,通过客户端Client得到数据流。Dao层实现对数据流的解析和把数据插入数据库的功能。Servive层调用Client和Dao实现把接收的数据解析存入数据库的功能。Controller层做了一个按钮触发,点击按钮执行Service。



导入WSDL文件

File---New---Other---Web Service Client



两种方式导入WSDL文件。如果有.wsdl源文件可以通过WSDL File选取源文件路径;或者选取WSDL URL。



点击Next----再点击Finish

WSDL文件

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="itarge-park" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="itarge-park" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="itarge-park">
<s:element name="GetDevConfigInfo">
<s:complexType />
</s:element>
<s:element name="GetDevConfigInfoResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetDevConfigInfoResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="GetDevConfigInfoSoapIn">
<wsdl:part name="parameters" element="tns:GetDevConfigInfo" />
</wsdl:message>
<wsdl:message name="GetDevConfigInfoSoapOut">
<wsdl:part name="parameters" element="tns:GetDevConfigInfoResponse" />
</wsdl:message>
<wsdl:portType name="Service1Soap">
<wsdl:operation name="GetDevConfigInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">This method get all dev config from databases;</wsdl:documentation>
<wsdl:input message="tns:GetDevConfigInfoSoapIn" />
<wsdl:output message="tns:GetDevConfigInfoSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="Service1Soap" type="tns:Service1Soap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="GetDevConfigInfo">
<soap:operation soapAction="itarge-park/GetDevConfigInfo" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="Service1Soap12" type="tns:Service1Soap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="GetDevConfigInfo">
<soap12:operation soapAction="itarge-park/GetDevConfigInfo" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Service1">
<wsdl:port name="Service1Soap" binding="tns:Service1Soap">
<soap:address location="http://localhost:54059/Service1.asmx" />
</wsdl:port>
<wsdl:port name="Service1Soap12" binding="tns:Service1Soap12">
<soap12:address location="http://localhost:54059/Service1.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


客户端  GetDevConfigClient.java

package com.cnc.park.client;
import itarge_park.Service1Soap;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import com.cnc.park.service.DeviceClientService;

/**
* author:zhangxuan
*/
public class GetDevConfigClient {
@Autowired
private DeviceClientService deviceClientService;

public static String get() {

JaxWsProxyFactoryBean  factoryBean=new JaxWsProxyFactoryBean();
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.setServiceClass(Service1Soap.class);
factoryBean.setAddress("http://localhost:54059/Service1.asmx");
Service1Soap impl=(Service1Soap) factoryBean.create();
String result = impl.getDevConfigInfo();

// String result = "0001,测试1,1,10.1.1.1,1111,1,2015-03-23 11:11:11;0002,测试2,2,10.2.2.2,2222,2,2015-03-23 22:22:22;";测试用
return result;
}

}


Domain层 DeviceClientDomain.java

package com.cnc.park.domain;
/**
*
* @author zhangxuan
* @date 2015年3月25日
* @time 下午4:42:50
*/
public class DeviceClientDomain {

private String devId;
private String devName;
private String devType;
private String devIp;
private String devPort;
private String installPos;
private String installTime;

public String getDevId() {
return devId;
}
public void setDevId(String devId) {
this.devId = devId;
}
public String getDevName() {
return devName;
}
public void setDevName(String devName) {
this.devName = devName;
}
public String getDevType() {
return devType;
}
public void setDevType(String devType) {
this.devType = devType;
}
public String getDevIp() {
return devIp;
}
public void setDevIp(String devIp) {
this.devIp = devIp;
}
public String getDevPort() {
return devPort;
}
public void setDevPort(String devPort) {
this.devPort = devPort;
}
public String getInstallPos() {
return installPos;
}
public void setInstallPos(String installPos) {
this.installPos = installPos;
}
public String getInstallTime() {
return installTime;
}
public void setInstallTime(String installTime) {
this.installTime = installTime;
}

public String toString(){
return devId +"\t"+ devName +"\t"+ devType +"\t"+"\t"
+devIp+"\t"+devPort+"\t"+installPos+"\t"+installTime;
}
}

Dao层 GetDevConfigClientDao.java

package com.cnc.park.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cnc.park.domain.DeviceClientDomain;
import com.cnc.park.tools.Log;
import com.cnc.park.tools.MyUtils;
/**
*
* @author zhangx
* @date 2015年3月4日
* @time 下午15:08:18
*/
@Repository
public class GetDevConfigClientDao {
@Autowired
private JdbcTemplate jdbcTemplate;

private static Logger logger=Log.getLog(GetDevConfigClientDao.class.getName());

public void split(String result){

try {
<strong>String[] devConfigList=result.split(";");
for(String devConfig:devConfigList){
<strong>String[] dev=devConfig.split(",");</strong>
DeviceClientDomain device = new DeviceClientDomain();
device.setDevId(dev[0]);
device.setDevName(dev[1]);
device.setDevType(dev[2]);
device.setDevIp(dev[3]);
device.setDevPort(dev[4]);
device.setInstallPos(dev[5]);
device.setInstallTime(dev[6]);

String sql = "INSERT INTO tb_device(dev_id,dev_name,dev_type_id,dev_ip,dev_port,install_pos,install_time) VALUES(?,?,?,?,?,?,?)"
jdbcTemplate.update(sql,new Object[]{device.getDevId(),
device.getDevName(),
device.getDevType(),
device.getDevIp(),
device.getDevPort(),
device.getInstallPos(),
device.getInstallTime()});
}
} catch (DataAccessException e) {
logger.error("解析数据出错--->split");
logger.error(e);
}
}
}

Service层  DeviceClientService.java

package com.cnc.park.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnc.park.client.GetDevConfigClient;
import com.cnc.park.dao.DeviceDao;
import com.cnc.park.dao.GetDevConfigClientDao;
import com.cnc.park.domain.DeviceClientDomain;
import com.cnc.park.domain.DeviceDomain;
import com.cnc.park.model.TreeNode;
/**
*
* @author zhangxuan
* @date 2015年3月25日
* @time 下午5:46:57
*/
@Service
public class DeviceClientService {

@Autowired
private GetDevConfigClientDao getDevConfigClientDao;

public void split(){
String result=GetDevConfigClient.get();
getDevConfigClientDao.split(result);
}

public GetDevConfigClientDao getGetDevConfigClientDao() {
return getDevConfigClientDao;
}

public void setGetDevConfigClientDao(GetDevConfigClientDao getDevConfigClientDao) {
this.getDevConfigClientDao = getDevConfigClientDao;
}
}


Controller层


@RequestMapping(value="/dev_wsUpdate.action")
public @ResponseBody Object wsUpdate(){
deviceClientService.split();
System.out.println("WebService 更新数据!");
return "success";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐