spring boot restTemplate protobuf(protocal buffer) demo

protobuf 个人理解就是一种数据格式,只不过在序列化后的大小、序列化、反序列化方面更优秀一些。


demo 是用spring boot做了一个server,一个test。server暴露出来的服务是rest的,数据格式就是protobuf。


直接上代码

common:

proto数据结构文件

 syntax = "proto3";

// package comp.protobuf.pb;

 option java_package = "com.protobuf.model";
 
 option java_outer_classname = "UserProto";

 message User {
   int64 id = 1;
   string name = 2;
   message PhoneNumber {
       string number = 1;
     }
     repeated PhoneNumber phone = 4;
     
 }


生成对应语言的类,我这直接用java 的runtime,也可以直接再cmd中操作

package com.protobuf;

import java.io.IOException;

public class GenerateClass {
	
	
	public static void main(String[] args) {
        String protoFile = "user.proto";//  
        String strCmd = "D:/software/protobuf-3.3.0/protoc.exe --java_out=./src/main/java ./src/main/resources/"+ protoFile;  
        try {
            Runtime.getRuntime().exec(strCmd);
        } catch (IOException e) {
            e.printStackTrace();
        }//通过执行cmd命令调用protoc.exe程序  
    }

}

生成的java类文件 UserProto.java




2. server   port:8080

application, controller

package com.protobuf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.web.bind.annotation.PathVariable;
import 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值