-->

스프링

 

 

1. 개별 사용자 조회

User  객쳉   @JsonFilter("UserInfo")  어노테이션 추가

User

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.validation.constraints.Past;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Date;

@Data
@AllArgsConstructor
//@JsonIgnoreProperties(value = {"password" })
@JsonFilter("UserInfo")
public class User {
    private Integer id;

    @Size(min=2, message = "Name 은 2글자 이상 입력해 주세요.")
    private String name;

    //과거데이터만 올수 있는 제약 조건
    @Past
    private Date joinDate;

//  @JsonIgnore
    private String password;

  //  @JsonIgnore
    private String ssn;





}

 

 

AdminUserController

    @GetMapping("/users/{id}")
    public MappingJacksonValue retrieveUser(@PathVariable int id){
        User user =service.findOne(id);
        if(user==null){
            throw new UserNotFoundException(String.format("ID[%s] not found", id));
        }

        SimpleBeanPropertyFilter filter =SimpleBeanPropertyFilter.filterOutAllExcept("id","name", "joinDate", "ssn");
        FilterProvider filters =new SimpleFilterProvider().addFilter("UserInfo", filter);

        MappingJacksonValue mapping =new MappingJacksonValue(user);
        mapping.setFilters(filters);
        return mapping;
    }



 

 

 

2. 전제 사용자 조회

 

    @GetMapping("/users")
    public MappingJacksonValue retrieveAllUsers() {
        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;
    }

 

 

 

spring

 

about author

PHRASE

Level 1  라이트

댓글 ( 4)

댓글 남기기

작성