目录html
近日发现了spring-data-rest项目,因而建立这个spring-data-rest-glance来体验一下。java
本例使用springboot,并使用了 spring-data-rest 和 spring-data-jpagit
此两者结合:真的能够实现10分钟建立一个rest应用,下面开始演示spring-data-rest+spring-data-rest的魔力
本例假设你已经熟悉或者了解 springboot,spring-data-jpagithub
咱们先建立一个springboot项目,能够经过 start.spring.io 或者在idea里边new module --> spring Initializer 的方式。spring
建立好项目以后,咱们的依赖配置是这样的:数据库
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
本例使用的是h2数据库,因此加了h2数据库的依赖。json
咱们建立一个person
表,并建立person
的entity
和repository
,让repository
继承JpaRepository
。api
关键的来了:@RepositoryRestResource(collectionResourceRel = "person", path = "person")
咱们给repository
加上一个@RepositoryRestResource
注解,咱们来启动项目,看看此注解的魔力。浏览器
访问:htttp://localhost:8000/person
获得的结果以下:tomcat
{ "_embedded": { "person": [] }, "_links": { "self": { "href": "http://localhost:8000/person{?page,size,sort}", "templated": true }, "profile": { "href": "http://localhost:8000/profile/person" }, "search": { "href": "http://localhost:8000/person/search" } }, "page": { "size": 20, "totalElements": 0, "totalPages": 0, "number": 0 } }
咱们看到 person
节点并没有内容。同时请注意 _links 节点下的内容。咱们下面将会用到它。
咱们使用POST方式访问 http://localhost:8000/person
并提交以下 JSON 数据:
{"firstName": "tomcat", "lastName": "cat"}
如此,就完成了添加操做,你能够多添加几条数据试试。
咱们再次在浏览器中访问(GET) http://localhost:8000/person
。获得的结果中,JSON数据和第一步中同样,person节点中再也不是空的了。
[ { "firstName": "tomcat", "lastName": "cat", "_links": { "self": { "href": "http://localhost:8000/person/1" }, "person": { "href": "http://localhost:8000/person/1" } } } ]
咱们能够继续多添加几条数据,方便下面展现查询。在添加多条信息以后,若是想查看某个person的详情,例如:http://localhost:8000/person/7
{ "firstName": "李", "lastName": "四", "_links": { "self": { "href": "http://localhost:8000/person/7" }, "person": { "href": "http://localhost:8000/person/7" } } }
假设咱们须要根据用户名查询用户,咱们在PersonRepository
中添加一个方法findByLastName
.
托spring-data-jpa的福,咱们只须要写这样的一行代码,而后什么都不用作,spring-data-jpa会解析findByLastName
并应用到查询上。
List<Person> findByLastName(@Param("name") String name);
写好上面的代码以后,咱们重启项目,访问http://localhost:8000/person/search
结果以下:
{ "_links": { "findByLastName": { "href": "http://localhost:8000/person/search/findByLastName{?name}", "templated": true }, "findByFirstName": { "href": "http://localhost:8000/person/search/findByFirstName{?name}", "templated": true }, "self": { "href": "http://localhost:8000/person/search" } } }
咱们能够看到,这里已经列出了当前可用的search方法。咱们访问:http://localhost:8000/person/search/findByLastName?name=cat
{ "person": [ { "firstName": "tomcat", "lastName": "cat", "_links": { "self": { "href": "http://localhost:8000/person/1" }, "person": { "href": "http://localhost:8000/person/1" } } }, { "firstName": "tom", "lastName": "cat", "_links": { "self": { "href": "http://localhost:8000/person/2" }, "person": { "href": "http://localhost:8000/person/2" } } } ] }
咱们能够看到,这里经过findByLastName?name=cat
找到了两我的:tomcat cat 和 tom cat
为了演示分页,咱们先多添加几条用户数据。在第一步中展现的结果中,咱们能够看到这样的一行数据:
http://localhost:8000/person{?page,size,sort}
这提示了咱们分页的使用方法,咱们来访问http://localhost:8000/person?page=2&size=3
试试,即:访问第2页数据,页大小是3。
下面贴出 关键结果的节点:
{ "_embedded": { "person": [ { "firstName": "李", "lastName": "四", "_links": { "self": { "href": "http://localhost:8000/person/7" }, "person": { "href": "http://localhost:8000/person/7" } } }, { "firstName": "王", "lastName": "五", "_links": { "self": { "href": "http://localhost:8000/person/8" }, "person": { "href": "http://localhost:8000/person/8" } } } ] }, "page": { "size": 3, "totalElements": 8, "totalPages": 3, "number": 2 } }
确实查到了数据,结果对不对呢?根据上面的从上面的结果看出,咱们添加8条数据,页大小是3,因此:
总页数 = 3 第一页 3条数据 第二页 3条数据 第三页 2条数据
咱们访问的是,http://localhost:8000/person?page=2&size=3
page=2,可是实际上取的是2条数据——是第3页。那么页码实际上是从0开始的对吗?
咱们继续访问 http://localhost:8000/person?page=0&size=3
http://localhost:8000/person?page=1&size=3
能够发现,确实是如此,页码从0开始。any way
到目前为止,咱们只写了不多的代码,只写了DAO,可是却已经实现了增删改查resp api。咱们甚至连 controller都没有写,就访问了这么多的rest url。
咱们只经过@RepositoryRestResource(collectionResourceRel = "person", path = "person")
在 dao 中就可以把 /path路径暴露出来。
边一切都有了,这就是spring-data-rest的魔力。
固然能够了,上面咱们所访问的 /person/* 的地址,是从dao中经过 @RepositoryRestResource
注解暴露出去的。
那么如今咱们就手写一个controller,访问路径也叫/person,即:@RequestMapping("/person")
@Controller @RequestMapping("/person") public class PersonController { @RequestMapping("/hello") @ResponseBody public String hello(){ return " Hello,welcome to the normal controller! "; } }
咱们本身建立的controller访问路径也是,/person
还建立了一个自定义的 hello
方法,这个/person
和dao里边暴露的/person
能共存,并和谐相处吗?咱们访问看看:http://localhost:8000/person/hello
咱们在浏览器中能够看到:
Hello,welcome to the normal controller!
很完美,这里咱们能够得出,咱们能利用spring-data-rest + spring-data-jpa实现基本的增删改查api.
咱们只须要本身去写复杂的api就好了,简单的根本不用写,岂不是很快!
至此,咱们体验了一下 spring-data-rest。总有“刁民”说java开发很慢,代码太多了。多吗?很少啊,咱们这里使用spring-data-jpa
加上spring-data-rest,只写了不多的代码就实现了大部分基础的功能了。下次开发新项目,能够尝试使用 spring-data-rest加spring-data-jpa了。
推荐阅读
探索Java9 模块系统和反应流