带有 FeignClient 的 Spring Boot RepositoryRestResource

Posted

技术标签:

【中文标题】带有 FeignClient 的 Spring Boot RepositoryRestResource【英文标题】:Spring Boot RepositoryRestResource with FeignClient 【发布时间】:2017-08-31 17:04:22 【问题描述】:

我已经构建了两个 spring-boot 应用程序,服务器端 spring-boot 微服务与休息资源和客户端 spring-boot 微服务应用程序使用 HATEOAS 提要使用 Feign 客户端。

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

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

在拥有网关对象的@RepositoryRestResource 接口的同时,我仍然可以从聚合对象获取网关对象吗?或者有没有办法配置 Feign Client 从链接中填充 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;
    .......

而服务器端的 rest 存储库是

@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 客户端)

提前感谢您的帮助,非常感谢任何建议和建议

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于带有 FeignClient 的 Spring Boot RepositoryRestResource的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Netflix:通过 RequestInterceptor 将主机请求参数传递给 FeignClient

在 Spring Boot 中使用 FeignClient 进行同步 Rest API 调用

Spring FeignClient使用

如何在 Spring Cloud 中禁用 Ribbon 并仅使用 FeignClient

在 Spring Boot 微服务中使用 FeignClient 报错 302

是否可以在没有功能区的情况下使用@FeignClient?