通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
# 使用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的后端 。