大家都知道protobuf好用,可是在網上找到的netty整合protobuf的文章都是千篇一律,自己編寫proto文件然后使用工具轉java文件用起來復雜麻煩,經過不懈努力終于找到了一個簡單的方法希望大家喜歡。
google原生的protobuffer使用起來相當麻煩,首先要寫.proto文件,然后編譯.proto文件,生成對應的.java文件,鄙人試了一次,發現真的很麻煩。而protostuff的官方網站(http://www.protostuff.io/documentation/runtime-schema/),對于智商比較低的小編來說也略顯生澀,于是鄙人就根據項目中用到的protostuff,撰寫此文,以方便自己和他人加深印象和學習。
1.實戰
1.maven依賴:
1
2
3
4
5
6
7
8
9
10
|
<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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
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; // 關于@tag,要么所有屬性都有@tag注解,要么都沒有,不能一個類中只有部分屬性有@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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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.輸出結果:
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'}
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/zhglance/article/details/56017926