何时在Java中使用@JsonAutoDetect注释?
@JsonAutoDetect注释 可以在类级别被用于覆盖 能见度 期间一个类的属性的序列化 和反序列化。我们可以使用“ creatorVisibility ”,“ fieldVisibility ”,“ getterVisibility ”,“ setterVisibility ”和“ isGetterVisibility ”等属性来设置可见性。该JsonAutoDetect 类可以定义公共静态常量类似于Java类的可见度像“ANY”,“默认”,“NON_PRIVATE”,“NONE”,PUBLIC_ONLY “。
示例
import com.fasterxml.jackson.annotation.*;import com.fasterxml.jackson.databind.*;
import java.io.*;
public class JsonAutoDetectTest {
public static void main(String[] args) throws IOException {
Address address = new Address("Madhapur", "Hyderabad", "Telangana");
Name name = new Name("Raja", "Ramesh");
Student student = new Student(address, name, true);
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(student);
System.out.println("JSON: " + jsonString);
}
}
//地址类别
class Address {
private String firstLine;
private String secondLine;
private String thirdLine;
public Address(String firstLine, String secondLine, String thirdLine) {
this.firstLine = firstLine;
this.secondLine = secondLine;
this.thirdLine = thirdLine;
}
public String getFirstLine() {
return firstLine;
}
public String getSecondLine() {
return secondLine;
}
public String getThirdLine() {
return thirdLine;
}
}
//名称类
class Name {
private String firstName;
private String secondName;
public Name(String firstName, String secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
public String getFirstName() {
return firstName;
}
public String getSecondName() {
return secondName;
}
}
//学生班
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
class Student {
private Address address;
private Name name;
private Boolean isActive;
public Student(Address address, Name name, Boolean isActive) {
this.address = address;
this.name = name;
this.isActive = isActive;
}
}
输出结果
{"address" : {
"firstLine" : "Madhapur",
"secondLine" : "Hyderabad",
"thirdLine" : "Telangana"
},
"name" : {
"firstName" : "Raja",
"secondName" : "Ramesh"
},
"isActive" : true
}
以上是 何时在Java中使用@JsonAutoDetect注释? 的全部内容, 来源链接: utcz.com/z/345607.html