小言_互联网的博客

基于CXF框架的WebService(xml 传输数据)

282人阅读  评论(0)
  1. Service 服务端
    接口JyDataAddService

    实现类JyDataAddServiceImp
@SuppressWarnings("serial")
@WebService(endpointInterface = "com.zytx.cxf.ws.service.JyDataAddService", serviceName = "JyDataAdd") 
public class JyDataAddServiceImp implements JyDataAddService {

private static final Logger logger = Logger.getLogger(JyDataAddServiceImp.class);
	
	ApplicationContext context;
	
	private IDevice  idevice;

	@Override
	public String jyDataAdd(String xmlStr) {
		idevice = (IDevice) SpringContextUtil.getBean("idevice"); 
		JYResult  jYResult  = new JYResult(0,0);
		int reason = 0;
		if(!"".equals(xmlStr)){
			JyAddInfo info =null;
			info = xmlToObject(xmlStr);
			if(info != null){  //XML转换成功
			   reason = serverYanZheng(info);
			   if(reason == 0){   //验证通过
				  if(idevice.jyDataAdd(info))
				  {   
					  jYResult  = new JYResult();
					  jYResult.setResult(1);
					  jYResult.setReason(reason);
				  }
				  else{
					  jYResult  = new JYResult();
					  jYResult.setResult(0);
					  jYResult.setReason(4);
				  }
			    }
			   else{     //验证没有通过
				      jYResult  = new JYResult();
					  jYResult.setResult(0);
					  jYResult.setReason(reason); 
			   }  
			 }
			}
			else{ 
				  jYResult.setResult(0);
				  jYResult.setReason(5); 
	}
		return  objectToXml(jYResult);
	}
	
	private int serverYanZheng(JyAddInfo info){
		int reason = 0;
		String userName ="";
		String password ="";
		
		String registDate="";
		
		int jyCompanyId = 0;
		if(info != null){
			userName = info.getUserName();
			password = info.getPassword();
			registDate = info.getRegistDate();
			
			if(("tjy".equals(userName) && "tjy".equals(password)) || ("tjy2".equals(userName) && "tjy2".equals(password)))
			{
				if(!isValidDate(registDate)){  //日期格式验证	
					reason = 2;
					return reason;
				}
				
			}
			else{       //用户名密码不对
				reason =1;
				return reason;
			}
		 }	
		return reason;
	}
	
	private boolean isValidDate(String sDate) {  //验证日期格式
	     String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
	     String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
	             + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
	             + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
	             + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
	             + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
	             + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
	     if ((sDate != null)) {
	         Pattern pattern = Pattern.compile(datePattern1);
	         Matcher match = pattern.matcher(sDate);
	         if (match.matches()) {
	             pattern = Pattern.compile(datePattern2);
	             match = pattern.matcher(sDate);
	             return match.matches();
	         }
	         else {
	             return false;
	         }
	     }
	     return false;
	}
	
	
	//xml转换成对象
	private  JyAddInfo xmlToObject(String xmlStr){
		JyAddInfo info =null;
		XStream xstream = new XStream(); 
		xstream.alias("info",JyAddInfo.class);
		info =(JyAddInfo)xstream.fromXML(xmlStr);
		return info;
	}
	
	//对象转换成xml
	private String objectToXml(JYResult info){
		System.out.println("将结果对象转换为xml!\n"); 
		XStream xstream = new XStream();
		xstream.alias("info",JYResult.class);
		String xml = xstream.toXML(info); 
        System.out.println(xml); 
        return xml;
	}
}
  1. Client 客户端
    客户端用apache-cxf-2.7.18自动生成
    apache-cxf-2.7.18自动生成链接

转载:https://blog.csdn.net/qq_29451823/article/details/102485144
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场