找回密码
 立即注册
查看: 338|回复: 0

java protobuf

[复制链接]
发表于 2022-4-1 11:04 | 显示全部楼层 |阅读模式
java 对象和proto转换
public class Test {        public static void main(String[] args) throws IOException {                //模拟将对象转成byte[],方便传输                PersonEntity.Person.Builder builder = PersonEntity.Person.newBuilder();                builder.setId(1);                builder.setName("ant");                builder.setEmail("ghb@soecode.com");                PersonEntity.Person person = builder.build();                System.out.println("before :"+ person.toString());                System.out.println("===========Person Byte==========");                for(byte b : person.toByteArray()){                        System.out.print(b);                }                System.out.println();                System.out.println(person.toByteString());                System.out.println("================================");               //模拟接收Byte[],反序列化成Person类                byte[] byteArray =person.toByteArray();                Person p2 = Person.parseFrom(byteArray);                System.out.println("after :" +p2.toString());        }}
依赖pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <parent>        <artifactId>bcp-session-parent</artifactId>        <groupId>com.baidu.cpd</groupId>        <version>1.0.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>bcp-session-proto</artifactId>    <properties>        <maven.compiler.source>11</maven.compiler.source>        <maven.compiler.target>11</maven.compiler.target>        <os-maven-plugin.version>1.5.0.Final</os-maven-plugin.version>        <protobuf.version>2.5.0</protobuf.version>        <protoc.version>2.6.1</protoc.version>    </properties>    <dependencies>        <dependency>            <groupId>com.google.protobuf</groupId>            <artifactId>protobuf-java</artifactId>            <version>${protobuf.version}</version>        </dependency>    </dependencies>    <build>        <extensions>            <extension>                <groupId>kr.motd.maven</groupId>                <artifactId>os-maven-plugin</artifactId>                <version>${os-maven-plugin.version}</version>            </extension>        </extensions>        <plugins>            <plugin>                <groupId>org.xolstice.maven.plugins</groupId>                <artifactId>protobuf-maven-plugin</artifactId>                <version>0.6.1</version>                <executions>                    <execution>                        <goals>                            <goal>compile</goal>                            <goal>test-compile</goal>                        </goals>                    </execution>                </executions>                <configuration>                    <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.6</source>                    <target>1.6</target>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-source-plugin</artifactId>                <version>3.0.1</version>                <executions>                    <execution>                        <id>attach-sources</id>                        <phase>verify</phase>                        <goals>                            <goal>jar-no-fork</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-5-9 09:59 , Processed in 0.185978 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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