@JsonAutoDetect 어노테이션은 클래스 레벨에 적용하여 직렬화(Serialization)와 역직렬화(Deserialization) 과정에서 클래스 프로퍼티의 가시성(Visibility)을 재정의할 수 있게 해주는 Jackson 어노테이션입니다.
기본적으로 Jackson은 public getter 메서드를 기준으로 JSON 필드를 생성하지만, getter가 없는 private 필드까지 포함하고 싶거나 반대로 특정 접근 수준의 멤버만 노출하고 싶을 때 이 어노테이션이 유용합니다.
설정 가능한 가시성 속성
@JsonAutoDetect에서는 다음과 같은 속성으로 각 멤버 유형별 가시성을 개별적으로 지정할 수 있습니다.
- creatorVisibility: 생성자 관련 가시성
- fieldVisibility: 필드(field) 가시성
- getterVisibility: 일반 getter 메서드 가시성
- setterVisibility: setter 메서드 가시성
- isGetterVisibility: boolean 타입의 is 접두사 getter 가시성
가시성 상수 종류
JsonAutoDetect 클래스는 Java 클래스의 접근 제한자 수준과 유사한 다음과 같은 public static 상수를 제공합니다.
- ANY: 모든 접근 수준 허용 (private 포함)
- DEFAULT: Jackson의 기본 동작 사용
- NON_PRIVATE: private이 아닌 모든 멤버 허용
- NONE: 어떤 멤버도 자동 감지하지 않음
- PROTECTED_AND_PRIVATE: protected 및 private 멤버만 허용
- PUBLIC_ONLY: public 멤버만 허용
예제 코드
아래 예제에서 Student 클래스에는 getter 메서드가 전혀 없지만, @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)를 적용하여 private 필드까지 모두 JSON으로 직렬화되도록 설정했습니다.
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 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 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 클래스 - getter 없이 필드 가시성을 ANY로 설정
@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;
}
}실행 결과
Student 클래스에 getter 메서드가 없음에도 불구하고, fieldVisibility를 ANY로 설정했기 때문에 모든 private 필드가 정상적으로 JSON에 포함된 것을 확인할 수 있습니다.
{
"address" : {
"firstLine" : "Madhapur",
"secondLine" : "Hyderabad",
"thirdLine" : "Telangana"
},
"name" : {
"firstName" : "Raja",
"secondName" : "Ramesh"
},
"isActive" : true
}정리
@JsonAutoDetect는 DTO나 도메인 객체에 getter를 추가하기 어려운 상황에서 private 필드를 JSON에 노출해야 할 때 특히 유용합니다. 다만 무분별하게 ANY로 설정하면 민감한 정보까지 직렬화될 수 있으므로, 필요한 범위만 선택적으로 지정하는 것이 좋습니다.