Mesut Can
Mesut Can

Reputation: 291

JsonIgnore except one endpoint

I am developing app using spring boot and I just want to ignore a field while returning to the client but except one endpoint.

public class MyJwt {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Lob
@JsonIgnore
private String jwt;
private String reminderKey;
private String userAccountId;
private String clientKey;
private Timestamp createdDate;
private Timestamp expirationDate;
private byte statusCode;

How can I expose jwt to the client only for one endpoint? Thanks

Upvotes: 1

Views: 275

Answers (1)

João Dias
João Dias

Reputation: 17460

Take a look at JSON Views. With it, you can tell which properties to include in each view. You could have 2 views:

public class Views {
    public static class Public {
    }

    public static class Internal extends Public {
    }
}

Then your model should look like:

@JsonView(Views.Public.class)
public class MyJwt {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  @Lob
  @JsonView(Views.Internal.class)
  private String jwt;
  private String reminderKey;
  private String userAccountId;
  private String clientKey;
  private Timestamp createdDate;
  private Timestamp expirationDate;
  private byte statusCode;
}

Then in your endpoints, you would use @JsonView(Views.Public.class) with the exception of the one that should include jwt, on which you should use @JsonView(Views.Internal.class).

Upvotes: 2

Related Questions