逛 Spring 官网学习总结
@RepositoryRestResource
看到这个注解,之前一直没有用到过,所以想要自己试试效果,顺道做下总结。
不想要看我废话想要直接看官网的 → Accessing JPA Data with REST
不想要看我废话想看大佬的 → Spring Boot之@RepositoryRestResource注解入门使用教程
构建项目
用 Maven 构建项目,建一个 Person
实体类,再建一个 PersonRepository
接口,这个是关键。
1 | package hello; |
官网是这么介绍的:
1 | At runtime, Spring Data REST will create an implementation of this interface automatically. Then it will use the @RepositoryRestResource annotation to direct Spring MVC to create RESTful endpoints at /people. |
我们翻译过来就是:
1 | 在运行时,Spring Data REST将自动创建此接口的实现。然后它将会在使用 @RepositoryRestResource 注解的 Spring MVC 在 /people 创建 RESTful 端点。 |
点进 PagingAndSortingRepository
接口看:
1 | @NoRepositoryBean |
看到该接口还是继承了平时常使用的 CrudRepository
,这就说明那些基本的增删改查方法也都有。
为了方便,我还写了一个接口用来保存 Person
实体,为后续测试使用。
1 | @Service |
1 | @RestController |
很简单的一个映射。
到这为止,我们的项目就全构建好了。
嫌麻烦不想自己动手建项目的 → GitHub
测试功能
我这里为了方便,用的是 Postman
进行测试。
首先访问 http://localhost:8080
,返回的是:
1 | { |
再访问 http://localhost:8080/people
,返回的是:
1 | { |
因为里面没有数据,我们先塞几个数据进去,这时候就利用我们之前写的接口来操作。如下:
1 | 我们用 PUT 方式去请求 http://localhost:8080/person/save?firstName=aaa&lastName=bbb |
同样的方式我们再多塞几个。
塞完值之后我们接着去访问 http://localhost:8080/people
,这时候发现结果与之前的稍有不同了:
1 | { |
是的,我们刚塞的值都查出来了。
接着测试,访问 http://localhost:8080/people/1
,返回结果为:
1 | { |
是的,就是第一条数据被查出来了,同理 /2 /3 也是。
访问 http://localhost:8080/people/search
,返回结果为:
1 | { |
对的,findByLastName{?name}
就是我们在里面新增的那个接口,那就顺着这个意思咱们继续来。
访问 http://localhost:8080/people/search/findByLastName?name=folger
,返回结果为:
1 | { |
与我们猜测无二。接口写的返回是集合,所以就返回了一个集合。
之前还看到了这么一条 link "href": "http://localhost:8080/people{?page,size,sort}"
有经验的程序员一眼就肯定知道这些个参数都是干什么用的,是啊,就是分页用的。
我们访问这个 url : http://localhost:8080/people?page=0&size=4&sort=lastName
,返回的结果为:
1 | { |
从返回的数据我们可以看出: page 当然是页数,size 就是每一页的个数,sort 就是排序的字段,默认为 asc。
这一波测试下来发现这样封装之后之前需要一大堆代码才能实现的分页,这里只需要一个注解即可。妙啊妙不可言。