Protostuff序列化和反序列化的使用說(shuō)明
大家都知道protobuf好用,可是在網(wǎng)上找到的netty整合protobuf的文章都是千篇一律,自己編寫proto文件然后使用工具轉(zhuǎn)java文件用起來(lái)復(fù)雜麻煩,經(jīng)過(guò)不懈努力終于找到了一個(gè)簡(jiǎn)單的方法希望大家喜歡。
google原生的protobuffer使用起來(lái)相當(dāng)麻煩,首先要寫.proto文件,然后編譯.proto文件,生成對(duì)應(yīng)的.java文件,鄙人試了一次,發(fā)現(xiàn)真的很麻煩。而protostuff的官方網(wǎng)站(http://www.protostuff.io/documentation/runtime-schema/),對(duì)于智商比較低的小編來(lái)說(shuō)也略顯生澀,于是鄙人就根據(jù)項(xiàng)目中用到的protostuff,撰寫此文,以方便自己和他人加深印象和學(xué)習(xí)。
1.實(shí)戰(zhàn)
1.maven依賴:
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.4.0</version>
</dependency>
2.ProtoBufUtil工具類:ProtoBufUtil.java
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
/**
* Created by zhangzh on 2017/2/20.
*/
public class ProtoBufUtil {
public ProtoBufUtil() {
}
public static <T> byte[] serializer(T o) {
Schema schema = RuntimeSchema.getSchema(o.getClass());
return ProtobufIOUtil.toByteArray(o, schema, LinkedBuffer.allocate(256));
}
public static <T> T deserializer(byte[] bytes, Class<T> clazz) {
T obj = null;
try {
obj = clazz.newInstance();
Schema schema = RuntimeSchema.getSchema(obj.getClass());
ProtostuffIOUtil.mergeFrom(bytes, obj, schema);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
}
3. bean類Student.java:
import io.protostuff.Tag;
/**
* Created by zhangzh on 2017/2/20.
*/
public class Student {
@Tag(1)
private String name;
@Tag(2)
private String studentNo;
@Tag(3)
private int age;
@Tag(4)
private String schoolName;
// 關(guān)于@Tag,要么所有屬性都有@Tag注解,要么都沒有,不能一個(gè)類中只有部分屬性有@Tag注解
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", studentNo='" + studentNo + '\'' +
", age=" + age +
", schoolName='" + schoolName + '\'' +
'}';
}
}
3.test類ProtoBufUtilTest.java:
import java.util.Arrays;
/**
* Created by zhangzh on 2017/2/20.
*/
public class ProtoBufUtilTest {
public static void main(String[] args) {
Student student = new Student();
student.setName("lance");
student.setAge(28);
student.setStudentNo("2011070122");
student.setSchoolName("BJUT");
byte[] serializerResult = ProtoBufUtil.serializer(student);
System.out.println("serializer result:" + Arrays.toString(serializerResult));
Student deSerializerResult = ProtoBufUtil.deserializer(serializerResult,Student.class);
System.out.println("deSerializerResult:" + deSerializerResult.toString());
}
}
4.輸出結(jié)果:
serializer result:[10, 5, 108, 97, 110, 99, 101, 18, 10, 50, 48, 49, 49, 48, 55, 48, 49, 50, 50, 24, 28, 34, 4, 66, 74, 85, 84]
deSerializerResult:Student{name='lance', studentNo='2011070122', age=28, schoolName='BJUT'}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
spring基于注解配置實(shí)現(xiàn)事務(wù)控制操作
這篇文章主要介紹了spring基于注解配置實(shí)現(xiàn)事務(wù)控制操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
"Method?Not?Allowed"405問(wèn)題分析以及解決方法
項(xiàng)目中在提交表單時(shí),提示“HTTP 405”錯(cuò)誤——“Method Not Allowed”這里顯示的是,方法不被允許,下面這篇文章主要給大家介紹了關(guān)于"Method?Not?Allowed"405問(wèn)題分析以及解決方法的相關(guān)資料,需要的朋友可以參考下2022-10-10
Spring WebFlux使用函數(shù)式編程模型構(gòu)建異步非阻塞服務(wù)
這篇文章主要介紹了Spring WebFlux使用函數(shù)式編程模型構(gòu)建異步非阻塞服務(wù),重點(diǎn)介紹如何使用函數(shù)式編程模型創(chuàng)建響應(yīng)式 RESTful 服務(wù),這種編程模型與傳統(tǒng)的基于 Spring MVC 構(gòu)建 RESTful 服務(wù)的方法有較大差別,感興趣的朋友跟隨小編一起看看吧2023-08-08
Spring MVC的文件上傳和下載以及攔截器的使用實(shí)例
這篇文章主要介紹了Spring MVC的文件上傳和下載以及攔截器的使用實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
IDEA安裝部署Alibaba Cloud Toolkit的實(shí)現(xiàn)步驟
Alibaba Cloud Toolkit是阿里云針對(duì)IDE平臺(tái)為開發(fā)者提供的一款插件,本文主要介紹了IDEA安裝部署Alibaba Cloud Toolkit的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
SpringBoot應(yīng)用部署到外置Tomcat的實(shí)現(xiàn)
SpringBoot內(nèi)置tomcat使用很方便,本文主要介紹了SpringBoot應(yīng)用部署到外置Tomcat的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
IDEA SSM整合Redis項(xiàng)目實(shí)例 附源碼
今天給大家普及IDEA SSM整合Redis項(xiàng)目實(shí)例,包括pom.xml 配置和spring-redis.xml 配置代碼,代碼也很簡(jiǎn)單,通過(guò)項(xiàng)目實(shí)際案例能更好的幫助大家理解,需要的朋友可以參考下2021-06-06
MyBatis分頁(yè)插件PageHelper的具體使用
這篇文章主要介紹了MyBatis分頁(yè)插件PageHelper的具體使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(3)
這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)第三篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

