# 使用REST訪問Pivotal GemFire中的數據
本指南將引導您完成創建應用程序的過程,該應用程序 訪問存儲在 數據 [Apache Geode中的](https://geode.apache.org/) 通過基于 [超媒體的](https://spring.io/guides/gs/rest-hateoas) [REST-ful前端](https://spring.io/understanding/REST) 。
## 你會建立什么
您將構建一個 *Spring* Web應用程序,讓您創建和檢索 `Person`存儲在 [Apache Geode](https://geode.apache.org/) 使用Spring Data REST 內存數據網格(IMDG)中的對象。 Spring Data REST具有 的 的功能, [Spring HATEOAS](https://projects.spring.io/spring-hateoas) 和 [適用于Apache Geode Spring Data](https://spring.io/projects/spring-data-geode) 并將它們自動組合在一起。
Spring Data REST還支持將 Spring Data JPA , Spring Data MongoDB 和 Spring Data Neo4j 作為后端數據存儲,但是這些都不屬于本指南的一部分。
有關Apache Geode概念的更多常識以及如何從Apache Geode訪問數據,請通讀指南“ 使用Apache Geode訪問數據” 。
## 你需要什么
* 約15分鐘
* 最喜歡的文本編輯器或IDE
* [JDK 1.8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) 或更高版本
* [Gradle 4+](http://www.gradle.org/downloads) 或 [Maven 3.2+](https://maven.apache.org/download.cgi)
* 您還可以將代碼直接導入到IDE中:
* [彈簧工具套件(STS)](https://spring.io/guides/gs/sts)
* [IntelliJ IDEA](https://spring.io/guides/gs/intellij-idea/)
## 如何完成本指南
像大多數Spring 一樣 [入門指南](https://spring.io/guides) ,您可以從頭開始并完成每個步驟,也可以繞過您已經熟悉的基本設置步驟。 無論哪種方式,您最終都可以使用代碼。
要 **從頭開始** ,請繼續進行“ [從Spring Initializr開始”](https://spring.io/guides/gs/accessing-gemfire-data-rest/#scratch) 。
要 **跳過基礎知識** ,請執行以下操作:
* [下載](https://github.com/spring-guides/gs-accessing-gemfire-data-rest/archive/master.zip) 并解壓縮本指南的源存儲庫,或使用 對其進行克隆 [Git](https://spring.io/understanding/Git) : `git clone [https://github.com/spring-guides/gs-accessing-gemfire-data-rest.git](https://github.com/spring-guides/gs-accessing-gemfire-data-rest.git)`
* 光盤進入 `gs-accessing-gemfire-data-rest/initial`
* 繼續 [創建域對象](https://spring.io/guides/gs/accessing-gemfire-data-rest/#initial) 。
**完成后** ,您可以根據中的代碼檢查結果 `gs-accessing-gemfire-data-rest/complete`.
## 從Spring Initializr開始
對于所有Spring應用程序,您應該從 開始 [Spring Initializr](https://start.spring.io) 。 Spring Initializr提供了一種快速的方法來提取應用程序所需的所有依賴關系,并為您完成了許多設置。 本示例需要“ *Spring for Apache Geode* ”依賴項。
以下清單顯示了一個示例 `pom.xml` 使用Maven時文件:
~~~
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
</parent>
<groupId>org.springframework</groupId>
<artifactId>gs-accessing-gemfire-data-rest</artifactId>
<version>0.1.0</version>
<properties>
<spring-shell.version>1.2.0.RELEASE</spring-shell.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-geode</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell</artifactId>
<version>${spring-shell.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
~~~
以下清單顯示了一個示例 `build.gradle`使用Gradle時的文件:
~~~
plugins {
id 'org.springframework.boot' version '2.4.1'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id "io.freefair.lombok" version "5.3.0"
id 'java'
}
apply plugin: 'eclipse'
apply plugin: 'idea'
group = "org.springframework"
version = "0.1.0"
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter-data-rest"
implementation "org.springframework.data:spring-data-geode"
implementation "org.projectlombok:lombok"
runtimeOnly "org.springframework.shell:spring-shell:1.2.0.RELEASE"
testImplementation "org.springframework.boot:spring-boot-starter-test"
}
test {
useJUnitPlatform()
}
bootJar {
baseName = 'gs-accessing-gemfire-data-rest'
version = '0.1.0'
}
~~~
## 創建一個域對象
創建一個新的域對象來呈現一個人。
`src/main/java/hello/Person.java`
~~~
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.gemfire.mapping.annotation.Region;
import lombok.Data;
@Data
@Region("People")
public class Person {
private static AtomicLong COUNTER = new AtomicLong(0L);
@Id
private Long id;
private String firstName;
private String lastName;
@PersistenceConstructor
public Person() {
this.id = COUNTER.incrementAndGet();
}
}
~~~
這 `Person`有名字和姓氏。 Apache Geode域對象需要一個ID,因此 `AtomicLong` 被用來增加每個 `Person` 對象創建。
## 創建個人資料庫
接下來,您需要創建一個簡單的 *存儲庫* 以持久化/訪問 `Person` 存儲在Apache Geode中的對象。
`src/main/java/hello/PersonRepository.java`
~~~
package hello;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends CrudRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
~~~
該 *存儲庫* 是一個界面,使您可以執行涉及以下內容的各種數據訪問操作(例如,基本的CRUD和簡單查詢) `Person`對象。 它通過擴展來獲得這些操作 `CrudRepository`.
在運行時, *Spring Data for Apache Geode* 將自動創建此接口的實現。 然后,Spring Data REST將使用 [@RepositoryRestResource](https://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/core/annotation/RepositoryRestResource.html) 批注來指示Spring MVC在以下位置創建REST風格的端點 `/people`.
@RepositoryRestResource不需要 存儲庫 導出 。 它僅用于更改導出詳細信息,例如使用 /people 而不是默認值 /persons.
在這里,您還定義了一個自定義查詢來檢索以下內容的列表 `Person` 基于的對象 `lastName`。 您將在本指南中進一步了解如何調用它。
## 使應用程序可執行
盡管可以將該服務打包為傳統的 [WAR](https://spring.io/understanding/WAR) 文件以部署到外部應用程序服務器,但是下面演示的更簡單的方法創建了一個獨立的應用程序。 您將所有內容打包在一個可執行的JAR文件中,由一個好的舊Java驅動 `main()`方法。 在此過程中,您將使用 *Spring的* 支持將 嵌入 [Tomcat](https://spring.io/understanding/Tomcat) servlet容器作為HTTP運行時 ,而不是部署到外部servlet容器。
`src/main/java/hello/Application.java`
~~~
package hello;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
@SpringBootApplication
@ClientCacheApplication(name = "AccessingGemFireDataRestApplication")
@EnableEntityDefinedRegions(
basePackageClasses = Person.class,
clientRegionShortcut = ClientRegionShortcut.LOCAL
)
@EnableGemfireRepositories
@SuppressWarnings("unused")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
~~~
`@SpringBootApplication` 是一個方便注釋,它添加了以下所有內容:
* `@Configuration`:將類標記為應用程序上下文的Bean定義的源。
* `@EnableAutoConfiguration`:告訴Spring Boot根據類路徑設置,其他bean和各種屬性設置開始添加bean。 例如,如果 `spring-webmvc` 在類路徑上,此注釋將應用程序標記為Web應用程序并激活關鍵行為,例如設置 `DispatcherServlet`.
* `@ComponentScan`:告訴Spring在服務器中尋找其他組件,配置和服務 `hello` 包,讓它找到控制器。
這 `main()` 方法使用Spring Boot的 `SpringApplication.run()`啟動應用程序的方法。 您是否注意到沒有一行XML? 沒有 `web.xml`文件。 該Web應用程序是100%純Java,因此您無需處理任何管道或基礎結構。
The `@EnableGemfireRepositories` annotation activates *Spring Data for Apache Geode* *Repositories*. *Spring Data for Apache Geode* will create a concrete implementation of the `PersonRepository` interface and configure it to talk to an embedded instance of Apache Geode.
### 建立可執行的JAR
您可以使用Gradle或Maven從命令行運行該應用程序。 您還可以構建一個包含所有必需的依賴項,類和資源的可執行JAR文件,然后運行該文件。 生成可執行jar使得在整個開發生命周期中,跨不同環境等等的情況下,都可以輕松地將服務作為應用程序進行發布,版本控制和部署。
如果您使用Gradle,則可以通過使用以下命令運行該應用程序 `./gradlew bootRun`。 或者,您可以通過使用以下命令構建JAR文件: `./gradlew build` 然后運行JAR文件,如下所示:
~~~
java -jar build/libs/gs-accessing-gemfire-data-rest-0.1.0.jar
~~~
如果您使用Maven,則可以通過使用以下命令運行該應用程序 `./mvnw spring-boot:run`。 或者,您可以使用以下命令構建JAR文件: `./mvnw clean package` 然后運行JAR文件,如下所示:
~~~
java -jar target/gs-accessing-gemfire-data-rest-0.1.0.jar
~~~
此處描述的步驟將創建可運行的JAR。 您還可以 構建經典的WAR文件 。
顯示日志記錄輸出。 該服務應在幾秒鐘內啟動并運行。
## 測試應用程序
現在該應用程序正在運行,您可以對其進行測試。 您可以使用任何所需的REST客戶端。 以下示例使用\* nix工具 `curl`.
首先,您要查看頂級服務。
~~~
$ curl http://localhost:8080
{
"_links" : {
"people" : {
"href" : "http://localhost:8080/people"
}
}
}
~~~
在這里,您可以初步了解該服務器所提供的功能。 有一個 **人員** 在 鏈接 [http:// localhost:8080 / people上](http://localhost:8080/people) 。 *Apache Geode* 的Spring Data不像其他Spring Data REST指南那樣支持分頁,因此沒有額外的導航鏈接。
Spring Data REST使用 HAL格式 進行JSON輸出。 它非常靈活,并提供了一種便捷的方式來提供與所提供數據相鄰的鏈接。
~~~
$ curl http://localhost:8080/people
{
"_links" : {
"search" : {
"href" : "http://localhost:8080/people/search"
}
}
}
~~~
是時候創建一個新的了 `Person`!
~~~
$ curl -i -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Frodo", "lastName" : "Baggins" }' http://localhost:8080/people
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: http://localhost:8080/people/1
Content-Length: 0
Date: Wed, 05 Mar 2014 20:16:11 GMT
~~~
* `-i`確保您可以看到包含標題的響應消息。 新創建的URI `Person` 顯示
* `-X POST` 發出一個 `POST` 創建新條目的HTTP請求
* `-H "Content-Type:application/json"` 設置內容類型,以便應用程序知道有效負載包含JSON對象
* `-d '{ "firstName" : "Frodo", "lastName" : "Baggins" }'` 是正在發送的數據
注意前一個 POST 操作包括 Location標頭。 它包含新創建的資源的URI。 Spring Data REST也有兩種方法 RepositoryRestConfiguration.setReturnBodyOnCreate(…) 和 setReturnBodyOnCreate(…) 您可以使用它配置框架以立即返回剛剛創建的資源的表示形式。
由此您可以查詢所有人:
~~~
$ curl http://localhost:8080/people
{
"_links" : {
"search" : {
"href" : "http://localhost:8080/people/search"
}
},
"_embedded" : {
"persons" : [ {
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
} ]
}
}
~~~
該 **人** 收集資源包含了弗羅多的列表。 注意它如何包含一個 **自我** 鏈接。 Spring Data REST還使用 [Evo Inflector](https://www.atteo.org/2011/12/12/Evo-Inflector.html) 來對實體名稱進行復數以進行分組。
您可以直接查詢單個記錄:
~~~
$ curl http://localhost:8080/people/1
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
}
~~~
這似乎純粹是基于Web的,但是在后臺,它正在與嵌入式Apache Geode數據庫進行通信。
在本指南中,只有一個域對象。 在域對象相互關聯的更復雜的系統中,Spring Data REST將提供附加鏈接以幫助導航到連接的記錄。
查找所有自定義查詢:
~~~
$ curl http://localhost:8080/people/search
{
"_links" : {
"findByLastName" : {
"href" : "http://localhost:8080/people/search/findByLastName{?name}",
"templated" : true
}
}
}
~~~
您可以看到查詢的URL,包括HTTP查詢參數 `name`。 如果您會注意到,這與 `@Param("name")` 批注嵌入在界面中。
要使用 `findByLastName` 查詢,執行以下操作:
~~~
$ curl http://localhost:8080/people/search/findByLastName?name=Baggins
{
"_embedded" : {
"persons" : [ {
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
} ]
}
}
~~~
因為您將其定義為返回 `List<Person>`在代碼中,它將返回所有結果。 如果您已將其定義為僅返回 `Person`,它將選擇其中之一 `Person`要返回的對象。 由于這可能是不可預測的,因此您可能不想對可以返回多個條目的查詢執行此操作。
您也可以發出 `PUT`, `PATCH`, 和 `DELETE` REST調用以替換,更新或刪除現有記錄。
~~~
$ curl -X PUT -H "Content-Type:application/json" -d '{ "firstName": "Bilbo", "lastName": "Baggins" }' http://localhost:8080/people/1
$ curl http://localhost:8080/people/1
{
"firstName" : "Bilbo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
}
~~~
~~~
$ curl -X PATCH -H "Content-Type:application/json" -d '{ "firstName": "Bilbo Jr." }' http://localhost:8080/people/1
$ curl http://localhost:8080/people/1
{
"firstName" : "Bilbo Jr.",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
}
~~~
PUT替換整個記錄。 未提供的字段將被替換為 null. PATCH 可用于更新項的子集。
您可以刪除記錄:
~~~
$ curl -X DELETE http://localhost:8080/people/1
$ curl http://localhost:8080/people
{
"_links" : {
"search" : {
"href" : "http://localhost:8080/people/search"
}
}
}
~~~
此 一個非常方便的方面 [超媒體驅動的界面的](https://spring.io/understanding/HATEOAS) 是如何使用以下方法發現所有REST風格的端點 `curl`(或您使用的任何REST客戶端)。 無需與客戶交換正式的合同或接口文檔。
## 概括
恭喜你! 您剛剛使用基于 開發了一個應用程序 [超媒體的](https://spring.io/guides/gs/rest-hateoas) [RESTful](https://spring.io/understanding/REST) 前端和基于Apache Geode的后端 。
- springboot概述
- springboot構建restful服務
- spring構建一個RESTful Web服務
- spring定時任務
- 消費RESTful Web服務
- gradle構建項目
- maven構建項目
- springboot使用jdbc
- springboot應用上傳文件
- 使用LDNA驗證用戶
- 使用 spring data redis
- 使用 spring RabbitTemplate消息隊列
- 用no4j訪問nosql數據庫
- springboot驗證web表單
- Spring Boot Actuator構j建服務
- 使用jms傳遞消息
- springboot創建批處理服務
- spring security保護web 安全
- 在Pivotal GemFire中訪問數據
- 使用Spring Integration
- 使用springboot jpa進行數據庫操作
- 數據庫事務操作
- 操作mongodb
- springmvc+tymleaf創建web應用
- 將Spring Boot JAR應用程序轉換為WAR
- 創建異步服務
- spring提交表單
- 使用WebSocket構建交互式Web應用程序
- 使用REST訪問Neo4j數據
- jquery消費restful
- springboot跨域請求
- 消費SOAP Web服務
- springboot使用緩存
- 使用Vaadin創建CRUD UI
- 使用REST訪問JPA數據
- 使用REST訪問Pivotal GemFire中的數據
- 構建soap服務
- 使用rest訪問mongodb數據
- 構建springboot應用docker鏡像
- 從STS部署到Cloud Foundry
- springboot測試web應用
- springboot訪問mysql
- springboot編寫自定義模塊并使用
- 使用Google Cloud Pub / Sub進行消息傳遞
- 構建反應式RESTful Web服務
- 使用Redis主動訪問數據
- Spring Boot 部署到Kubernetes
- 使用反應式協議R2DBC訪問數據
- Spring Security架構
- spring構建Docker鏡像詳解
- Spring Boot和OAuth2
- springboot應用部署到k8s
- spring構建rest服務詳解