스프링부트 Response Filtering

dragonappear·2022년 1월 31일
0

Spring & SpringBoot

목록 보기
9/11

의존성 추가

// https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.1'

JsonIgnore()

  • JsonIgnore을 필드 위에 붙여주면 데이터를 주고 받을 때 해당 데이터는 Ignore된다
public class User{

private Long id;
    
@JsonIgnore()
private String name;
}
{
	id:1
}

@JsonIgnoreProperties(value)

  • 필드마다 말고, 클래스 위에 달아서 한꺼번에
@Data
@AllArgsConstructor
@JsonIgnoreProperties(value={"password","id"})
public class User {

@JsonFilter(value)

  • 필터를 동적으로 걸 수 있다.
@JsonFilter("UserInfo")
public class User {
	List<User> users = service.findAll();
        SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter
                .filterOutAllExcept("id", "name", "joinDate", "password");

        FilterProvider filters = new SimpleFilterProvider().addFilter("UserInfo", filter);

        MappingJacksonValue mapping = new MappingJacksonValue(users);
        mapping.setFilters(filters);

        return mapping;

0개의 댓글