一、发出请求

public String test(){
       List<CompanyResPool> list=new ArrayList<CompanyResPool>();
       CompanyResPool comResPool = new CompanyResPool();
        
        String companyIdStr=companyId+"";
		int vrBandwidth=0;
		String ip_num="0";
		List<String> ipList=new ArrayList<String>();
		String vr_service="暂无";
		List<String> vr_eip=new ArrayList<String>();
		List<String> vr_snat=new ArrayList<String>();
		List<String> vr_dnat=new ArrayList<String>();
		List<String> vr_total=new ArrayList<String>();
			
		String vr_userNum="0";			
		String vr_totalNum="0";
	    String Url2 = "http://" +nodeIp+ "/BackManagerNode/VrouterApi_getBandwithByCompanyId";
		String Str2 = HttpRequest.sendGet(Url2, "companyId="+companyIdStr+"&poolid="+comResPool.getId());
			
		if (StringUtils.isNotBlank(Str2)) {
			JSONObject resultObj = JSONObject.fromObject(Str2);
			vrBandwidth = resultObj.getInt("VR_BandWidth");
			vr_service=resultObj.getString("VR_Service");
			vr_userNum=resultObj.getString("IP_usedNum");
			vr_totalNum=resultObj.getString("IP_totalNum");
			vr_eip=(List<String>)resultObj.get("VR_eip");
			vr_snat=(List<String>)resultObj.get("VR_snatip");
			vr_dnat=(List<String>)resultObj.get("VR_dnatip");
			vr_total=(List<String>)resultObj.get("IP_total");
		}
					
		others.put("vrBandwidth", vrBandwidth);
		others.put("vr_service", vr_service);
		others.put("vr_eip", vr_eip);
		others.put("vr_snat", vr_snat);
		others.put("vr_dnat", vr_dnat);
		others.put("IP_usedNum", vr_userNum);
		others.put("IP_total", vr_total);
		others.put("IP_totalNum", vr_totalNum);
        comResPool.setOthers(others);
    list.add(comResPool);


        JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,
			new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));
		jsonConfig.setExcludes(new String[]{"node_capacity"});
			
		generateSuccessListResponse(JSONArray.fromObject(newComResList,jsonConfig));
    return JSON;
}

 

二、处理请求

// 根据companyId查询Bandwidth
	public void getBandwithByCompanyId() {
		JSONObject json = new JSONObject();
		VRouter vr=vRouterService.getBandwithByCompanyId(companyId+"");
		//获取分配给SLB的IP
		List<Ipv4Entity> list = ipv4Service.getByPoolId(poolid+"");
		List<String> ipList=new ArrayList<String>();
		
		if(list!=null&&list.size()!=0){
			for(Ipv4Entity ip:list){
				ipList.add(ip.getIpv4_address());
			}
		}
		
		List<String> dnatIp=new ArrayList<String>();
		List<String> eipIp=new ArrayList<String>();
		List<String> snatIp=new ArrayList<String>();
		
		if(vr!=null){
			VRouterEntity vRouterEntity = vRouterEntityService.getByUUID(vr.getUUID());
			boolean flag=false;
			String str="";
			//公网:
			//1.eip(若同时有eip和dnat,显示功能为eip)
			List<EIPInfoEntity> eipList = eipInfoService.getByVrUUID(vr.getUUID());
			
			for(int i=0;i<eipList.size();i++){
				if (eipList.get(i) != null && "1".equals(eipList.get(i).getStatus())) {
					eipIp.add(eipList.get(i).getWanip());
				}
				
			}
			//2.dnat(有 多个,也可能为同一个)
			List<DnatEntity>  dnatList=dnatService.getByVruuid(vr.getUUID());
			for(int i=0;i<dnatList.size();i++){
				if (dnatList.get(i) != null && "1".equals(dnatList.get(i).getStatus())) {
					dnatIp.add(dnatList.get(i).getWanip());
				}
				
			}
			
			//3.snat(一个;若同时有dnat和snat,显示功能为snat)
			List<SnatEntity>  snatList=snatService.getByVruuid(vr.getUUID());
			for(int i=0;i<snatList.size();i++){
				if(snatList.get(i)!=null&&"1".equals(snatList.get(i).getStatus())){
					snatIp.add(snatList.get(i).getWan());
				}
				
			}
			
			System.out.println("dnatIp="+dnatIp);
			System.out.println("eipIp="+eipIp);
			System.out.println("snatIp="+snatIp);
			//若dnat有多个,只保留一个
			dnatIp=removeSelf(dnatIp);
			//若同时有eip和dnat,那么删除dnat里面的ip
			dnatIp=removeDuplicate(dnatIp, eipIp);
			//若同时有dnat和snat,删除dnat里面的ip
			dnatIp=removeDuplicate(dnatIp, snatIp);
			
			json.accumulate("VR_BandWidth", vr.getBandWidth());
			//vr的管理网ip
			if(vRouterEntity!=null&&(!"".equals(vRouterEntity.getControl_ip()))){
				json.accumulate("VR_Service", vRouterEntity.getControl_ip());
			}else{
				json.accumulate("VR_Service", "暂无");
			}
			json.accumulate("VR_eip", eipIp);
			json.accumulate("VR_snatip", snatIp);
			json.accumulate("VR_dnatip", dnatIp);
			
			if(eipIp!=null||snatIp!=null||dnatIp!=null){
				json.accumulate("IP_usedNum", eipIp.size()+snatIp.size()+dnatIp.size());
			}else{
				json.accumulate("IP_usedNum", "0");
			}
			json.accumulate("IP_total", ipList);
			
			if(ipList!=null&&ipList.size()!=0){
				json.accumulate("IP_totalNum", ipList.size());
			}else{
				json.accumulate("IP_totalNum", "0");
			}
			
		}else{
			json.accumulate("VR_BandWidth", "0");
			json.accumulate("VR_Service", "暂无");
			json.accumulate("VR_eip", eipIp);
			json.accumulate("VR_snatip", snatIp);
			json.accumulate("VR_dnatip", dnatIp);
			json.accumulate("IP_usedNum", "0");
			json.accumulate("IP_total", ipList);
			json.accumulate("IP_totalNum","0");
			
		}

		responseData(json.toString());
	}
	

 

感悟:最近的工作比较多的接触json,发现json在数据传输上去,确实要比一般的List方便很多。页面展示也比较轻松。

GitHub 加速计划 / js / json
58
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
568b708f Bumps [step-security/harden-runner](https://github.com/step-security/harden-runner) from 2.12.0 to 2.12.1. - [Release notes](https://github.com/step-security/harden-runner/releases) - [Commits](https://github.com/step-security/harden-runner/compare/0634a2670c59f64b4a01f0f96f84700a4088b9f0...002fdce3c6a235733a90a27c80493a3241e56863) --- updated-dependencies: - dependency-name: step-security/harden-runner dependency-version: 2.12.1 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 4 天前
c633693d Bumps [ossf/scorecard-action](https://github.com/ossf/scorecard-action) from 2.4.1 to 2.4.2. - [Release notes](https://github.com/ossf/scorecard-action/releases) - [Changelog](https://github.com/ossf/scorecard-action/blob/main/RELEASE.md) - [Commits](https://github.com/ossf/scorecard-action/compare/f49aabe0b5af0936a0987cfb85d86b75731b0186...05b42c624433fc40578a4040d5cf5e36ddca8cde) --- updated-dependencies: - dependency-name: ossf/scorecard-action dependency-version: 2.4.2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 13 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐