几种序列化与get、set方法的关系
若get开头且第四个字母是大写的方法中有空指针异常时(无论有没有对应属性)
1.阿里巴巴的FastJson会出现空指针异常,证明与get开头的方法有关
2.Google的Gson不会出现异常,因为只和属性有关,和get开头的方法没关系
3.java开源的Jackson也会出现异常,证明与get开头的方法有关
但是set开头的方法有异常时,三种序列化都不会影响
下面是三种的maven依赖:
1 <dependencies> 2 <dependency> 3 <groupId>com.alibaba</groupId> 4 <artifactId>fastjson</artifactId> 5 <version>1.2.28</version> 6 </dependency> 7 <dependency> 8 <groupId>com.google.code.gson</groupId> 9 <artifactId>gson</artifactId> 10 <version>2.2.4</version> 11 </dependency> 12 <dependency> 13 <groupId>com.fasterxml.jackson.core</groupId> 14 <artifactId>jackson-databind</artifactId> 15 <version>2.3.1</version> 16 </dependency>
测试代码:
1 import com.alibaba.fastjson.JSON; 2 import com.alibaba.fastjson.JSONObject; 3 import com.fasterxml.jackson.core.JsonProcessingException; 4 import com.fasterxml.jackson.databind.ObjectMapper; 5 import com.google.gson.Gson; 6 7 /** 8 * Created by huyanxia on 2017/11/16. 9 */ 10 public class TestFastJson { 11 public static void main(String[] args) { 12 test(); 13 } 14 public static void test() { 15 Student student = new Student(); 16 student.setName("huyanxia"); 17 //1.阿里巴巴的FastJson会出现空指针异常 18 System.out.println(JSON.toJSON(student)); 19 //同样会出现空指针异常 20 System.out.println(JSONObject.parseObject(JSON.toJSONString(student),student.getClass())); 21 //2.Google的Gson不会出现异常,因为只和属性有关,和get开头的方法没关系 22 Gson gson = new Gson(); 23 gson.toJson(student); 24 //3.java开源的Jackson也会出现异常,证明与get开头的方法有关 25 ObjectMapper objectMapper = new ObjectMapper(); 26 try { 27 objectMapper.writeValueAsString(student); 28 }catch(JsonProcessingException e){ 29 e.printStackTrace(); 30 } 31 } 32 } 33 class Student{ 34 private String name; 35 private Integer age; 36 private String gender; 37 private Teacher teacher; 38 39 public String getName() { 40 return name; 41 } 42 43 public void setName(String name) { 44 this.name = name; 45 } 46 47 public Integer getAge() { 48 return null; 49 } 50 51 public void setAge(Integer age) { 52 this.age = age; 53 } 54 55 public String getGender() { 56 return null; 57 } 58 //没有Num属性,但是getNum方法中有异常,所有get开头的方法序列化时会被调用 59 public String getNum(){ 60 return teacher.getName(); 61 } 62 63 //Num()方法中有异常,但是序列化正常 64 public String Num() { 65 return teacher.getName(); 66 } 67 //setNum方法没有关系,即使其中有异常,序列化也没有问题 68 public void setNum(){ 69 teacher.getName(); 70 } 71 72 public void setGender(String gender) { 73 this.gender = gender; 74 } 75 } 76 class Teacher{ 77 private String name; 78 79 public String getName() { 80 return name; 81 } 82 83 public void setName(String name) { 84 this.name = name; 85 } 86 }
JSON.toJSONString方法字符串输出的顺序与对象不一致,可以通过注解解决
1 第一种:通过在实体类添加@JSONType(orders={"name","age","gender","height","weight"}) 2 @JSONType(orders={"name","age","gender","height","weight"}) 3 public class Person { 4 private String name; 5 private Integer age; 6 private String gender; 7 private Double height; 8 private Double weight; 9 } 10 第二种:在属性上添加@JSONField(ordinal = 1) 11 public class Person { 12 @JSONField(ordinal = 1) 13 private String name; 14 @JSONField(ordinal = 2) 15 private Integer age; 16 @JSONField(ordinal = 3) 17 private String gender; 18 @JSONField(ordinal = 4) 19 private Double height; 20 @JSONField(ordinal = 5) 21 private Double weight; 22 }
JSON.toJSONString方法序列化时,需要排除一些字段,或者使字段名称缩短,可以使用注解
//不会序列化 @JSONField(serialize = false) private ActivitySKUStatusEnum statusEnum; //字段名称缩短 @JSONField(name = "type") private PromotionEnum promotionEnum;
注意:
org.springframework.beans.BeanInstantiationException异常解决
在引入阿里巴巴的FastJson的maven依赖时,若项目已经引入spring包,那么需要排除阿里巴巴的spring,如下:
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.3.2</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency>
友情提醒一下:
由于fastjson低版本存在反序列化漏洞,建议大家用较新版本,至少在1.2.28版本以上吧