Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Inheritance 如何在使用Spring Boot时为Spring HATEOAS配置自定义RelProvider?_Inheritance_Spring Boot_Spring Data Rest_Spring Hateoas_Rel - Fatal编程技术网

Inheritance 如何在使用Spring Boot时为Spring HATEOAS配置自定义RelProvider?

Inheritance 如何在使用Spring Boot时为Spring HATEOAS配置自定义RelProvider?,inheritance,spring-boot,spring-data-rest,spring-hateoas,rel,Inheritance,Spring Boot,Spring Data Rest,Spring Hateoas,Rel,我使用的是派对模式: @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 =

我使用的是派对模式:

@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 {
  ...
}
Spring数据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"
          }
        }
      }
    ]
  }
}
但我需要它这样回应:

{
  "_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"
          }
        }
      }
    ]
  }
}
为此,我理解我:

但这不起作用。它似乎没有注册,或者注册不正确。看


如何修复此问题?

您是否已使用
@exposeSourceFor

  • SPIs
  • 3.1。relproviderAPI

    构建链接时,通常需要确定链接要使用的关系类型>。在大多数情况下,关系类型与(域)类型直接关联。我们封装了详细的算法来查找RelProvider API后面的关系类型,该API允许确定单个和集合资源的关系类型。以下是查找关系类型的算法:

  • 如果使用注释对类型进行注释,则使用注释中配置的值

  • 如果不是,则默认为未资本化的简单类名加上集合rel的附加列表

  • 如果EVO拐点JAR在类路径中,我们使用复数化算法提供的单个资源rel的复数形式

  • @使用@ExposesResourceFor注释的控制器类(有关详细信息,请参阅EntityLinks)将透明地查找注释中配置的类型的关系类型,以便您可以使用relProvider.getSingleResourceRelFor(MyController.class)并获取域类型的关系类型

  • 当自动使用@EnableHypermediaSupport时,RelProvider作为Springbean公开。您可以通过简单地实现接口并依次将其作为Springbean公开来插入自定义提供程序


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

    @RepositoryRestResource(path = "parties")    
    interface IndividualRepository extends PagingAndSortingRepository<Individual, Long>{
    }
    
    @RepositoryRestResource(path=“双方”)
    接口IndividualRepository扩展了分页和排序存储库{
    }
    

    希望这能给你一些暂时的缓解

    什么控制器类?这是界面上的Spring数据REST,具有@RepositoryRestResource注释