1

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

4EACH
  • 2,132
  • 4
  • 20
  • 28
Mesut Can
  • 291
  • 2
  • 5
  • 19
  • 1
    Good reference: https://stackoverflow.com/questions/51172496/how-to-dynamically-ignore-a-property-on-jackson-serialization – 4EACH Oct 07 '21 at 11:56

1 Answers1

2

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).

João Dias
  • 16,277
  • 6
  • 33
  • 45
  • Thank you @João Dias it helped a lot – Mesut Can Oct 08 '21 at 06:45
  • @MesutCan, in that case, consider accepting the answer as correct so that others can benefit from it by easily finding a possible solution to similar topics and the question is "closed". Thanks! – João Dias Oct 08 '21 at 08:39