增加Rest Repositories讓Spring數據直接升級為RestAPI

增加Rest Repositories讓Spring數據直接升級為RestAPI

透過Spring Data原本提供的功能,加上這個套件:

compile('org.springframework.boot:spring-boot-starter-data-rest')

然後把原本的@Repository換成@RepositoryRestResource

這樣我們的Book物件,一行程序都不用寫

就可以透過

POST /書籍

PUT / books / {bookid}

GET / books / {bookid}

DELETE / books / {bookid}

就不用一直重複去寫這些CRUD了

但是通常你會有需求不想讓所有API對外開放ex:DELETE / books / {bookid}

你可以進一步用這個註解開控制是否對外@RestResource(exported = false)

整個改完程序就會像這樣

的build.gradle

dependencies {compile('org.springframework.boot:spring-boot-starter-data-jpa')compile('org.springframework.boot:spring-boot-starter-data-rest')compile('org.springframework.boot:spring-boot-starter-web')runtime('com.h2database:h2')compileOnly('org.projectlombok:lombok')testCompile('org.springframework.boot:spring-boot-starter-test')}

BookRepository.java

我把CRUD的Override都列出來,

刪除功能我們控制RestResource(exported = false)不對外開放

package com.example.book;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.rest.core.annotation.RepositoryRestResource;import org.springframework.data.rest.core.annotation.RestResource;@RepositoryRestResourcepublic interface BookRepository extends JpaRepository { // Prevents GET /books/:id @Override Book findOne(Integer id); // Prevents GET /books @Override Page findAll(Pageable pageable); // Prevents POST /books and PATCH /books/:id @Override Book save(Book s); // Prevents DELETE /books/:id @Override @RestResource(exported = false) void delete(Book t);}

程序啟動後可以透過

curl -X GET http://localhost:8080

來知道這服務提供多少個API來操作

增加Rest Repositories讓Spring數據直接升級為RestAPI

查詢分頁

curl -X GET 'http://localhost:8080/books?page=0&size=2&sort=bookid,desc'
{ "_embedded": { "books": [ { "name": "天生變態:一個擁有變態大腦的天才科學家", "author": "詹姆斯‧法隆", "_links": { "self": { "href": "http://localhost:8080/books/12" }, "book": { "href": "http://localhost:8080/books/12" } } }, { "name": "魔法精油寶典:102種植物香氣的能量運用", "author": "史考特,", "_links": { "self": { "href": "http://localhost:8080/books/11" }, "book": { "href": "http://localhost:8080/books/11" } } } ] }, "_links": { "first": { "href": "http://localhost:8080/books?page=0&size=2&sort=bookid,desc" }, "self": { "href": "http://localhost:8080/books" }, "next": { "href": "http://localhost:8080/books?page=1&size=2&sort=bookid,desc" }, "last": { "href": "http://localhost:8080/books?page=5&size=2&sort=bookid,desc" }, "profile": { "href": "http://localhost:8080/profile/books" } }, "page": { "size": 2, "totalElements": 12, "totalPages": 6, "number": 0 }}

取得單一資料

curl -X GET http://localhost:8080/books/12
{ "name": "天生變態:一個擁有變態大腦的天生科學家", "author": "詹姆斯‧法隆", "_links": { "self": { "href": "http://localhost:8080/books/12" }, "book": { "href": "http://localhost:8080/books/12" } }}

然後我們試一下刪除是不是真的會被外表RestAPI給刪除

curl -I -X DELETE http://localhost:8080/books/12HTTP/1.1 405Allow: GET,HEAD,PUT,PATCH,OPTIONSContent-Length: 0Date: Tue, 12 Dec 2017 01:44:29 GMT

就可以很清楚看到405此操作已被拒絕了

你看CRUD都不用重複再開發,查詢也支援分頁

是不是大大節省我們重複寫一些基本操作的時間呢?

工作聰明不努力!

多用一些新的工具節省你的精力,把時間花在你的商業邏輯吧〜


分享到:


相關文章: