protobuf&restlet
来源:互联网 发布:人工智能的企业 编辑:程序博客网 时间:2024/06/08 12:35
定义proto文件
package hotel;option java_package = "com.meituan.service.mobile.protobuf.hotel";option java_outer_classname = "HotelCommentProto";message HotelCommentList{ repeated HotelComment comments=1;}message HotelComment{ optional int64 id=1; optional int64 poi_id=2; optional int64 did=3; optional int64 user_id=4; optional string user_name=5; optional int32 score=6; optional string score_text=7; optional string comment=8; optional int64 feedback_time=9;}~
生成java类
protoc --java_out=./ HotelCommentDO.proto
组装返回
public Representation generateResult(List<HotelCommentDO> comments) { final HotelCommentProto.HotelCommentList.Builder listBuilder = HotelCommentProto.HotelCommentList .newBuilder(); try { if (!CollectionUtils.isEmpty(comments)) { for (HotelCommentDO comment : comments) { HotelCommentProto.HotelComment.Builder commentBuilder = HotelCommentProto.HotelComment .newBuilder(); commentBuilder.setId(comment.getId()); commentBuilder.setPoiId(comment.getPoiId()); commentBuilder.setUserName(comment.getUserName()); commentBuilder.setScore(comment.getScore()); commentBuilder.setScoreText(comment.getScoreText()); commentBuilder.setComment(comment.getComment()); commentBuilder.setFeedbackTime(comment.getFeedbackTime() .getTime()); listBuilder.addComments(commentBuilder); } } } catch (Exception e) { e.printStackTrace(); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { listBuilder.build().writeTo(bos); } catch (IOException e) { e.printStackTrace(); } return new OutputRepresentation(MediaType.APPLICATION_ALL) { @Override public void write(OutputStream outputStream) throws IOException { listBuilder.build().writeTo(outputStream); outputStream.flush(); } }; }
接收请求返回值
public static Object getUrlContent(String newurl, String charset, int readTimeOut, int connectionTimeOut, Map<String, String> headers) { java.io.InputStream inr = null; java.net.HttpURLConnection conn = null; try { conn = (java.net.HttpURLConnection) (new java.net.URL(newurl)) .openConnection(); if (headers != null) { for (Map.Entry<String, String> header : headers.entrySet()) conn.addRequestProperty(header.getKey(), header.getValue()); } conn.setReadTimeout(readTimeOut); conn.setConnectTimeout(connectionTimeOut); inr = conn.getInputStream(); return HotelCommentProto.HotelCommentList.parseFrom(inr); } catch (Exception e) { log.error("getUrlContent exception! url=" + newurl, e); return null; } finally { try { if (inr != null) inr.close(); if (conn != null) conn.disconnect(); } catch (IOException e) { } } }
- protobuf&restlet
- restlet
- restlet
- Restlet
- Restlet指南
- Restlet 指南
- restlet简介
- Restlet 第一步
- Restlet指南
- restlet简介
- Restlet overview
- Restlet 指南
- restlet学习心得
- Restlet学习
- restlet简介
- Restlet Introduction
- protobuf
- ProtoBuf
- 四面体体积 poj2208 hdu1411
- java操作csv文件(读、写)
- Java中的接口(interface)和抽象类(abstract class)
- epoll使用详解
- gcc 编译 c++
- protobuf&restlet
- db_name和db_unique_name的区别
- [MFC]选择目录对话框和选择文件对话框
- Linux环境下MySQL数据库大小写区分问题
- Linux内核配置
- Spring中ClassPathXmlApplicationContext类的简单使用
- 配置http服务 Apache
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
- Windows 7安装.net framework 4 安装