Tôi đang sử dụng mô hình bên:Làm thế nào để cấu hình một RelProvider tùy chỉnh cho Spring HATEOAS khi sử dụng Spring Boot?
@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 {
...
}
mùa xuân dữ liệu REST của phản ứng như thế này:
{
"_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"
}
}
}
]
}
}
Nhưng tôi cần nó để đáp ứng như thế này:
{
"_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"
}
}
}
]
}
}
Để làm như vậy , Tôi hiểu tô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);
}
}
tôi đã cố gắng cấu hình nó trong 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();
}
}
này không hoạt động mặc dù. Có vẻ như không được đăng ký hoặc đăng ký chính xác. Xem http://andreitsibets.blogspot.ca/2014/04/hal-configuration-with-spring-hateoas.html
Làm cách nào để khắc phục sự cố này?
lớp điều khiển nào? Đây là Spring Data REST –
trên giao diện của bạn có chú thích @RepositoryRestResource – stringy05