<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 使用REST訪問Neo4j數據 本指南將引導您完成創建通過基于超媒體的RESTful前端訪問基于圖的數據的應用程序的過程。 ## 你會建立什么 您將構建一個Spring應用程序,使您可以創建和檢索 `Person`存儲在 中的對象 [Neo4j](https://www.neo4j.org/) 使用Spring Data REST NoSQL數據庫 。 Spring Data REST具有 的功能, [Spring HATEOAS](https://projects.spring.io/spring-hateoas) 和 [Spring Data Neo4j](https://projects.spring.io/spring-data-neo4j) 并將它們自動組合在一起。 Spring Data REST還支持將 Spring Data JPA , Spring Data Gemfire 和 Spring Data MongoDB 作為后端數據存儲,但是本指南涉及Neo4j。 ## 你需要什么 * 約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) ,您可以從頭開始并完成每個步驟,也可以繞過您已經熟悉的基本設置步驟。 無論哪種方式,您最終都可以使用代碼。 要 **從頭開始** ,請繼續至 [\[scratch\]](https://spring.io/guides/gs/accessing-neo4j-data-rest/#scratch) 。 要 **跳過基礎知識** ,請執行以下操作: * [下載](https://github.com/spring-guides/gs-accessing-neo4j-data-rest/archive/master.zip) 并解壓縮本指南的源存儲庫,或使用 對其進行克隆 [Git](https://spring.io/understanding/Git) : `git clone [https://github.com/spring-guides/gs-accessing-neo4j-data-rest.git](https://github.com/spring-guides/gs-accessing-neo4j-data-rest.git)` * 光盤進入 `gs-accessing-neo4j-data-rest/initial` * 跳至 [訪問Neo4j的權限](https://spring.io/guides/gs/accessing-neo4j-data-rest/#initial) 。 **完成后** ,您可以根據中的代碼檢查結果 `gs-accessing-neo4j-data-rest/complete`. ## 站起來Neo4j服務器 在構建此應用程序之前,您需要設置Neo4j服務器。 Neo4j有一個開源服務器,您可以免費安裝。 在安裝了Homebrew的Mac上,可以在終端窗口中鍵入以下內容: ~~~ $ brew install neo4j ~~~ 有關其他選項,請參見 [https://neo4j.com/download/community-edition/](https://neo4j.com/download/community-edition/) 安裝Neo4j后,可以通過運行以下命令以默認設置啟動它: ~~~ $ neo4j start ~~~ 您應該看到類似于以下內容的消息: ~~~ Starting Neo4j. Started neo4j (pid 96416). By default, it is available at http://localhost:7474/ There may be a short delay until the server is ready. See /usr/local/Cellar/neo4j/3.0.6/libexec/logs/neo4j.log for current status. ~~~ 默認情況下,Neo4j的用戶名和密碼為 `neo4j` 和 `neo4j`。 但是,它要求更改新的帳戶密碼。 為此,請運行以下命令: ~~~ $ curl -v -u neo4j:neo4j -X POST localhost:7474/user/neo4j/password -H "Content-type:application/json" -d "{\"password\":\"secret\"}" ~~~ 這會將密碼從 `neo4j` 到 `secret`(有些事情在生產中不做!)完成后,您應該準備運行本指南。 ## 從Spring Initializr開始 如果您使用Maven,請訪問 [Spring Initializr](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=accessing-neo4j-data-rest&name=accessing-neo4j-data-rest&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.accessing-neo4j-data-rest&dependencies=data-rest,data-neo4j) 以生成具有所需依賴項(Rest Repositories和Spring Data Neo4j)的新項目。 以下清單顯示了 `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.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>accessing-neo4j-data-rest</artifactId> <version>0.0.1-SNAPSHOT</version> <name>accessing-neo4j-data-rest</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.neo4j.test</groupId> <artifactId>neo4j-harness</artifactId> <version>4.2.2</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ~~~ 如果您使用Gradle,請訪問 [Spring Initializr](https://start.spring.io/#!type=gradle-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=accessing-neo4j-data-rest&name=accessing-neo4j-data-rest&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.accessing-neo4j-data-rest&dependencies=data-rest,data-neo4j) 以生成具有所需依賴項(Rest Repositories和Spring Data Neo4j)的新項目。 以下清單顯示了 `build.gradle`選擇Gradle時創建的文件: ~~~ plugins { id 'org.springframework.boot' version '2.4.3' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-neo4j' implementation 'org.springframework.boot:spring-boot-starter-data-rest' testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() } ~~~ ### 手動初始化(可選) 如果要手動初始化項目而不是使用前面顯示的鏈接,請按照以下步驟操作: 1. 導航到 [https://start.spring.io](https://start.spring.io) 。 該服務提取應用程序所需的所有依賴關系,并為您完成大部分設置。 2. 選擇Gradle或Maven以及您要使用的語言。 本指南假定您選擇了Java。 3. 單擊 **Dependencies,** 然后選擇 **Rest Repositories** 和 **Spring Data Neo4j** 。 4. 點擊 **生成** 。 5. 下載生成的ZIP文件,該文件是使用您的選擇配置的Web應用程序的存檔。 如果您的IDE集成了Spring Initializr,則可以從IDE中完成此過程。 ## 訪問Neo4j的權限 Neo4j Community Edition需要憑據才能訪問它。 您可以通過在以下位置設置屬性來配置憑據 `src/main/resources/application.properties`, 如下: ~~~ spring.neo4j.uri=bolt://localhost:7687 spring.data.neo4j.username=neo4j spring.data.neo4j.password=secret ~~~ 這包括默認的用戶名( `neo4j`)和新設置的密碼( `secret`)您之前設置的。 不要在源存儲庫中存儲真實憑證。 而是在您的運行時中通過使用 配置它們 Spring Boot的屬性overrides 。 ## 創建一個域對象 您需要創建一個新的域對象來呈現一個人,如下面的示例(在 `src/main/java/com/example/accessingneo4jdatarest/Person.java`)顯示: ~~~ package com.example.accessingneo4jdatarest; import org.springframework.data.neo4j.core.schema.Id; import org.springframework.data.neo4j.core.schema.Node; import org.springframework.data.neo4j.core.schema.GeneratedValue; @Node public class Person { @Id @GeneratedValue private Long id; private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } ~~~ 這 `Person`對象具有名字和姓氏。 還有一個ID對象,該對象被配置為自動生成,因此您無需這樣做。 ## 創建一個 `Person` 資料庫 接下來,您需要創建一個簡單的存儲庫,如以下示例(在 `src/main/java/com/example/accessingneo4jdatarest/PersonRepository.java`)顯示: ~~~ package com.example.accessingneo4jdatarest; import java.util.List; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource(collectionResourceRel = "people", path = "people") public interface PersonRepository extends PagingAndSortingRepository<Person, Long> { List<Person> findByLastName(@Param("name") String name); } ~~~ 該存儲庫是一個界面,可讓您執行涉及以下內容的各種操作 `Person`對象。 它通過擴展 [`PagingAndSortingRepositry`](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/PagingAndSortingRepository.html) Spring Data Commons中定義的接口。 在運行時,Spring Data REST自動創建此接口的實現。 然后使用 [`@RepositoryRestResource`](https://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/core/annotation/RepositoryRestResource.html) 注釋以指導Spring MVC在以下位置創建RESTful端點 `/people`. @RepositoryRestResource不需要導出存儲庫。 它僅用于更改導出詳細信息,例如使用 /people 而不是默認值 /persons. 在這里,您還定義了一個自定義查詢來檢索以下內容的列表 `Person` 基于的對象 `lastName`價值。 您可以在本指南的后面部分中看到如何調用它。 ## 查找應用程序類別 當您使用Spring Initializr創建項目時,它會創建一個應用程序類。 你可以在 `src/main/java/com/example/accessingneo4jdatarest/Application.java`。 請注意,Spring Initializr會串聯(并正確更改大小寫)程序包名稱并將其添加到 `Application`創建應用案例名稱。 在這種情況下,我們得到 `AccessingNeo4jDataRestApplication`,如以下清單所示: ~~~ package com.example.accessingneo4jdatarest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableTransactionManagement @EnableNeo4jRepositories @SpringBootApplication public class AccessingNeo4jDataRestApplication { public static void main(String[] args) { SpringApplication.run(AccessingNeo4jDataRestApplication.class, args); } } ~~~ 在此示例中,您無需對此應用程序類進行任何更改 `@SpringBootApplication` 是一個方便注釋,它添加了以下所有內容: * `@Configuration`:將類標記為應用程序上下文的Bean定義的源。 * `@EnableAutoConfiguration`:告訴Spring Boot根據類路徑設置,其他bean和各種屬性設置開始添加bean。 例如,如果 `spring-webmvc` 在類路徑上,此注釋將應用程序標記為Web應用程序并激活關鍵行為,例如設置 `DispatcherServlet`. * `@ComponentScan`:告訴Spring在服務器中尋找其他組件,配置和服務 `com/example` 包,讓它找到控制器。 這 `main()` 方法使用Spring Boot的 `SpringApplication.run()`啟動應用程序的方法。 您是否注意到沒有一行XML? 沒有 `web.xml`文件。 該Web應用程序是100%純Java,因此您無需處理任何管道或基礎結構。 這 `@EnableNeo4jRepositories`批注激活Spring Data Neo4j。 Spring Data Neo4j創建了具體的實現 `PersonRepository` 并使用Cypher查詢語言將其配置為與嵌入式Neo4j數據庫對話。 ### 建立可執行的JAR 您可以使用Gradle或Maven從命令行運行該應用程序。 您還可以構建一個包含所有必需的依賴項,類和資源的可執行JAR文件,然后運行該文件。 生成可執行jar使得在整個開發生命周期中,跨不同環境等等的情況下,都可以輕松地將服務作為應用程序進行發布,版本控制和部署。 如果您使用Gradle,則可以通過使用以下命令運行該應用程序 `./gradlew bootRun`。 或者,您可以通過使用以下命令構建JAR文件: `./gradlew build` 然后運行JAR文件,如下所示: ~~~ java -jar build/libs/gs-accessing-neo4j-data-rest-0.1.0.jar ~~~ 如果您使用Maven,則可以通過使用以下命令運行該應用程序 `./mvnw spring-boot:run`。 或者,您可以使用以下命令構建JAR文件: `./mvnw clean package` 然后運行JAR文件,如下所示: ~~~ java -jar target/gs-accessing-neo4j-data-rest-0.1.0.jar ~~~ 此處描述的步驟將創建可運行的JAR。 您還可以 構建經典的WAR文件 。 顯示日志記錄輸出。 該服務應在幾秒鐘內啟動并運行。 ## 測試應用 現在該應用程序正在運行,您可以對其進行測試。 您可以使用任何所需的REST客戶端。 以下示例使用名為\* nix的工具 `curl`. 首先,您要查看頂級服務。 以下示例(帶有輸出)顯示了如何執行此操作: ~~~ $ curl http://localhost:8080 { "_links" : { "people" : { "href" : "http://localhost:8080/people{?page,size,sort}", "templated" : true } } } ~~~ 在這里,您可以初步了解該服務器所提供的功能。 有一個 `people`鏈接位于 [http:// localhost:8080 / people](http://localhost:8080/people) 。 它具有一些選項,例如 `?page`, `?size`, 和 `?sort`. Spring Data REST使用 HAL格式 進行JSON輸出。 它非常靈活,并提供了一種便捷的方式來提供與所提供數據相鄰的鏈接。 ~~~ $ curl http://localhost:8080/people { "_links" : { "self" : { "href" : "http://localhost:8080/people{?page,size,sort}", "templated" : true }, "search" : { "href" : "http://localhost:8080/people/search" } }, "page" : { "size" : 20, "totalElements" : 0, "totalPages" : 0, "number" : 0 } } ~~~ 當前沒有任何元素,因此也沒有頁面,因此是時候創建一個新的頁面了 `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/0 Content-Length: 0 Date: Wed, 26 Feb 2014 20:26:55 GMT ~~~ * `-i`確保您可以看到包含標題的響應消息。 新創建的URI `Person` 顯示 * `-X POST` 發出信號 `POST` 用于創建新條目 * `-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" : { "self" : { "href" : "http://localhost:8080/people{?page,size,sort}", "templated" : true }, "search" : { "href" : "http://localhost:8080/people/search" } }, "_embedded" : { "people" : [ { "firstName" : "Frodo", "lastName" : "Baggins", "_links" : { "self" : { "href" : "http://localhost:8080/people/0" } } } ] }, "page" : { "size" : 20, "totalElements" : 1, "totalPages" : 1, "number" : 0 } } ~~~ 這 `people`對象包含帶有Frodo的列表。 請注意,它如何包含一個 `self`關聯。 Spring Data REST還使用 [Evo Inflector](https://www.atteo.org/2011/12/12/Evo-Inflector.html) 庫來對實體名稱進行復數以進行分組。 您可以通過運行以下命令(及其輸出顯示)直接查詢單個記錄: ~~~ $ curl http://localhost:8080/people/0 { "firstName" : "Frodo", "lastName" : "Baggins", "_links" : { "self" : { "href" : "http://localhost:8080/people/0" } } } ~~~ 這似乎純粹是基于Web的,但是在后臺有一個嵌入式Neo4j圖形數據庫。 在生產中,您可能會連接到獨立的Neo4j服務器。 在本指南中,只有一個域對象。 在域對象相互關聯的更復雜的系統中,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" : { "people" : [ { "firstName" : "Frodo", "lastName" : "Baggins", "_links" : { "self" : { "href" : "http://localhost:8080/people/0" }, "person" : { "href" : "http://localhost:8080/people/0" } } } ] }, "_links" : { "self" : { "href" : "http://localhost:8080/people/search/findByLastName?name=Baggins" } } } ~~~ 因為您將其定義為返回 `List<Person>`在代碼中,它返回所有結果。 如果您已將其定義為僅返回 `Person`,它將選擇其中之一 `Person`要返回的對象。 由于這可能是不可預測的,因此對于可能返回多個條目的查詢,您可能不想這樣做。 您也可以發出 `PUT`, `PATCH`, 和 `DELETE`REST調用以替換,更新或刪除現有記錄。 以下示例(及其輸出顯示)顯示了 `PUT` 稱呼: ~~~ $ curl -X PUT -H "Content-Type:application/json" -d '{ "firstName": "Bilbo", "lastName": "Baggins" }' http://localhost:8080/people/0 $ curl http://localhost:8080/people/0 { "firstName" : "Bilbo", "lastName" : "Baggins", "_links" : { "self" : { "href" : "http://localhost:8080/people/0" } } } ~~~ 以下示例(及其輸出顯示)顯示了 `PATCH` 稱呼: ~~~ $ curl -X PATCH -H "Content-Type:application/json" -d '{ "firstName": "Bilbo Jr." }' http://localhost:8080/people/0 $ curl http://localhost:8080/people/0 { "firstName" : "Bilbo Jr.", "lastName" : "Baggins", "_links" : { "self" : { "href" : "http://localhost:8080/people/0" } } } ~~~ PUT替換整個記錄。 未提供的字段將替換為 null. PATCH 可用于更新項的子集。 您還可以刪除記錄,如以下示例(及其輸出所示)所示: ~~~ $ curl -X DELETE http://localhost:8080/people/0 $ curl http://localhost:8080/people { "_links" : { "self" : { "href" : "http://localhost:8080/people{?page,size,sort}", "templated" : true }, "search" : { "href" : "http://localhost:8080/people/search" } }, "page" : { "size" : 20, "totalElements" : 0, "totalPages" : 0, "number" : 0 } } ~~~ 此超媒體驅動的界面的一個方便方面是如何使用curl(或您喜歡的任何REST客戶端)發現所有RESTful端點。 您無需與客戶交換正式的合同或接口文檔。 ## 概括 恭喜你! 您剛剛開發了一個具有基于 的應用程序 [超媒體的](https://spring.io/guides/gs/rest-hateoas) RESTful前端和基于Neo4j的后端 。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看