【问题标题】:Spring Boot RepositoryRestResource with FeignClient带有 FeignClient 的 Spring Boot RepositoryRestResource
【发布时间】: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 客户端)

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

【问题讨论】:

    标签: spring spring-boot spring-data-rest hateoas feign


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 2018-01-29
      • 2016-04-13
      • 2016-12-01
      • 2015-07-08
      • 1970-01-01
      • 2014-08-25
      相关资源
      最近更新 更多