@ JsonProperty / @ JsonIgnore does not work as expected, only for the record property

As suggested here I tried to use @JsonIgnoreboth @JsonProperty(from import com.fasterxml.jackson.annotation.*;) in the fields passwordand passwordSaltmy AppUser.

@Entity
@Table(name = "app_user")
public class AppUser extends AbstractTimestampEntity {

    // ..

    @JsonIgnore
    public String getPassword() {
        return password;
    }

    @JsonProperty
    public void setPassword(String password) {
        this.password = password;
    }

    @JsonIgnore
    public String getPasswordSalt() {
        return passwordSalt;
    }

    @JsonProperty
    public void setPasswordSalt(String passwordSalt) {
        this.passwordSalt = passwordSalt;
    }

}

However, for some reason, two fields are always null. When I try to “register” a user, the JSON I submit is not completely de-serialized. passwordset to null:

enter image description here

What I want to achieve is to get the user password to save it, but at the same time make sure that the saved (encrypted) password is not serialized and sent back to the client.

What am I doing wrong here?

0
1

Jackson 2.9.0 :

@NoArgsConstructor
@AllArgsConstructor
public class Credentials {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @JsonProperty(access = Access.WRITE_ONLY)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

... ...

@Test
public void jsonIgnoreSerialization() throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();

    String serialized = objectMapper.writeValueAsString(new Credentials("user", "pass"));
    assertThat(serialized).isEqualTo("{\"username\":\"user\"}");

    Credentials credentials = objectMapper.readValue("{\"username\":\"user\",\"password\":\"pass\"}", Credentials.class);
    assertThat(credentials.getUsername()).isEqualTo("user");
    assertThat(credentials.getPassword()).isEqualTo("pass");
}

Lombok, AssertJ.

?

0

All Articles