開始

Spring Data REST本身是一個Spring MVC應用,其設計宗旨是儘可能方便地與現有Spring MVC應用整合。一個現有(或未來的)服務層可以與Spring Data REST並行執行,只需少量額外工作。

將Spring Data REST新增到Spring Boot專案

最簡單的入門方法是構建一個Spring Boot應用,因為Spring Boot為Spring Data REST提供了啟動器並使用自動配置。以下示例展示瞭如何使用Gradle在Spring Boot專案中包含Spring Data REST

示例 1. 使用Gradle的Spring Boot配置
dependencies {
  ...
  implementation("org.springframework.boot:spring-boot-starter-data-rest")
  ...
}

以下示例展示瞭如何使用Maven在Spring Boot專案中包含Spring Data REST

示例 2. 使用Maven的Spring Boot配置
<dependencies>
  ...
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
  </dependency>
  ...
</dependencies>
如果您使用Spring Boot Gradle外掛Spring Boot Maven外掛,則無需提供版本號。

當您使用Spring Boot時,Spring Data REST會自動配置。

將Spring Data REST新增到Gradle專案

要將Spring Data REST新增到基於Gradle的專案,請將spring-data-rest-webmvc artifact新增到您的編譯時依賴項中,如下所示

dependencies {
  … other project dependencies
  implementation("org.springframework.data:spring-data-rest-webmvc:5.0.0")
}

將Spring Data REST新增到Maven專案

要將Spring Data REST新增到基於Maven的專案,請將spring-data-rest-webmvc artifact新增到您的編譯時依賴項中,如下所示

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-rest-webmvc</artifactId>
  <version>5.0.0</version>
</dependency>

配置Spring Data REST

要將Spring Data REST與現有Spring MVC應用一起安裝,您需要包含適當的MVC配置。Spring Data REST配置在名為RepositoryRestMvcConfiguration的類中定義,您可以將該類匯入到您的應用配置中。

如果您使用Spring Boot的自動配置,則此步驟是不必要的。當您包含spring-boot-starter-data-rest,並且在您的依賴項列表中,您的應用程式帶有@SpringBootApplication@EnableAutoConfiguration標記時,Spring Boot會自動啟用Spring Data REST。

要自定義配置,請註冊一個RepositoryRestConfigurer並實現或覆蓋與您的用例相關的configure…方法。

確保您還為您使用的儲存配置Spring Data倉庫。有關詳細資訊,請參閱相應Spring Data模組的參考文件。

Spring Data REST的基本設定

本節介紹配置Spring Data REST應用時可以操作的基本設定,包括

設定倉庫檢測策略

Spring Data REST使用RepositoryDetectionStrategy來確定是否將倉庫匯出為REST資源。RepositoryDiscoveryStrategies列舉包含以下值

表 1. 倉庫檢測策略

名稱

描述

DEFAULT

暴露所有公共倉庫介面,但考慮@(Repository)RestResourceexported標誌。

全部 (ALL)

暴露所有倉庫,無論型別可見性和註解如何。

已註解 (ANNOTATED)

只有使用@(Repository)RestResource註解的倉庫才會被暴露,除非它們的exported標誌設定為false

可見性 (VISIBILITY)

僅暴露使用註解的公共倉庫。

更改基本URI

預設情況下,Spring Data REST在根URI '/' 提供REST資源。有多種方法可以更改基本路徑。

對於Spring Boot 1.2及更高版本,您可以透過在application.properties中設定一個屬性來更改基本URI,如下所示

spring.data.rest.basePath=/api

對於Spring Boot 1.1或更早版本,或者如果您不使用Spring Boot,您可以執行以下操作

@Configuration
class CustomRestMvcConfiguration {

  @Bean
  public RepositoryRestConfigurer repositoryRestConfigurer() {

    return new RepositoryRestConfigurer() {

      @Override
      public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
        config.setBasePath("/api");
      }
    };
  }
}

或者,您可以將RepositoryRestConfigurer的自定義實現註冊為Spring bean,並確保它透過元件掃描被發現,如下所示

@Component
public class CustomizedRestMvcConfiguration extends RepositoryRestConfigurer {

  @Override
  public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
    config.setBasePath("/api");
  }
}

上述兩種方法都將基本路徑更改為/api

更改其他Spring Data REST屬性

您可以更改以下屬性

表 2. Spring Boot可配置屬性

財產

描述

basePath

Spring Data REST的根URI

defaultPageSize

更改單頁中提供專案的預設數量

maxPageSize

更改單頁中專案的最大數量

pageParamName

更改用於選擇頁面的查詢引數名稱

limitParamName

更改用於指定單頁中顯示專案數量的查詢引數名稱

sortParamName

更改用於排序的查詢引數名稱

defaultMediaType

更改未指定時使用的預設媒體型別

returnBodyOnCreate

更改建立新實體時是否應返回響應體

returnBodyOnUpdate

更改更新實體時是否應返回響應體

啟動應用

此時,您還必須配置您的關鍵資料儲存。

Spring Data REST正式支援

以下入門指南可以幫助您快速啟動和執行

這些連結的指南介紹瞭如何新增相關資料儲存的依賴項、配置領域物件以及定義倉庫。

您可以將應用程式作為Spring Boot應用程式執行(使用前面顯示的連結),也可以將其配置為經典的Spring MVC應用程式。

通常,Spring Data REST不會向給定資料儲存新增功能。這意味著,根據定義,它應該適用於任何支援倉庫程式設計模型的Spring Data專案。上面列出的資料儲存是我們編寫了整合測試以驗證Spring Data REST可以與之配合使用的資料儲存。

從這裡開始,您可以透過各種選項自定義Spring Data REST

© . This site is unofficial and not affiliated with VMware.