1.IDEA 下protobuf 使用(Java)

1.环境

版本: spring boot 2.2.5.RELEASE
语言:Java

2.安装IDEA插件

Protobuf Support | GoogleProtobufTool

Protobuf Support
  • Full Proto3 support.
  • Custom include path for proto files.
  • Usage search for messages, enums and fields (for standard and custom options).
  • Syntax validation for proto2/proto3. Checks for reserved/duplicated field tags and names. Highlight unresolved reference errors.
  • Fonts & Colors configuration.
  • Structure View.
  • Code formatting.
  • Navigation to message, enum or service by name (Ctrl+N)
    Rename refactoring (files, messages, enums and fields).
    Spell checking.
GoogleProtobufTool
  • generate java protobuf plugin for idea. generate one or more file.

3.配置pom文件

protobuf-java


  com.google.protobuf
  protobuf-java
  3.12.2
 

grpc


  io.grpc
  grpc-netty
  1.29.0


  io.grpc
  grpc-protobuf
   1.29.0
 
 
   io.grpc
   grpc-stub
   1.29.0

4.生成Java

插件配置

pom设置


  
    
        kr.motd.maven
        os-maven-plugin
        1.4.1.Final
     
  
  
    
        org.xolstice.maven.plugins
        protobuf-maven-plugin
        0.5.0
        
            
                com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}
            
            grpc-java
            
                io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
            
        
        
            
                
                    compile
                    compile-custom
                
            
        
    
  

proto代码

  • src main 目录下新建的proto文件夹下,创建UserModel.proto文件
syntax = "proto3";
option java_outer_classname = "UserModel";

message User{
     int32 id = 1;
     string name = 2;
     string sex= 3;
}
  • 执行生成(protobuf:compile-javano)
  • 生成Java文件(/target/generated-sources/protobuf/java/*)

你可能感兴趣的:(1.IDEA 下protobuf 使用(Java))