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

I am using a batch 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 as follows:

 { "_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 him to answer 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" } } } ] } } 

For this, I understand what a custom RelProvider must provide :

 @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 to configure 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 does not work. It seems that it is not registered or not registered correctly. See http://andreitsibets.blogspot.ca/2014/04/hal-configuration-with-spring-hateoas.html

How can i fix this?

+5
source share
2 answers

Did you @ExposesResourceFor your controller classes with @ExposesResourceFor

Of spring hate papers

  1. SPI modules

3.1. RelProvider API

When creating links, you usually need to determine the type of relationship that will be used> for the link. In most cases, the type of relationship is directly related to the type (domain). We encapsulate the detailed relationship type search algorithm behind the RelProvider API, which allows you to define relationship types for single and collection resources. Heres the relationship type search algorithm:

  • If the type is annotated using @Relation , we use the values โ€‹โ€‹configured in the annotation.

  • If not, we by default use the uncapitalized simple class name plus the added list for the rel collection.

  • in case the JAR EVO-inflector is on the way to the classes, we would rather use the plural of the single resource rel provided by the plurality algorithm.

  • @ Control classes annotated with @ExposesResourceFor (see EntityLinks for details) will transparently look for relationship types for the type specified in the annotation, so you can use relProvider.getSingleResourceRelFor (MyController.class) and get the type relationship of the open domain type.

RelProvider appears as a Spring bean when using @EnableHypermediaSupport automatically. You can plug in custom providers by simply implementing the interface and displaying them as a Spring bean in turn.

0
source

I have the same problem and have a dirty solution if you are interested. Implement private repositories for subclasses such as

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

Hope this gives you some temporary relief.

0
source

Source: https://habr.com/ru/post/1241184/


All Articles