Spring Boot:Data Rest Service

在文章RESTful by Spring Boot with MySQL通过在Controller中引入BookRepository来对外提供REST API。Spring Boot还可以通过spring-boot-starter-data-rest来对外提供REST API,可以免于编写对应的Controller,且具备分页和排序的功能。

实践

  • 在pom文件中添加依赖项
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
  • 在包com.test.bookpub.repository下创建AuthorRepository接口,该接口继承自PagingAndSortingRepository,并用@RepositoryRestResource注解修饰。代码如下:
1
2
3
4
5
6
7
8
9
10
package com.test.bookpub.repository;

import com.test.bookpub.domain.Author;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource
public interface AuthorRepository
extends PagingAndSortingRepository<Author, Long> {
}
  • 可以看出,实际编写的代码很少,同样套路,为Publisher和Reviewer也添加类似的接口。
    PublisherRepository的代码如下:
1
2
3
4
5
6
7
8
9
10
package com.test.bookpub.repository;

import com.test.bookpub.domain.Publisher;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResourcepublic
interface PublisherRepository
extends PagingAndSortingRepository<Publisher, Long> {
}

ReviewerRepository的代码如下:

1
2
3
4
5
6
7
8
package com.test.bookpub.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.test.bookpub.domain.Publisher.Reviewer;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResourcepublic interface ReviewerRepoistory
extends PagingAndSortingRepository<Reviewer, Long> {
}
  • 启动应用程序,并访问http://localhost:8080/authors,将会得到如下结果
    访问author信息

分析

显然,通过继承PagingAndSortingRepository接口,比直接写Controller能提供更多的功能:分页查询和对查询结果排序。

@RepositoryRestResource注解让编程人员可以直接通过repository提供数据接口,在这个“前端负责V和C,后端负责提供数据”的时代,非常方便;并且,可以通过给该注解传入参数来改变URL。

只要在项目的classpath中包含spring-boot-starter-data-rest,同时就包含了spring-hateoas库支持,这个库可以提供ALPS元数据——一种数据格式,可以用于描述应用级别的API语义。

个人看法,在大型项目中不太会使用到这个特性,因为会有比较多的定制需求,以及需要对查询条件做一些必要的检查,确保不会引入安全问题。

参考资料

  1. ALPS主页
  2. Spring Data Rest + Spring Secuirty