Spring 使用 FeignClient 启动 RepositoryRestResource

Spring Boot RepositoryRestResource with FeignClient

我构建了两个 spring-boot 应用程序,服务器端 spring- 带有剩余资源的引导微服务和客户端 spring- 引导使用 HATEOAS 提要的微服务应用程序使用 Feign 客户端。

我在两边都有两个实体对象聚合和网关。网关在聚合对象内

只要我没有网关对象的@RepositoryRestResource 接口,我就可以通过聚合检索网关对象,但如果我有注释,我就无法在客户端获取聚合对象上列出的网关。我注意到这是因为服务器端 HATEOAS 提要在聚合上为网关添加 link 而不是为网关添加 Json 结构。

我是否仍然可以在具有网关对象的@RepositoryRestResource 接口的同时从聚合对象获取网关对象?或者有没有办法配置 Feign Client 来填充来自 link?

的 Gateway 对象

例如.. 来自客户 http://localhost:9999/aggregates/

在 GatewayRepository 上使用 @RepositoryRestResource 注释

[
  {
    "id": "a65b4bf7-6ba5-4086-8ca2-783b04322161",
    "gateway": null, //<-- Gateway is null here
    .......

GatewayRepository 上没有@RepositoryRestResource 注释

[
  {
    "id": "a65b4bf7-6ba5-4086-8ca2-783b04322161",
    "gateway": { //<-- Gateway id and properties are there now on Aggregate object
      "id": "4a857a7a-2815-454c-a271-65bf56dc6f79",
    .......

来自服务器http://localhost:8000/aggregates/

在 GatewayRepository 上使用 @RepositoryRestResource 注释

{
  "_embedded": {
    "aggregates": [
      {
        "id": "a65b4bf7-6ba5-4086-8ca2-783b04322161",
        "_links": {
          "self": {
            "href": "http://localhost:8000/aggregates/a65b4bf7-6ba5-4086-8ca2-783b04322161"
          },
          "gateway": { //<-- Gateway becomes a link here
            "href": "http://localhost:8000/aggregates/a65b4bf7-6ba5-4086-8ca2-783b04322161/gateway"
          },
        .......

GatewayRepository 上没有@RepositoryRestResource 注释

  "_embedded": {
    "aggregates": [
      {
        "id": "b5171138-4313-437a-86f5-f70b2b5fcd22",
        "gateway": { //<-- Gateway id and properties are there now on Aggregate object
          "id": "3608726b-b1b1-4bd4-b861-ee2bf5c0cc03",
        .......

这是我的模型对象的服务器端实现

@Entity
class Aggregate extends TemplateObject {
    @OneToOne(cascade = CascadeType.MERGE)
    private Gateway gateway;
    .......
}

@Entity
class Gateway extends TemplateObject {
    @NotNull
    @Column(unique = true)
    private String name;
    .......
}

服务器端其余存储库是

@RepositoryRestResource
interface GatewayRepository extends JpaRepository<Gateway, String> {
    Optional<Gateway> findByName(@Param("name") String name);
}

@RepositoryRestResource
interface AggregateRepository extends JpaRepository<Aggregate, String> {
    Optional<Aggregate> findByName(@Param("name") String name);
}

(在端口 8000 上使用这些 Rest 资源)

在客户端,我对模型 dto 对象进行了相同的植入

class Gateway extends TemplateObject {
    @NotNull
    private String name;
    .......
}

class Aggregate extends TemplateObject {
    private Gateway gateway;
    .......
}

和 Feign 客户

@FeignClient("billing-service/gateways")
interface GatewayService extends GenericService<Gateway> {
}

@FeignClient("billing-service/aggregates")
interface AggregateService extends GenericService<Aggregate> {
}

(在端口 9999 客户端控制器上使用这些 Feign 客户端)

在此先感谢您的帮助,非常感谢任何意见和建议

您可以看看将投影与您的其余存储库一起使用 - 看看 here。之后,您只需调整 FeignClient 请求路径,以提供相应的投影作为参数。