之前开发都是JSON格式, 据说用这种格式的 安全, 输入输出全是二进制文件,且 数据占用内存小。
主要永远移动端数据传送。以下是代码:
controller: 请求的是实体:
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("系统异常"); } } }
查询实体 :
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"); } } }
返回实体:
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。 传输效率快 ,安全,
option java_package = "model.report";
message ReportS {
required string searchId = 1;
required int32 dateNum = 2;
required int32 type = 3;
}