5

我用党型号:如何在使用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

我该如何解决这个问题?

回答

0

有你@ExposesResourceFor

注解控制器类从spring hateos docs

  • 的SPI
  • 3.1。 RelProvider API

    建立链接时,通常需要确定要用于链接的关系类型>。在大多数情况下,关系类型直接与(域)类型相关联。我们封装了详细的算法以查找RelProvider API背后的关系类型,以确定单个和集合资源的关系类型。这里的算法的关系型抬头:

    1. 如果类型与@Relation注释我们使用注解配置的值。

    2. 如果不是,我们默认为未加帽的简单类名加上收藏品rel的附加列表。

    3. 如果EVO变形器JAR在类路径中,我们更倾向于使用多元化算法提供的多个单资源rel。

    4. 与@ExposesResourceFor注释(详见EntityLinks)将透明地查找关系类型的注释中配置的类型,这样就可以使用relProvider.getSingleResourceRelFor(MyController.class),并获得关系类型@Controller类的域类型暴露。

    自动使用@EnableHypermediaSupport时,RelProvider作为Spring bean公开。您可以通过简单地实现接口并将它们公开为Spring bean来插入自定义提供程序。

    +0

    什么控制器类?这是你的界面上的Spring Data REST –

    +0

    ,它有@RepositoryRestResource注释 – stringy05

    0

    我有同样的问题,并有一个肮脏的解决方案,如果你有兴趣。为子类实现私人存储库,如

    @RepositoryRestResource(path = "parties")  
    interface IndividualRepository extends PagingAndSortingRepository<Individual, Long>{ 
    } 
    

    希望能给你一些暂时的解脱。

    +0

    我会试一试。谢谢 –

    相关问题