Protobuf & restlet

Source: Internet
Author: User
Document directory
  • Define the proto File
  • Generate Java class
  • Assembly return
  • Receive request return value
Define the proto File
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;}~     

Generate Java class

 protoc --java_out=./ HotelCommentDO.proto

Assembly return

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();            }        };    }
Receive request return value
    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) {            }        }    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.