protobuf&restlet

來源:互聯網
上載者:User
文章目錄
  • 定義proto檔案
  • 產生java類
  • 組裝返回
  • 接收請求傳回值
定義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) {            }        }    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.