ChrisGeo
ChrisGeo

Reputation: 3907

Spring Data REST - Exclude Subtypes

Lets say I have the following Hibernate Entities (fields ommitted)

@Entity
@DiscriminatorColumn(name = "T")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class SuperClass {
}

@Entity
@DiscriminatorValue(name = "subClassA")
public SubClassA extends SuperClass {
}

@Entity
@DiscriminatorValue(name = "subClassB")
public SubClassB extends SuperClass {
}

With Spring Data REST I would get the following JSON representation:

{
  "_links": {
  },
  "_embedded": {
    "subclassA": [
      {
        "field1": "",
        "field2": ""
      }
    ],
    "subclassB": [
      {
        "field1": "",
        "field2": "",
        "field3": ""
      }
    ]
  }
}

Again ommiting the _links attributes. Is there some sort of configuration I can use so the Serializer can ignore the subclasses and do a representation like this:

{
  "_links": {
  },
  "_embedded": {
    "superClass": [
      {
        "field1": "",
        "field2": ""
      },
      {
        "field1": "",
        "field2": "",
        "field3": ""
      }
    ]
  }
}

Upvotes: 2

Views: 296

Answers (1)

Francisco Spaeth
Francisco Spaeth

Reputation: 23893

One way to solve the problem would be the implementation of a RelProvider. All you need to do is to implement it and add it to spring container (this could be done but i.e. annotating the implementation with @Component).

Considering that you would be able to get the response you are expecting simply by adding the following implementation (considering it will be scanned by spring):

@Component
public class MessageRelProvider implements RelProvider {

    public boolean supports(Class<?> arg0) {
        return SuperClass.class.isAssignableFrom(arg0);
    }

    public String getItemResourceRelFor(Class<?> type) {
        return "superClass";
    }

    public String getCollectionResourceRelFor(Class<?> type) {
        return "superClasses";
    }

}

Upvotes: 1

Related Questions