How to Use @JsonView Annotation for nested Object ( DTOs)

26 views Asked by At

I want to use @JsonView In Nested DTOs.

My UserDTO looks like this

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserDTO {
    @JsonView(value = {Views.UserProtected.class, Views.UserPrivate.class})
    Long userId;
    @JsonView(value = {Views.UserProtected.class})
    String firstname;
    @JsonView(value = {Views.UserProtected.class})
    String lastname;
    @JsonView(value = {Views.UserProtected.class})
    String email;
    @JsonView(value = {Views.UserPrivate.class})
    String password;
}

and My appointmentDTO looks like this

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class GetAppointmentResponseDTO {
        private Long appointmentId;
        private String title;
        private String description;

        @JsonView(Views.UserProtected.class)
        private UserDTO scheduledBy;

        @JsonView(Views.UserProtected.class)
        private UserDTO scheduledWith;

        private List<AppointmentInstance> appointmentInstances;
        private AppointmentType type;
        private String location;
}

when i am fetching appointment data UserDTO inside AppointmentDTO also has password field inside it, i want to include protected fields mention in views inside it. how should i approach it.

0

There are 0 answers