파란하늘의 지식창고
반응형

기존에 다음과 같이 사용한 domain이 있다.

@Data
public class User {

	private long idx;
	private String userId;
	private String userName;

	@JsonIgnore
	private String password;

	@JsonIgnore
	private ZonedDateTime createdDate;

	@JsonIgnore
	private boolean accountNonExpired;
	
	@JsonIgnore
	private boolean accountNonLocked;
	
	@JsonIgnore
	private boolean credentialsNonExpired;
	
	@JsonIgnore
	private boolean enabled;

	private List<UserAuthority> userAuthorityList;
	
	@JsonIgnore
	private UserType userType;
	
	@JsonIgnore
	private String externalId;

}

위 domain을 사용한 object가 다음과 같은 값을 가지고 있다면

User[idx=2, userId=850207a4-e587-479c-830e-3f3542c2b154, userName=testUserName, password=testPassword, createdDate=2022-07-10T22:18:30.675887Z[UTC], accountNonExpired=true, accountNonLocked=true, credentialsNonExpired=true, enabled=true, userAuthorityList=[], userType=LOCAL, externalId=null]

해당 object의 json 요청은  다음과 같이 @JsonIgnore로 지정된 필드는 제거된 채 반환된다.

{
    "idx": 2,
    "userId": "850207a4-e587-479c-830e-3f3542c2b154",
    "userName": "testUserName",
    "userAuthorityList": []
}

jdk 14에 preview 기능으로 소개되고 jdk 16에 정식 기능으로 추가된 record를 사용해 보면 위 domain class는 다음과 같이 사용할 수 있다.

public record User(long idx, String userId, String userName, String password, ZonedDateTime createdDate, boolean accountNonExpired, boolean accountNonLocked, boolean credentialsNonExpired, boolean enabled, List<UserAuthority> userAuthorityList,
		UserType userType, String externalId) {
}

lombok을 사용할 필요 없이 간결해졌다.

만약 @JsonIgnore를 다음과 같이 이 record에 사용하면

public record User(long idx, String userId, String userName, String password, @JsonIgnore ZonedDateTime createdDate, @JsonIgnore boolean accountNonExpired, @JsonIgnore boolean accountNonLocked, @JsonIgnore boolean credentialsNonExpired, @JsonIgnore boolean enabled, List<UserAuthority> userAuthorityList,
		@JsonIgnore UserType userType, @JsonIgnore String externalId) {
}

json response 응답은 동일하게 보이지만 만약 저 record를 restTemplate이나 webclient를 사용하여 다른 곳에서 가져와 java에서 사용할 때 해당 값들 자체가 설정이 되지 않은 것을 볼 수 있다.

User[idx=2, userId=850207a4-e587-479c-830e-3f3542c2b154, userName=testUserName, password=null, createdDate=null, accountNonExpired=false, accountNonLocked=false, credentialsNonExpired=false, enabled=false, userAuthorityList=[], userType=null, externalId=null]

이유는 record는 필드 선언과 생성자 역할을 동시에 수행하는데 위에서 @JsonIgnore를 선언한 구간은 생성자의 역할을 하기 때문에 setter의 동작을 수행하며 jsonIgnore이 처리되기 때문이다.

즉, 기존의 domain class에서 jsonIgnore를 gettter나 setter가 아닌 필드에 선언하면 getter에서 처리로 판단되는 반면 record는 setter에서 처리로 판단한다.

record에서 응답 값에 대해 @JsonIgnore를 하고자 하는 경우 다음과 같이 @JsonIgnore를 사용하지 않고 record에 @JsonIgnoreProperties의 allowSetters 옵션을 true를 설정하고 필드를 지정하여 사용해야 한다.

@JsonIgnoreProperties(allowSetters = true, value = {"password", "createdDate", "accountNonExpired", "accountNonLocked", "credentialsNonExpired", "enabled", "userType", "externalId"})
public record User(long idx, String userId, String userName, String password, ZonedDateTime createdDate, boolean accountNonExpired, boolean accountNonLocked, boolean credentialsNonExpired, boolean enabled, List<UserAuthority> userAuthorityList,
		UserType userType, String externalId) {
}

 

반응형
profile

파란하늘의 지식창고

@Bluesky_

내용이 유익했다면 광고 배너를 클릭 해주세요