1.
com.fasterxml.jackson.databind.ObjectMapper可用于序列化反序列化对象。将一个json字符串转化为对象,将一个对象转化为json字符串。
将字符串转化为对象:
String data = "{\"name\":\"wzz\",\"age\":1}";
Student student = new ObjectMapper().readValue(data, Student.class);
System.out.println(student);
将对象转化为字符串:
Student student = new Student("wzz", 1);
System.out.println(new ObjectMapper().writeValueAsString(student));
2.注解
(1)@JsonIgnore 作用于属性上面,在序列化反序列化过程中忽略该属性。
(2)@JsonIgnoreProperties 作用于类上面,标注忽略哪些属性,可以配置多个值。
(3)@JsonInclude 在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonInclude 注解可以控制在哪些情况下才将被注解的属性转换成 json,例如只有属性不为 null 时。
(4)lombok.@NonNull 作用于属性,新建对象不允许属性为null。
(5)
javax.annotation.Nonnull注解使用,使用该注解时,需要引入maven依赖,注解可以作用于属性和方法之上,只有标识作用。
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>