Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jackson anotations with lombok @Getter dont work

having class, the data goes into the map

@Getter
@ToString
@Builder
//@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
//@JsonIgnoreProperties(value = {"firstName", "lastName"})
public class User {

//    @JsonProperty("id")
    private final UUID userUid;

    @JsonIgnore
    private final String firstName;
    @JsonIgnore
    private final String lastName;
    private final Gender gender;
    private final Integer age;
    private final String email;

    public enum Gender {
        MALE, FEMALE
    }

    public String getFullName() {
        return this.firstName + " " + this.lastName;
    }

    public int getDateOfBirth() {
        return LocalDate.now().minusYears(age).getYear();
    }

    public User(@JsonProperty("userUid") UUID userUid
            , @JsonProperty("firstName") String firstName
            , @JsonProperty("lastName") String lastName
            , @JsonProperty("gender") Gender gender
            , @JsonProperty("age") Integer age
            , @JsonProperty("email") String email) {
        this.userUid = userUid;
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.age = age;
        this.email = email;
    }

    public static User newUser(UUID userUid, User user) {
        return User.builder()
                .userUid(userUid)
                .firstName(user.getFirstName())
                .lastName(user.getLastName())
                .gender(user.getGender())
                .age(user.getAge())
                .email(user.email)
                .build();
    }
}    

@JsonIgnore annotations on the field do not work, the fields are displayed in the response.
the data is hidden when I annotate class by @JsonIgnoreProperties or by adding annotations over the getter.
and @JsonProperty above the field generally throws a 500 error …
It turns out that jackson annotations with lombok @Getter do not work, and it is necessary to annotate getters, or am I doing something wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can assign attribute access of @JsonProperty to JsonProperty.Access.WRITE_ONLY in order to be able to receive this property but exclude it while serializing a POJO.

@Getter
@ToString
@Builder
public static class User {
    
    private final UUID userUid;
    private final String firstName;
    private final String lastName;
    private final Gender gender;
    private final Integer age;
    private final String email;
    
    public User(@JsonProperty("userUid") UUID userUid,
                @JsonProperty(value = "firstName", access = JsonProperty.Access.WRITE_ONLY)
                    String firstName,
                @JsonProperty(value = "lastName", access = JsonProperty.Access.WRITE_ONLY)
                    String lastName,
                @JsonProperty("gender") Gender gender,
                @JsonProperty("age") Integer age,
                @JsonProperty("email") String email) {
        
        this.userUid = userUid;
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.age = age;
        this.email = email;
    }
    
    // the rest code
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading