java整合ProtoBuf简便方法

    大家都知道protobuf好用,可是在网上找到的netty整合protobuf的文章都是千篇一律,自己编写proto文件然后使用工具转java文件用起来复杂麻烦,经过不懈努力终于找到了一个简单的方法希望大家喜欢。

    转载自:http://blog.csdn.net/zhglance/article/details/56017926

    google原生的protobuffer使用起来相当麻烦,首先要写.proto文件,然后编译.proto文件,生成对应的.java文件,鄙人试了一次,发现真的很麻烦。而protostuff的官方网站(http://www.protostuff.io/documentation/runtime-schema/),对于智商比较低的小编来说也略显生涩,于是鄙人就根据项目中用到的protostuff,撰写此文,以方便自己和他人加深印象和学习。


1.实战

1.maven依赖:

[java]  view plain  copy
  1. <dependency>  
  2.     <groupId>io.protostuff</groupId>  
  3.     <artifactId>protostuff-core</artifactId>  
  4.     <version>1.4.0</version>  
  5. </dependency>  
  6.   
  7. <dependency>  
  8.     <groupId>io.protostuff</groupId>  
  9.     <artifactId>protostuff-runtime</artifactId>  
  10.     <version>1.4.0</version>  
  11. </dependency>  


2.ProtoBufUtil工具类:ProtoBufUtil.java
[java]  view plain  copy
  1. import io.protostuff.LinkedBuffer;  
  2. import io.protostuff.ProtobufIOUtil;  
  3. import io.protostuff.ProtostuffIOUtil;  
  4. import io.protostuff.Schema;  
  5. import io.protostuff.runtime.RuntimeSchema;  
  6.   
  7. /** 
  8.  * Created by zhangzh on 2017/2/20. 
  9.  */  
  10. public class ProtoBufUtil {  
  11.     public ProtoBufUtil() {  
  12.     }  
  13.   
  14.     public static <T> byte[] serializer(T o) {  
  15.         Schema schema = RuntimeSchema.getSchema(o.getClass());  
  16.         return ProtobufIOUtil.toByteArray(o, schema, LinkedBuffer.allocate(256));  
  17.     }  
  18.   
  19.     public static <T> T deserializer(byte[] bytes, Class<T> clazz) {  
  20.   
  21.         T obj = null;  
  22.         try {  
  23.             obj = clazz.newInstance();  
  24.             Schema schema = RuntimeSchema.getSchema(obj.getClass());  
  25.             ProtostuffIOUtil.mergeFrom(bytes, obj, schema);  
  26.         } catch (InstantiationException e) {  
  27.             e.printStackTrace();  
  28.         } catch (IllegalAccessException e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.   
  32.         return obj;  
  33.     }  
  34. }  

  3. bean类Student.java:    
[java]  view plain  copy
  1. import io.protostuff.Tag;  
  2.   
  3. /** 
  4.  * Created by zhangzh on 2017/2/20. 
  5.  */  
  6. public class Student {  
  7.   
  8.     @Tag(1)  
  9.     private String name;  
  10.     @Tag(2)  
  11.     private String studentNo;  
  12.     @Tag(3)  
  13.     private int age;  
  14.     @Tag(4)  
  15.     private String schoolName;  
  16.   
  17.    // 关于@Tag,要么所有属性都有@Tag注解,要么都没有,不能一个类中只有部分属性有@Tag注解  
  18.   
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.   
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.   
  27.     public String getStudentNo() {  
  28.         return studentNo;  
  29.     }  
  30.   
  31.     public void setStudentNo(String studentNo) {  
  32.         this.studentNo = studentNo;  
  33.     }  
  34.   
  35.     public int getAge() {  
  36.         return age;  
  37.     }  
  38.   
  39.     public void setAge(int age) {  
  40.         this.age = age;  
  41.     }  
  42.   
  43.     public String getSchoolName() {  
  44.         return schoolName;  
  45.     }  
  46.   
  47.     public void setSchoolName(String schoolName) {  
  48.         this.schoolName = schoolName;  
  49.     }  
  50.   
  51.     @Override  
  52.     public String toString() {  
  53.         return "Student{" +  
  54.                 "name='" + name + '\'' +  
  55.                 ", studentNo='" + studentNo + '\'' +  
  56.                 ", age=" + age +  
  57.                 ", schoolName='" + schoolName + '\'' +  
  58.                 '}';  
  59.     }  
  60. }  

3.test类ProtoBufUtilTest.java:  
[java]  view plain  copy
  1. import java.util.Arrays;  
  2.   
  3. /** 
  4.  * Created by zhangzh on 2017/2/20. 
  5.  */  
  6. public class ProtoBufUtilTest {  
  7.   
  8.     public static void main(String[] args) {  
  9.   
  10.         Student student = new Student();  
  11.         student.setName("lance");  
  12.         student.setAge(28);  
  13.         student.setStudentNo("2011070122");  
  14.         student.setSchoolName("BJUT");  
  15.   
  16.         byte[] serializerResult = ProtoBufUtil.serializer(student);  
  17.   
  18.         System.out.println("serializer result:" + Arrays.toString(serializerResult));  
  19.   
  20.         Student deSerializerResult = ProtoBufUtil.deserializer(serializerResult,Student.class);  
  21.   
  22.         System.out.println("deSerializerResult:" + deSerializerResult.toString());  
  23.     }  
  24.   
  25. }  

4.输出结果:  

[java]  view plain  copy
  1. serializer result:[1051089711099101181050484949485548495050242834466748584]  
  2. deSerializerResult:Student{name='lance', studentNo='2011070122', age=28, schoolName='BJUT'}  

看,简单吧!


 参考资料:1. protostuff官方网站.

                     2. Protostuff序列化.


猜你喜欢

转载自blog.csdn.net/qq_36680439/article/details/79563349