【发布时间】:2016-01-22 00:47:06
【问题描述】:
我正在使用派对模式:
@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 响应如下:
{
"_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"
}
}
}
]
}
}
但我需要它这样回应:
{
"_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"
}
}
}
]
}
}
为此,我了解我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);
}
}
我尝试在 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();
}
}
但这不起作用。它似乎没有注册,或者没有正确注册。见http://andreitsibets.blogspot.ca/2014/04/hal-configuration-with-spring-hateoas.html
我该如何解决这个问题?
【问题讨论】:
标签: inheritance spring-boot spring-data-rest spring-hateoas rel