有许多选项可以定制 Spring Data REST。这些小节演示了如何操作。Spring中文文档

自定义项目资源 URI

默认情况下,项资源的 URI 由用于集合资源的路径段组成,并追加了数据库标识符。 这允许您使用存储库的方法查找实体实例。 从 Spring Data REST 2.5 开始,可以通过使用 配置 API(在 Java 8 上首选)或通过在应用程序中注册 Spring Bean 的实现来定制它。 Spring Data REST会拾取这些内容,并根据其实现调整URI生成。findOne(…)RepositoryRestConfigurationEntityLookupSpring中文文档

假定 a 具有唯一标识它的属性。 进一步假设我们在相应的存储库上有一个方法。UserusernameOptional<User> findByUsername(String username)Spring中文文档

在 Java 8 上,我们可以将映射方法注册为方法引用来调整 URI 的创建,如下所示:Spring中文文档

@Component
public class SpringDataRestCustomization implements RepositoryRestConfigurer {

  @Override
  public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
    config.withEntityLookup()
      .forRepository(UserRepository.class)
      .withIdMapping(User::getUsername)
      .withLookup(UserRepository::findByUsername);
  }
}

forRepository(…)将存储库类型作为第一个参数,将存储库域类型映射到某个目标类型的方法引用作为第二个参数,将另一个方法引用使用作为第一个参数提到的存储库映射回该值。Spring中文文档

如果你没有运行 Java 8 或更好的版本,你可以使用该方法,但它需要一些非常冗长的匿名内部类。 在较旧的 Java 版本上,您可能更愿意实现类似于以下内容的 a:UserEntityLookupSpring中文文档

@Component
public class UserEntityLookup extends EntityLookupSupport<User> {

    private final UserRepository repository;

    public UserEntityLookup(UserRepository repository) {
        this.repository = repository;
    }

    @Override
    public Serializable getResourceIdentifier(User entity) {
        return entity.getUsername();
    }

    @Override
    public Object lookupEntity(Serializable id) {
        return repository.findByUsername(id.toString());
    }
}

请注意如何返回 URI 创建要使用的用户名。要按从该方法返回的值加载实体实例,我们现在使用 .getResourceIdentifier(…)lookupEntity(…)UserRepositorySpring中文文档

自定义存储库公开

默认情况下,所有公共 Spring Data 存储库都用于公开 HTTP 资源,如存储库资源中所述。 包保护的存储库接口从此列表中排除,因为您表示其功能仅在包内部可见。 这可以通过在 上显式设置 a(通常通过枚举)来自定义。 可以配置以下值:RepositoryDetectionStrategyRepositoryDetectionStrategiesRepositoryRestConfigurationSpring中文文档

  • ALL— 公开所有 Spring Data 存储库,无论其 Java 可见性或注解配置如何。Spring中文文档

  • DEFAULT— 公开公共 Spring 数据存储库或显式注释且其属性未设置为 的存储库。@RepositoryRestResourceexportedfalseSpring中文文档

  • VISIBILITY— 仅公开公共 Spring 数据存储库,而不考虑注解配置。Spring中文文档

  • ANNOTATED— 仅公开显式注释的 Spring Data 存储库,其属性未设置为 。@RepositoryRestResourceexportedfalseSpring中文文档

如果需要应用自定义规则,只需手动实施即可。RepositoryDetectionStrategySpring中文文档

自定义支持的 HTTP 方法

自定义默认曝光

默认情况下,Spring Data REST 公开 HTTP 资源和方法,如存储库资源中所述,存储库基于哪些 CRUD 方法公开。 存储库不需要扩展,但也可以有选择地声明上述部分中描述的方法,资源暴露将随之而来。 例如,如果存储库未公开方法,则项资源将不支持 HTTP。CrudRepositorydelete(…)DELETESpring中文文档

如果需要声明一个供内部使用的方法,但不希望它触发 HTTP 方法公开,则可以使用 对存储库方法进行注释。 要像这样注释哪些方法,以删除对存储库资源中描述的 HTTP 方法的支持。@RestResource(exported = false)Spring中文文档

有时,在方法层面上管理风险暴露不够细粒度。 例如,该方法用于备份集合资源以及项目资源。 要有选择地定义应该公开哪些 HTTP 方法,可以使用 .save(…)POSTPUTPATCHRepositoryRestConfiguration.getExposureConfiguration()Spring中文文档

该类公开了一个基于 Lambda 的 API,用于定义全局规则和基于类型的规则:Spring中文文档

ExposureConfiguration config = repositoryRestConfiguration.getExposureConfiguration();

config.forDomainType(User.class).disablePutForCreation(); (1)
config.withItemExposure((metadata, httpMethods) -> httpMethods.disable(HttpMethod.PATCH)); (2)
1 禁用对 HTTP 直接创建项目资源的支持。PUT
2 在所有项目资源上禁用对 HTTP 的支持。PATCH
1 禁用对 HTTP 直接创建项目资源的支持。PUT
2 在所有项目资源上禁用对 HTTP 的支持。PATCH