0
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger id;
    private String name;
    private String lastName;

    @ManyToOne
    @JoinColumn(name="subscription_id",nullable = false)
    private Subscription subscription;

.

public class Subscription {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger id;
    private String type;
    private double price;
    private Date payDate;

    @OneToMany(mappedBy = "subscription", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JsonIgnore
    private List<User> users = new ArrayList<User>();

I have an user class with a relationship with the subscription class, I've ignored the user list at subscription level.

My problem comes when I'm trying to create a new endpoint to get the Subscription with the users that are part of that subscription,I just want to ignore the user list of the Subscription object in the User controller, is there a way to ignore at controller level? the @JsonIgnore seems to not work at that level. In the Subscription controller I need the Subscription list of users.

Regards.

1 Answers1

0

There are two approaches. First, you can create a DTO (Data Transfer Object), with just the elements that you want to transfer for that particular endpoint. This is a requirement in highly secure coding environments, because there's no chance that somebody later adds a sensitive field and forgets to JSONIgnore it. Since the DTA is specifically for that resource (and is often just a static class within it) then there's no chance it gets something unwanted later.

The other approach is to create a custom Serializer. See How do I use a custom Serializer with Jackson?

Zag
  • 638
  • 4
  • 8