1、Overview
廢話不多說直接上代碼锋谐,在代碼中解釋如何入門使用遍尺。
2、Build with 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-accessing-data-rest</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
3涮拗、Create a domain object
目錄:
src/main/java/hello/Person.java
代碼:
package hello;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
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
有一個firstName和lastName乾戏,主鍵id
配置成自動生成迂苛。
4、Create a Person repository
創(chuàng)建一個簡單的repository類
package hello;
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);
}
PersonRepository
是一個繼承PagingAndSortingRepository
的interface鼓择,然后三幻,你就可以對Person
對象進行各種操作。
在程序運行的時候惯退,Spring Data REST將自動創(chuàng)建此接口的實現(xiàn)赌髓。然后,它將使用@RepositoryRestResource注解讓Spring MVC在/people
處創(chuàng)建RESTful入口點催跪。
在這里锁蠕,您還可以加入一個自定義查詢,傳入lastName
參數(shù)來檢索Person對象的列表懊蒸,稍后我會將介紹如何詳細使用荣倾。
5、Make the application executable
目錄:
src/main/java/hello/Application.java
代碼:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
這里就不需要我詳細解釋了吧骑丸?使用過SpringBoot的人基本知道了舌仍。
6、Test the application
運行這個Web程序通危,然后進入http://localhost:8080
會顯示如下信息:
$ curl http://localhost:8080
{
"_links" : {
"people" : {
"href" : "http://localhost:8080/people{?page,size,sort}",
"templated" : true
}
}
}
在這里你能看到服務(wù)器提供給你的基本信息铸豁,能看到剛才我們所定義的people
所在地址http://localhost:8080/people
,它還提供了一些選項:?page
,?size
,?sort
菊碟。
注意:Spring Data REST的JSON輸出格式使用的是HAL
格式节芥。
現(xiàn)在我們進入這個URL地址:http://localhost:8080/people
,看下服務(wù)器是如何輸出的逆害。
$ 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
}
}
由輸出可見头镊,數(shù)據(jù)庫中暫無任何Person
對象的信息,現(xiàn)在我們開始新建一個對象:
$ 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, 13 Dec 2016 14:26:55 GMT
-i 顯示請求頭信息
-X POST : 利用POST創(chuàng)建一個新的實體
-H "Content-Type:application/json" : 設(shè)置內(nèi)容類型, 讓應(yīng)用程序知道你要傳輸?shù)膬?nèi)容是JSON還是XML
-d '{ "firstName" : "Frodo", "lastName" : "Baggins" }' 一個數(shù)據(jù)包魄幕,要發(fā)送給應(yīng)用程序的數(shù)據(jù)相艇。
$ 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" : {
"persons" : [ {
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
} ]
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
persons
JSON數(shù)組現(xiàn)在就包含了所有Person
對象的列表。其中也包含了這條消息的self
連接地址纯陨,也就是你可以直接查詢單個記錄的詳細信息坛芽,現(xiàn)在就讓我們訪問一下看下結(jié)果。
$ curl http://localhost:8080/people/1
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
}
查找所有自定義查詢:
$ curl http://localhost:8080/people/search
{
"_links" : {
"findByLastName" : {
"href" : "http://localhost:8080/people/search/findByLastName{?name}",
"templated" : true
}
}
}
通過返回結(jié)果队丝,我們能看見之前定義的findByLastName
接口靡馁,給name
參數(shù)傳入一個值,就可以達到查詢功能机久。
$ curl http://localhost:8080/people/search/findByLastName?name=Baggins
{
"_embedded" : {
"persons" : [ {
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
}
}
} ]
}
}
因為我們之前在代碼里面定義的返回結(jié)果是一個List<Person>
類型臭墨,所以它將返回所有符合條件的結(jié)果。如果你在代碼里定義返回結(jié)果為Person
膘盖,程序會選擇一個Person對象進行返回胧弛。
除此之外尤误,我們還可以使用PUT
、PATCH
结缚、DELETE
請求方式损晤,來進行替換、更新或刪除現(xiàn)有記錄的操作
$ curl -X PUT -H "Content-Type:application/json" -d '{ "firstName": "Bilbo", "lastName": "Baggins" }' http://localhost:8080/people/1
$ curl http://localhost:8080/people/1
輸出內(nèi)容:
{
"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
輸出內(nèi)容:
{
"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" : {
"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
}
}