Home > front end >  jackson anotations with lombok @Getter dont work
jackson anotations with lombok @Getter dont work

Time:10-09

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?

CodePudding user response:

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
}

Usage example:

String user = mapper.writerWithDefaultPrettyPrinter()
    .writeValueAsString(
        User.builder()
            .userUid(new UUID(1, 1))
            .firstName("firstName")
            .lastName("lastName")
            .gender(User.Gender.MALE)
            .age(1000)
            .email("[email protected]")
            .build()
    );
    
System.out.println(user);

Output:

{
  "userUid" : "00000000-0000-0001-0000-000000000001",
  "gender" : "MALE",
  "age" : 1000,
  "email" : "[email protected]",
  "fullName" : "firstName lastName",
  "dateOfBirth" : 1022
}

Note: there are no attributes firstName and lastName, but instead fullName is present because of the public method getFullName() which would be treated by Jackson as a plain getter (I'm sure that was precisely the original intention of the OP, but it's worth to draw the reader's attention to this fact)

  • Related