Home > OS >  What @Json annotation to use for forgein key
What @Json annotation to use for forgein key

Time:08-04

I need an annotation that will only return certain Object properties or only forgein key
User:

@Id
    @GeneratedValue(strategy = AUTO)
    private Long id;
    ...
    @JsonIgnore
    @OneToMany(mappedBy="user")
    private Set<Log> logs=new HashSet<>();

Logs:

@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long logsId;
    ...
    @JsonProperty<--------
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private User user;
    ...

I am using @JsonProperty but it returns a whole object with vulnerable data

CodePudding user response:

You can use @jsonignore annotation for the properties that you don't want to be retrieved. If you have too much data to be annotated, I would suggest you to move those attributes to a single class and have a property on that while it's being annotated with @jsonignore. Mainly this annotation is used to remove cyclic data returned with foreign keys.

  • Related