Spring Boot实现反序列化的3种方式 - fasterxml
在Spring Boot中,反序列化通常使用Jackson库来实现。以下是三种常见的反序列化方式:
使用@JsonSerialize和@JsonDeserialize注解
import com.fasterxml.jackson.annotation.JsonSerialize;
import com.fasterxml.jackson.annotation.JsonDeserialize;
public class Person {
private String name;
private int age;
// getter and setter methods
}
@JsonSerialize(using = PersonSerializer.class)
@JsonDeserialize(using = PersonDeserializer.class)
public class PersonDTO {
private String name;
private int age;
// getter and setter methods
} 拼多多帮助力入口
使用@JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
public class Person {
private String name;
private int age;
// getter and setter methods
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class PersonDTO {
private String name;
private int age;
// getter and setter methods
}
使用@JsonProperty和@JsonValue注解
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
public class Person {
private String name;
private int age;
@JsonProperty("name")
@JsonValue("张三")
private String name;
@JsonProperty("age")
@JsonValue("25")
private int age;
// getter and setter methods
}
public class PersonDTO {
@JsonProperty("name")
@JsonValue("张三")
private String name;
@JsonProperty("age")
@JsonValue("25")
private int age;
// getter and setter methods
}