@JsonAutoDetect 주석 가시성 을 재정의하기 위해 클래스 수준에서 사용할 수 있습니다. 직렬화 중 클래스 속성 및 역직렬화 . "creatorVisibility와 같은 속성으로 가시성을 설정할 수 있습니다. ", "fieldVisibility ", "getter가시성 ", "setter가시성 " 및 "isGetterVisibility ". JsonAutoDetect 클래스는 공개 정적 상수를 정의할 수 있습니다. 자바 클래스와 유사 "ANY", "DEFAULT", "NON_PRIVATE", "NONE", "PROTECTED_AND_PRIVATE"와 같은 가시성 수준 및 "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);
}
}
// Address class
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;
}
}
// Name class
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;
}
}
// Student class
@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
}