找回密码
 立即注册
查看: 220|回复: 3

JAVA使用Protobuf

[复制链接]
发表于 2022-6-17 08:04 | 显示全部楼层 |阅读模式

  • IDEA安装Protobuf插件



  • 引入maven依赖
<dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.19.1</version>
</dependency>protobuf是目前比较新的版本,之前测试过程中使用3.9.1。发现生成的源代码中,某些函数报错,调用不了,是因为函数在该版本声明为protected,其他地方调用不了。

  • maven中protobuf plugin
                       <plugin>
                                <groupId>org.xolstice.maven.plugins</groupId>
                                <artifactId>protobuf-maven-plugin</artifactId>
                                <version>0.6.1</version>

                                <configuration><!-- proto文件目录 -->
                                        <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
                                        <!-- 生成的Java文件目录 -->
                                        <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
                                        <clearOutputDirectory>false</clearOutputDirectory>
                                        <!--<outputDirectory>${project.build.directory}/generated-sources/protobuf</outputDirectory>-->
                                </configuration>
                                <executions>
                                        <execution>
                                                <goals>
                                                        <goal>compile</goal>
                                                        <goal>test-compile</goal>
                                                </goals>
                                        </execution>
                                </executions>
                        </plugin>在maven中的build中的plugins中添加上面插件。
protoSourceRoot指定*.proto定义的消息文件路径。
outputDirectory指定输出的java文件地址。默认是输出到target中。
clearOutputDirectory是否清空输出文件,默认为是,如果是,outputDirectory下的项目工程会被清空

  • 定义消息文件
syntax = "proto3";

package protocol;

option go_package = "protocol";
option java_package = "com.kone.pbdemo.protocol";

message File {
  string name = 1;
  int32 size = 2;
}

  • 编译protobuf文件


可以查看文件输出



  • 测试
public class PbTest {
    public static void main(String[] args) {
        FileOuterClass.File file = FileOuterClass.File.newBuilder()
                .setName("fileName")
                .setSize(200)
                .build();
        System.out.println(file);
    }
}


  • proto文件导入插件显示问题
在*.proto文件中导入其他proto文件,提示未找到文件问题。


在Setting => Languates & Frameworks => Protocol Buffers中添加proto路径


找不到文件的提示就会消失:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2022-6-17 08:06 | 显示全部楼层
为什么Message.proto编译后的文件为Message.java,但是另一个却带OuterClass的后缀?
发表于 2022-6-17 08:10 | 显示全部楼层
为什么Message.proto编译后的文件为Message.java,但是另一个却带OuterClass的后缀?
发表于 2022-6-17 08:14 | 显示全部楼层
那是file.proto的
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-26 20:39 , Processed in 0.094563 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表