Nei*_*gan 5 inheritance rel spring-data-rest spring-hateoas 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 {
...
}
Run Code Online (Sandbox Code Playgroud)
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"
}
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
但我需要它像这样回应:
{
"_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"
}
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
为此,我了解我需要提供自定义的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);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试在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();
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.它似乎没有注册,或正确注册.请参见http://andreitsibets.blogspot.ca/2014/04/hal-configuration-with-spring-hateoas.html
我怎样才能解决这个问题?
你是否用以下注释你的控制器类@ExposesResourceFor
\n\n\n
\n\n- SPI
\n3.1. 相关提供者API
\n\n构建链接时,您通常需要确定链接要使用的关系类型。在大多数情况下,关系类型直接与(域)类型关联。我们封装了详细的算法来查找 RelProvider API 背后的关系类型,该 API 允许确定单个资源和集合资源的关系类型。这里\xe2\x80\x99s查找关系类型的算法:
\n\n\n
\n\n- \n
如果类型用@Relation注释,我们将使用注释中配置的值。
- \n
如果没有,我们默认使用非大写的简单类名加上集合 rel 的附加列表。
- \n
如果 EVO inflector JAR 位于类路径中,我们宁愿使用复数算法提供的单个资源 rel 的复数形式。
- \n
使用 @ExposesResourceFor 注解的 @Controller 类(详细信息请参阅 EntityLinks)将透明地查找注解中配置的类型的关系类型,以便您可以使用 relProvider.getSingleResourceRelFor(MyController.class) 并获取暴露的域类型的关系类型。
自动使用 @EnableHypermediaSupport 时,RelProvider 将作为 Spring bean 公开。您可以通过简单地实现接口并将它们依次公开为 Spring bean 来插入自定义提供程序。
\n
归档时间: |
|
查看次数: |
1554 次 |
最近记录: |