Jackson how to masking a property with an annotation in

Viewed 22

there are 2 model like these.

public class UserDto implements Serializable {
    private final String no;

    public UserDto(String no) {
        this.no = no;
    }

    public String getNo() {
        return no;
    }
}
public class UserListDto implements Serializable {

    @Masking(prop = "no", mask = "*")
    private final UserDto user;

    private final Long ser;

    public UserListDto(UserDto user, Long ser) {
        this.user = user;
        this.ser = ser;
    }

    public UserDto getUser() {
        return user;
    }

    public Long getSer() {
        return ser;
    }
}

and an annotation like this

@Retention(RetentionPolicy.RUNTIME)
@Documented
@JacksonAnnotationsInside
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Masking {
    // the prop will to be mask
    String prop();
    // the masking char
    String mask() default "*";
}

how to write 'no' prop with masking char '*' in UserListDto, but not masking in UserDto?

public class TestJackson {

    @Test
    void test1() throws JsonProcessingException {
        ObjectMapper om = new ObjectMapper();
        UserDto user = new UserDto("ba la la");
        UserListDto uld = new UserListDto(user, 11L);
        // the no prop can not be masking
        System.out.println(om.writeValueAsString(user));
        // the no prop in user prop mast be masking
        System.out.println(om.writeValueAsString(uld));
    }
}

how to do this? It looks like your post is mostly code; please add some more details.

0 Answers
Related