WebService客戶端,接收數據解析存入數據庫

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";
	}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章