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文件
This method get all dev config from databases;
客户端 GetDevConfigClient.java
package c.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 c.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,-03-23 11:11:11;0002,测试2,2,10.2.2.2,2222,2,-03-23 22:22:22;";测试用
return result;
}
}
Domain层DeviceClientDomain.java
package c.park.domain;
/**
*
* @author zhangxuan
* @date 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 c.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 c.park.domain.DeviceClientDomain;
import c.park.tools.Log;
import c.park.tools.MyUtils;
/**
*
* @author zhangx
* @date 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 {
String[] devConfigList=result.split(";");
for(String devConfig:devConfigList){
String[] dev=devConfig.split(",");
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 c.park.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import c.park.client.GetDevConfigClient;
import c.park.dao.DeviceDao;
import c.park.dao.GetDevConfigClientDao;
import c.park.domain.DeviceClientDomain;
import c.park.domain.DeviceDomain;
import c.park.model.TreeNode;
/**
*
* @author zhangxuan
* @date 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";
}
如果觉得《java webservice 接收数据_WebService客户端 接收数据解析存入数据库》对你有帮助,请点赞、收藏,并留下你的观点哦!