@RepositoryRestResource 的 collectionResourceRel 属性未被遵守

@RepositoryRestResource's collectionResourceRel attribute not being obeyed

我有一个

的MongoRepository
@RepositoryRestResource(collectionResourceRel = "tools")
public interface ToolRepository extends MongoRepository<Tool, Long>

工具可以是 2 种实现之一:

public class Screwdriver extends Tool
public class Hammer extends Tool

使用@JsonTypeInfo

映射工具
@JsonTypeInfo(use = 
com.fasterxml.jackson.annotation.JsonTypeInfo.Id.CLASS, include = 
As.PROPERTY, property = "_class")
public abstract class Tool

当我做 toolRepository.findAll() 这个 returns 的 JSON 响应时:

{
  "_embedded" : {
    "screwdrivers" : [ {
      "name" : "Screwdriver",
      ...
    } ],
    "hammers" : [ {
      "name" : "Hammer",
      ...
      }
 }

预期的响应应该是:

{
  "_embedded" : {
    "tools" : [ {
      "name" : "Screwdriver",
      ...
      },
      {
      "name" : "Hammer",
      ...
      }
 }

在 Json 映射数据中 class 没有遵守 collectionResourceRel

进一步调查; PersistentEntitiesResourceMapping.getMetadataFor()(在 Spring 内)是说确保如果 ResourceMetadata 缓存中没有这些子 class 工具的条目,则使用 TypeBasedCollectionResourceMapping,这会导致每个 class 在 json 响应中有自己的条目。

有没有办法告诉 Spring data rest 特定子 class 应该绑定到特定存储库,在这种情况下有没有办法告诉 Spring 其余数据表明 Screwdriver 是 ToolRepository 的一部分,因此应该使用此存储库的 collectionResourceRel

尝试设置这些注释:

@RestResource(rel = "tools", path = "tools")
public abstract class Tool {...}

@RepositoryRestResource(path = "tools", collectionResourceRel = "tools", itemResourceRel = "tool")
public interface ToolRepository extends MongoRepository<Tool, Long> {...}

查看工作 example。这与 Mongo 无关,但我相信它会有用...