2017-06-26 140 views
0

@ RepositoryRestResource的collectionResourceRel属性不被服从

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

工具的MongoRepository可以是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()这将返回一个JSON响应:

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

预期的反应应该是:

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

collectionResourceRel不被服从用于使用JSON映射数据类中

进一步调查; (在Spring中)要求确保如果ResourceMetadata缓存中没有这些工具子类的条目,则使用TypeBasedCollectionResourceMapping,这会导致每个类在json响应中都有其自己的条目。

有没有告诉春天数据休息一个特定的子类应该绑定到特定的资源库,在这种情况下的方式会告诉他春天数据的方式休息那把螺丝刀是ToolRepository的一部分,因此应该使用这个存储库的collectionResourceRel

回答

1

尝试设置这些注释:

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

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

见工作example。这不是关于蒙戈,但我相信它会有用...

+0

非常感谢 - '@ RestResource'似乎有诀窍。有些讨厌的是必须将注释添加到我的所有子类中(我比上面多了几个),但它将我的问题排除在外。再次感谢您 –

+1

忽略上述注意事项 - 将注释添加到假设的“工具”类中可以避免我必须将其添加到所有子类的问题,因此完美答案。 –

+0

@ alex.p在我的代码中确认相同的行为。谢谢Alex!我会纠正我的答案...... – Cepr0