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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With