未遵守@RepositoryRestResource 的 collectionResourceRel 属性 [英] @RepositoryRestResource's collectionResourceRel attribute not being obeyed

查看:39
本文介绍了未遵守@RepositoryRestResource 的 collectionResourceRel 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

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

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

Tool can be one of 2 implementations:

public class Screwdriver extends Tool
public class Hammer extends Tool

使用@JsonTypeInfo 映射工具

Tool is mapped using @JsonTypeInfo

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

当我执行 toolRepository.findAll() 这会返回一个 JSON 响应:

When I do toolRepository.findAll() this returns a JSON response of :

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

预期的响应应该是:

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

对于包含 Json 映射数据的类,collectionResourceRel 没有被遵守.

The collectionResourceRel is not being obeyed for classes with Json mapping data in.

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

Investigating further; PersistentEntitiesResourceMapping.getMetadataFor() (within Spring) is saying make sure that if there's no entry for these subclasses of tool inside the ResourceMetadata cache then use a TypeBasedCollectionResourceMapping which results in each class having its own entry in the json response.

有没有办法告诉 Spring data rest 一个特定的子类应该绑定到一个特定的存储库,在这种情况下有没有办法告诉 Spring data rest 螺丝刀是 ToolRepository 因此应该使用此存储库的 collectionResourceRel 吗?

Is there a way of telling Spring data rest that a specific subclass should be bound to a specific repository, in this case is there a way of telling Spring data rest that Screwdriver is part of the ToolRepository and therefore should use the collectionResourceRel of this repository?

推荐答案

尝试设置这些注释:

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

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

参见工作示例.这与 Mongo 无关,但我相信它会很有用...

See working example. It's not about Mongo but I'm sure it will be useful...

这篇关于未遵守@RepositoryRestResource 的 collectionResourceRel 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆