欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Mac下IDEA安裝并使用protobuf方式(Java)

 更新時(shí)間:2023年11月22日 09:21:35   作者:小楊同學(xué)的筆記本  
這篇文章主要介紹了在Mac下IDEA安裝并使用protobuf方式(Java),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

安裝插件

引入依賴

    <dependencies>
    	<!--這個(gè)是netty的依賴包,可以不引用-->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.20.Final</version>
        </dependency>

        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>1.23.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>1.23.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty</artifactId>
            <version>1.23.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>1.23.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-okhttp</artifactId>
            <version>1.23.0</version>
        </dependency>
        
    </dependencies>


    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.5.0.Final</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.0</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.23.0:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

編寫(xiě)Student.proto文件

需要在main包下新建一個(gè)proto包,然后把proto文件放包此包里

syntax = "proto3";  //版本
option java_outer_classname = "StudentPOJO";    //生成的外部類名,同時(shí)也是文件名

//protobuf 使用massage管理數(shù)據(jù)
//會(huì)在 StudentPOJO 外部類生成一個(gè)內(nèi)部類 Student,這個(gè)才是真正發(fā)送的POJO對(duì)象
message Student {
  int32 id = 1; //Student類中有一個(gè)屬性名為id,類型為int32(protobuf類型),1表示屬性序號(hào),不是值
  string name = 2;
}

proto類型

生成文件

新建對(duì)象

把生成的java文件放到項(xiàng)目的pojo包里,然后就可以新建對(duì)象了。

StudentPOJO.Student stu1 = StudentPOJO.Student.newBuilder().setId(1).setName("張三").build();

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論