【问题标题】:How to configure a custom RelProvider for Spring HATEOAS when using Spring Boot?使用 Spring Boot 时如何为 Spring HATEOAS 配置自定义 RelProvider?
【发布时间】:2016-01-22 00:47:06
【问题描述】:

我正在使用派对模式:

@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 Data 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"
          }
        }
      }
    ]
  }
}

为此,我了解我need to provide a custom RelProvider

@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public class MyRelProvider implements RelProvider {

    public MyRelProvider() {}

    @Override
    public String getItemResourceRelFor(Class<?> aClass) {
        return "party";

    }

    @Override
    public String getCollectionResourceRelFor(Class<?> aClass) {
        return "parties";
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return aClass.isAssignableFrom(Party.class);
    }
}

我尝试在 Application.java 中配置它:

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    RelProvider myRelProvider() {
        return new MyRelProvider();
    }
}

但这不起作用。它似乎没有注册,或者没有正确注册。见http://andreitsibets.blogspot.ca/2014/04/hal-configuration-with-spring-hateoas.html

我该如何解决这个问题?

【问题讨论】:

    标签: inheritance spring-boot spring-data-rest spring-hateoas rel


    【解决方案1】:

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

    @RepositoryRestResource(path = "parties")    
    interface IndividualRepository extends PagingAndSortingRepository<Individual, Long>{
    }
    

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

    【讨论】:

      【解决方案2】:

      你有没有用@ExposesResourceFor注释你的控制器类

      来自spring hateos docs

      1. SPI

      3.1。 RelProvider API

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

      1. 如果使用@Relation 注释类型,我们将使用注释中配置的值。

      2. 如果不是,我们默认为未大写的简单类名加上集合 rel 的附加列表。

      3. 如果 EVO 变形器 JAR 在类路径中,我们宁愿使用复数算法提供的单个资源 rel 的复数。

      4. 使用@ExposesResourceFor 注解的@Controller 类(详见EntityLinks)将透明地查找注解中配置的类型的关系类型,以便您可以使用relProvider.getSingleResourceRelFor(MyController.class) 并获取关系type 暴露的域类型。

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

      【讨论】:

      • 什么控制器类?这是 Spring Data REST
      • 在具有@RepositoryRestResource 注释的界面上
      猜你喜欢
      • 1970-01-01
      • 2014-07-22
      • 2018-12-24
      • 2020-02-02
      • 2016-09-08
      • 2020-11-06
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多