conanan's blog conanan's blog
首页
关于
  • 分类
  • 标签
  • 归档
  • Java
  • Java Web
  • 工具

    • Maven
  • MySQL
  • Redis
  • Git
  • Vim
  • Nginx
  • Docker
GitHub

Evan Xu

前端界的小学生
首页
关于
  • 分类
  • 标签
  • 归档
  • Java
  • Java Web
  • 工具

    • Maven
  • MySQL
  • Redis
  • Git
  • Vim
  • Nginx
  • Docker
GitHub
  • Jakarta EE

  • Tomcat

  • Spring

  • SpringMVC

    • 概述
    • 路由&请求参数绑定
    • 响应结果
    • 异常处理&统一响应
    • Interceptor
    • CORS
    • 自动URL前缀
    • Jackson
      • 通用注解
        • @JsonProperty 🔥
      • 序列化注解
        • @JsonAnyGetter
        • @JsonGetter 🔥
        • @JsonPropertyOrder
        • @JsonRawValue
        • @JsonValue 🔥
      • 反序列化注解
        • @JsonSetter 🔥
      • 属性包含注解
        • @JsonIgnoreProperties 🔥
  • SpringBoot

  • SpringDataJPA

  • Test
  • Shiro
  • Thymeleaf
  • Java Web
  • SpringMVC
conanan
2021-06-21

Jackson

# Jackson

参考这篇博客

# 通用注解

序列化和反序列化都生效

# @JsonProperty 🔥

添加@JsonProperty注释来表示JSON中的属性名。

public class MyBean {
    public int id;
    private String name;
 
    @JsonProperty("name1")
    public void setTheName(String name) {
        this.name = name;
    }
 
    @JsonProperty("name1")
    public String getTheName() {
        return name;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{"id":1,"name1":"My bean"}
1

# 序列化注解

将 Java 对象转为与平台无关的二进制字节流

# @JsonAnyGetter

@JsonAnyGetter注释允许灵活地使用Map类型字段作为标准属性。

@Data
public class ExtendableBean {
    private String name;

    private Map<String, String> map = new HashMap<>(10);

    private Map<String, String> properties = new HashMap<>(10);

    @JsonAnyGetter
    public Map<String, String> getProperties() {
        return properties;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

当我们序列化这个实体的一个实例时,我们将得到映射中的所有键值作为标准的普通属性:

{
  "name": "Tom",
  "map": {},
  "attr2": "val2",
  "attr1": "val1",
  "attr3": "val3"
}
1
2
3
4
5
6
7

示例

@Test
public void whenSerializingUsingJsonAnyGetter_thenCorrect() throws JsonProcessingException {
    ExtendableBean bean = new ExtendableBean();
    bean.setName("Tom");
    bean.getProperties().put("attr1", "val1");
    bean.getProperties().put("attr3", "val3");
    bean.getProperties().put("attr2", "val2");

    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);
}
1
2
3
4
5
6
7
8
9
10
11

# @JsonGetter 🔥

@JsonGetter注释是@JsonProperty注释的替代方法,用于将方法标记为getter方法,替代原样getter。

public class MyBean {
    public int id;
    private String name;
 
    @JsonGetter("name1")
    public String getTheName() {
        return name;
    }
}
1
2
3
4
5
6
7
8
9
{"id":1,"name1":"My bean"}
1

示例

@Test
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {

    MyBean bean = new MyBean(1, "My bean");

    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);

}
1
2
3
4
5
6
7
8
9

# @JsonPropertyOrder

我们可以使用@JsonPropertyOrder注释来指定序列化中属性的顺序。

我们还可以使用@JsonPropertyOrder(alphabetic=true)按字母顺序排列属性。

@JsonPropertyOrder({ "name", "id" })
public class MyBean {
    public int id;
    public String name;
}
1
2
3
4
5
{
    "name":"My bean",
    "id":1
}
1
2
3
4

# @JsonRawValue

@JsonRawValue注释可以指示Jackson按原样序列化属性。 可以设置true或false来定义此注释是否处于活动状态(一般不用)

public class RawBean {
    public String name;
 
    @JsonRawValue
    public String json;
}
1
2
3
4
5
6
{
    "name":"My bean",
    "json":{
        "attr":false
    }
}
1
2
3
4
5
6

示例

@Test
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {

    MyBean bean = new MyBean(1, "My bean");
    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);
}
1
2
3
4
5
6
7

# @JsonValue 🔥

@JsonValue表示该库将用于序列化整个实例的单个方法。

例如,在enum中,我们使用@JsonValue注释getName,以便通过它的名称序列化任何这样的实体

public enum TypeEnumWithValue {
    TYPE1(1, "Type A"), TYPE2(2, "Type 2");
 
    private Integer id;
    private String name;
 
    // standard constructors
 
    @JsonValue
    public String getName() {
        return name;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
"Type A"
1

示例

@Test
public void whenSerializingUsingJsonValue_thenCorrect() throws IOException {

    String enumAsString = new ObjectMapper().writeValueAsString(TypeEnumWithValue.TYPE1);
    System.out.println(enumAsString);
}
1
2
3
4
5
6

# 反序列化注解

# @JsonSetter 🔥

@JsonSetter 注释是@JsonProperty注释的替代方法,用于将方法标记为setter方法,替代原样setter。

{"id":1,"name1":"My bean"}
1
public class MyBean {
    public int id;
    private String name;
 
    @JsonSetter("name1")
    public String getTheName() {
        return name;
    }
}
1
2
3
4
5
6
7
8
9

示例

@Test
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {

    MyBean bean = new MyBean(1, "My bean");

    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);

}
1
2
3
4
5
6
7
8
9

# 属性包含注解

# @JsonIgnoreProperties 🔥

标记Jackson将忽略的属性或属性列表。可用在类、方法、构造器、字段上

设置@JsonIgnoreProperties注释的ignoreUnknown=true。 定义可以在反序列化期间仅忽略任何无法识别的属性的属性。如果为true,则所有无法识别的属性(即没有setter或builder接受它们)都将在没有警告的情况下被忽略(尽管仍会调用未知属性的处理程序)。对序列化没有任何影响。即常用于RequesBody

@JsonIgnoreProperties({ "id" })
public class BeanWithIgnore {
    public int id;
    public String name;
}
1
2
3
4
5
{"name":"My bean"}
1

示例

@Test
public void whenSerializingUsingJsonIgnoreProperties_thenCorrect()
    throws JsonProcessingException {

    BeanWithIgnore bean = new BeanWithIgnore(1, "My bean");

    String result = new ObjectMapper().writeValueAsString(bean);
    System.out.println(result);
}
1
2
3
4
5
6
7
8
9
编辑
上次更新: 2021/06/21, 15:45:42
自动URL前缀
SSM

← 自动URL前缀 SSM→

最近更新
01
线程生命周期
07-06
02
线程安全理论
06-24
03
并发简史
06-24
更多文章>
Theme by Vdoing | Copyright © 2019-2021 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×