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) {            }        }    }