protobuf&restlet

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值