0

In scenario where I'm exposing following entity in Spring data rest:

public class User implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Long userId;

@NotNull
@Size(min = 1, max = 50)
@Column(length = 50, unique = true, nullable = false)
private String username;

@JsonIgnore
@NotNull
@Size(min = 60, max = 60)
@Column(name = "password",length = 60)
private String password;
....

I would like password to not be exposed in the response when performing GET on ../users/1 but allowing hal browser to know that when performing post request it needs to set password.

When I'm using @JsonIgnore Hal Browser do not know that it needs to set password field:

hal browser post request

How can I do it? Is this even possible?

user1048282
  • 780
  • 1
  • 9
  • 22

2 Answers2

1

Have you tried adding @JsonIgnore in the getter method only instead of the private String password property?

...

@NotNull
@Size(min = 60, max = 60)
@Column(name = "password",length = 60)
private String password;

...

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


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

Edit: I just found this

Community
  • 1
  • 1
dschulz
  • 4,666
  • 1
  • 31
  • 31
0

Just use the property access of @JsonIgnore.

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)

You don't need to place it in the getter.

Hope it helps.

bergacat1
  • 133
  • 9