以前開發都是JSON格式, 聽說用這種格式的 安全, 輸入輸出全是二進制文件,且 數據佔用內存小。java
主要永遠移動端數據傳送。如下是代碼:web
controller: 請求的是實體:spring
package cn.ycmedia.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.RequestEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import cn.ycmedia.common.utils.result.ResultUtils; import cn.ycmedia.model.ad.CreativeRequest.CreativeMsg; import cn.ycmedia.model.report.ReportSearch; import cn.ycmedia.model.report.ReportSearch.ReportS; import cn.ycmedia.service.ReportService; /** * @date * @author zhuliangxing * 報表 * */ @RestController @RequestMapping("report") public class ReportController { private static Logger logger = LoggerFactory.getLogger(ReportController.class); @Autowired private ReportService reportService; /** * 獲取報表數據 * @param dateNum 天數 * @param searchId 查詢ID , 根據type 1 表明 用戶UID 2 表明廣告ID * @param type * @return */ @RequestMapping(value = "reportData", method = RequestMethod.POST) public String getReportData(RequestEntity<ReportS> requestEntity) { try { return ResultUtils.success("查詢成功",reportService.findReportData(requestEntity) ); } catch (Exception e) { logger.error(e.getMessage()); return ResultUtils.error("系統異常"); } } }
查詢實體 :json
option java_package = "model.report"; message ReportS { required string searchId = 1; required int32 dateNum = 2; required int32 type = 3; }
service:安全
package cn.ycmedia.service; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.RequestEntity; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import cn.ycmedia.common.ThirdUrlConstants; import cn.ycmedia.common.utils.HttpUtils; import cn.ycmedia.dao.ReportDao; import cn.ycmedia.model.report.ReportInfo; import cn.ycmedia.model.report.ReportInfo.ReportInfoClass; import cn.ycmedia.model.report.ReportSearch; import cn.ycmedia.model.report.ReportSearch.ReportS; /** * @author 朱良興 * */ @Service("reportService") public class ReportService { @Autowired private ReportDao reportDao; public Object findReportData(RequestEntity<ReportS> requestEntity) { ReportS searCh =requestEntity.getBody(); Map<String,String> map =new HashMap<String,String>(2); map.put("days",String.valueOf(searCh.getDateNum())); map.put(searCh.getType()==1?"uid":"appAdId", searCh.getSearchId()); String data = HttpUtils.httpPost(searCh.getType()==1?ThirdUrlConstants.REPORT_USER_URL:ThirdUrlConstants.REPORT_AD_URL, map); JSONObject json = JSONObject.parseObject(data); //調用智橙接口查詢成功 if(json.getInteger("code")==200){ ReportInfo.ReportInfoClass.Builder builder =ReportInfo.ReportInfoClass.newBuilder(); builder.setClick(json.getInteger("click")); builder.setCost(json.getString("cost")); builder.setPv(json.getIntValue("pv")); //循環詳細數據 JSONArray jsonArr = json.getJSONArray("days"); for (Object jsonDetail:jsonArr) { JSONObject detailData=(JSONObject)jsonDetail; builder.addDetail(ReportInfo.ReportInfoClass.DataDetail.newBuilder(). setClick(detailData.getIntValue("click")) .setCost(detailData.getString("cost")) .setPv(detailData.getIntValue("pv"))); } ReportInfoClass info =builder.build(); return info.toByteArray(); }else{ return json.getInteger("code")+":"+json.getString("Msg"); } } }
返回實體:app
option java_package = "model.report"; option java_outer_classname = "ReportInfo"; message ReportInfoClass { required string cost = 1; required int32 pv = 2; required int32 click = 3; message DataDetail { required int32 pv = 1; required int32 click = 2; required string cost = 3; } repeated DataDetail detail = 4; }
==================================大數據
固然我我的仍是以爲JSON好點, 主要是習慣了,可是大數據的狀況, 好比100M的JSON數據, 若是是protoBuf 只有 50M。 傳輸效率快 ,安全,ui
option java_package = "model.report";
message ReportS {
required string searchId = 1;
required int32 dateNum = 2;
required int32 type = 3;
}spa