⼏种序列化与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&le.code.gson</groupId>
9 <artifactId>gson</artifactId>
10 <version>2.2.4</version>
11 </dependency>
12 <dependency>
13 <groupId>com.</groupId>
14 <artifactId>jackson-databind</artifactId>
15 <version>2.3.1</version>
16 </dependency>
测试代码:
1import com.alibaba.fastjson.JSON;
2import com.alibaba.fastjson.JSONObject;
3import com.JsonProcessingException;
4import com.fasterxml.jackson.databind.ObjectMapper;
le.gson.Gson;
6
7/**
8 * Created by huyanxia on 2017/11/16.
9*/
10public class TestFastJson {
11public static void main(String[] args) {
12 test();
13 }
14public static void test() {
15 Student student = new Student();
16 student.setName("huyanxia");
17//1.阿⾥巴巴的FastJson会出现空指针异常
18 System.out.JSON(student));
19//同样会出现空指针异常
20 System.out.println(JSONObject.JSONString(student),Class()));
21//2.Google的Gson不会出现异常,因为只和属性有关,和get开头的⽅法没关系
22 Gson gson = new Gson();
23 Json(student);
24//3.java开源的Jackson也会出现异常,证明与get开头的⽅法有关
25 ObjectMapper objectMapper = new ObjectMapper();
26try {
27 objectMapper.writeValueAsString(student);
28 }catch(JsonProcessingException e){
29 e.printStackTrace();
30 }
31 }
32 }
33class Student{
34private String name;
35private Integer age;
36private String gender;
37private Teacher teacher;
38
39public String getName() {
40return name;
41 }
42
43public void setName(String name) {
44this.name = name;
45 }
46
47public Integer getAge() {
48return null;
49 }
50
51public void setAge(Integer age) {
52this.age = age;
53 }
54
55public String getGender() {
56return null;
57 }
58//没有Num属性,但是getNum⽅法中有异常,所有get开头的⽅法序列化时会被调⽤
59public String getNum(){
Name();
61 }
62
63//Num()⽅法中有异常,但是序列化正常
64public String Num() {
Name();
66 }
67//setNum⽅法没有关系,即使其中有异常,序列化也没有问题
68public void setNum(){
69 Name();
70 }
71
72public void setGender(String gender) {
74 }
75 }
76class Teacher{
77private String name;
78
79public String getName() {
80return name;
81 }
82
fastjson常用方法83public void setName(String name) {
84this.name = name;
85 }
86 }
1第⼀种:通过在实体类添加@JSONType(orders={"name","age","gender","height","weight"})
2 @JSONType(orders={"name","age","gender","height","weight"})
3public class Person {
4private String name;
5private Integer age;
6private String gender;
7private Double height;
8private Double weight;
9 }
10第⼆种:在属性上添加@JSONField(ordinal = 1)
11public class Person {
12 @JSONField(ordinal = 1)
13private String name;
14 @JSONField(ordinal = 2)
15private Integer age;
16 @JSONField(ordinal = 3)
17private String gender;
18 @JSONField(ordinal = 4)
19private Double height;
20 @JSONField(ordinal = 5)
21private Double weight;
22 }
//不会序列化
@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版本以上吧
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论