Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure a custom RelProvider for Spring HATEOAS when using Spring Boot?

I'm using the party model:

@Entity
@Inheritance(strategy=...)
@JsonTypeInfo(use= JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@DiscriminatorColumn(name = "type")
public abstract class Party {

  @Column(updatable = false, insertable = false)
  private String type;

  ...
}

@Entity
public class Individual extends Party {
  ...
}

@Entity class Organization extends Party {
  ...
}

Spring Data REST responds like this:

{
  "_embedded": {
    "organizations": [
      {
        "type":"Organization",
        "name": "Foo Enterprises",
        "_links": {
          "self": {
            "href": "http://localhost/organization/2"
          },
          "organization": {
            "href": "http://localhost/organization/2"
          }
        }
      }
    ],
    "individuals": [
      {
        "type":"Individual",
        "name": "Neil M",
        "_links": {
          "self": {
            "href": "http://localhost/individual/1"
          },
          "individual": {
            "href": "http://localhost/individual/1"
          }
        }
      }
    ]
  }
}

But I need it to respond like this:

{
  "_embedded": {
    "parties": [
      {
        "type": "Organization",
        "name": "Foo Enterprises",
        "_links": {
          "self": {
            "href": "http://localhost/party/2"
          },
          "organization": {
            "href": "http://localhost/party/2"
          }
        }
      },
      {
        "type": "Individual",
        "name": "Neil M",
        "_links": {
          "self": {
            "href": "http://localhost/party/1"
          },
          "individual": {
            "href": "http://localhost/party/1"
          }
        }
      }
    ]
  }
}

To do so, I understand I need to provide a custom RelProvider:

@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public class MyRelProvider implements RelProvider {

    public MyRelProvider() {}

    @Override
    public String getItemResourceRelFor(Class<?> aClass) {
        return "party";

    }

    @Override
    public String getCollectionResourceRelFor(Class<?> aClass) {
        return "parties";
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return aClass.isAssignableFrom(Party.class);
    }
}

I tried configuring it in Application.java:

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    RelProvider myRelProvider() {
        return new MyRelProvider();
    }
}

This doesn't work though. It seems to not get registered, or get registered correctly. See http://andreitsibets.blogspot.ca/2014/04/hal-configuration-with-spring-hateoas.html

How can I fix this?

like image 304
Neil McGuigan Avatar asked Nov 08 '22 21:11

Neil McGuigan


1 Answers

I have the same issue and have a dirty solution in case you are interested. Implement private repositories for the subclasses like

@RepositoryRestResource(path = "parties")    
interface IndividualRepository extends PagingAndSortingRepository<Individual, Long>{
}

Hope that will give you some temporary relief.

like image 149
FFL Avatar answered Nov 15 '22 05:11

FFL