我用党型号:如何在使用Spring Boot时为Spring HATEOAS配置自定义RelProvider?
@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 {
...
}
春季数据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
我该如何解决这个问题?
什么控制器类?这是你的界面上的Spring Data REST –
,它有@RepositoryRestResource注释 – stringy05