准备
安装插件
新建立目录
新建目录proto(名称任意都可)并按图指为Sources Root
实现
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lylbp</groupId>
<artifactId>protobuf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>protobuf</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<grpc.version>1.12.0</grpc.version>
<protobuf.version>3.5.1</protobuf.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- protobuf依赖开始 -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<!-- protobuf依赖结束 -->
</dependencies>
<build>
<extensions>
<!-- protobuf extension开始 -->
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
<!-- protobuf extension结束 -->
</extensions>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.3.RELEASE</version>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- protobuf插件开始 -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- protobuf插件结束 -->
</plugins>
</build>
</project>
测试代码
proto目录下新建Person.proto文件
syntax = "proto3";
package com.lylbp.protobuf.test;
option java_outer_classname = "PersonBean";
message Person {
string name = 1;
int32 age = 2;
string gender = 3;
}
运行
点击maven运行后 到target中找到生成的代码 复制到Person.proto中所写的对应的package 即可
测试
package com.lylbp.protobuf.test;
import com.google.protobuf.InvalidProtocolBufferException;
public class Test {
public static void main(String[] args) {
PersonBean.Person.Builder builder = PersonBean.Person.newBuilder();
builder.setAge(15);
builder.setName("张三");
builder.setGender("男");
byte[] bytes = builder.build().toByteArray();
try {
PersonBean.Person person = PersonBean.Person.parseFrom(bytes);
System.out.println(person);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
|