Spring Data REST(一):两行代码搞定RESTFul
一、环境搭建
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.4.3.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-restartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
dependency>
dependencies>
这里采用最简单的方式Spring Boot集成 Spring Data REST,并引入h2数据库作为测试数据库,使用Spring Data Jpa的默认配置为数据持久层,顺便提一句Spring Data Jpa的默认实现为Hibernate。
2. 创建实体类
@Entity
public class User {
/**
* 指定id为主键,并设置为自增长
*/
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@GenericGenerator(name = "increment", strategy = "increment")
private long id;
private String name;
private String password;
private int age;
private boolean sex;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
}
/**
* path="user": 映射路由
* User:实体类
* Long:主键
* @author white
*
*/
@RepositoryRestResource(path="user")
public interface UserRepository extends JpaRepository<User, Long>{
}
@org.springframework.boot.autoconfigure.SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
在Resource目录下创建application.properties
#测试数据库地址
spring.datasource.url=jdbc:h2:file:D:\\testdb
#ddl策略
spring.jpa.hibernate.ddl-auto=update
(注:如果你使用的不是h2数据库,在这里配置相应的数据库源和驱动即可)
到这里,一个简单的环境搭建完毕
二、实战测试
{
"_embedded": {
"users": [
{
"name": "white",
"password": "123123",
"age": 12,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/1" },
"user": {
"href": "http://127.0.0.1:8080/user/1" }
}
},
{
"name": "white yu",
"password": "123123",
"age": 13,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/2" },
"user": {
"href": "http://127.0.0.1:8080/user/2" }
}
},
{
"name": "white yu11",
"password": "123123",
"age": 14,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/3" },
"user": {
"href": "http://127.0.0.1:8080/user/3" }
}
},
{
"name": "白鱼",
"password": "123123",
"age": 15,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/4" },
"user": {
"href": "http://127.0.0.1:8080/user/4" }
}
},
{
"name": "小白鱼",
"password": "123123",
"age": 25,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/5" },
"user": {
"href": "http://127.0.0.1:8080/user/5" }
}
},
{
"name": "小白",
"password": "123123",
"age": 25,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/6" },
"user": {
"href": "http://127.0.0.1:8080/user/6" }
}
},
{
"name": "小 鱼 ",
"password": "123123",
"age": 25,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/7" },
"user": {
"href": "http://127.0.0.1:8080/user/7" }
}
}
]
},
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user"
},
"profile": {
"href": "http://127.0.0.1:8080/profile/user"
}
},
"page": {
"size": 20,
"totalElements": 7,
"totalPages": 1,
"number": 0
}
}
(2)主键查询
url: 127.0.0.1:8080/user/2
返回数据:
{
"name": "white yu",
"password": "123123",
"age": 13,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/2"
},
"user": {
"href": "http://127.0.0.1:8080/user/2"
}
}
}
(3)分页查询
url: 127.0.0.1:8080/user?page=1&size=3
返回数据:
{
"_embedded": {
"users": [
{
"name": "白鱼",
"password": "123123",
"age": 15,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/4" },
"user": {
"href": "http://127.0.0.1:8080/user/4" }
}
},
{
"name": "小白鱼",
"password": "123123",
"age": 25,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/5" },
"user": {
"href": "http://127.0.0.1:8080/user/5" }
}
},
{
"name": "小白",
"password": "123123",
"age": 25,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/6" },
"user": {
"href": "http://127.0.0.1:8080/user/6" }
}
}
]
},
"_links": {
"first": {
"href": "http://127.0.0.1:8080/user?page=0&size=3"
},
"prev": {
"href": "http://127.0.0.1:8080/user?page=0&size=3"
},
"self": {
"href": "http://127.0.0.1:8080/user"
},
"next": {
"href": "http://127.0.0.1:8080/user?page=2&size=3"
},
"last": {
"href": "http://127.0.0.1:8080/user?page=2&size=3"
},
"profile": {
"href": "http://127.0.0.1:8080/profile/user"
}
},
"page": {
"size": 3,
"totalElements": 7,
"totalPages": 3,
"number": 1
}
}
(4)分页+排序查询
url: 127.0.0.1:8080/user?page=1&size=3&sort=age,desc
返回数据
{
"_embedded": {
"users": [
{
"name": "白鱼",
"password": "123123",
"age": 15,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/4" },
"user": {
"href": "http://127.0.0.1:8080/user/4" }
}
},
{
"name": "white yu11",
"password": "123123",
"age": 14,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/3" },
"user": {
"href": "http://127.0.0.1:8080/user/3" }
}
},
{
"name": "white yu",
"password": "123123",
"age": 13,
"sex": false,
"_links": {
"self": {
"href": "http://127.0.0.1:8080/user/2" },
"user": {
"href": "http://127.0.0.1:8080/user/2" }
}
}
]
},
"_links": {
"first": {
"href": "http://127.0.0.1:8080/user?page=0&size=3&sort=age,desc"
},
"prev": {
"href": "http://127.0.0.1:8080/user?page=0&size=3&sort=age,desc"
},
"self": {
"href": "http://127.0.0.1:8080/user"
},
"next": {
"href": "http://127.0.0.1:8080/user?page=2&size=3&sort=age,desc"
},
"last": {
"href": "http://127.0.0.1:8080/user?page=2&size=3&sort=age,desc"
},
"profile": {
"href": "http://127.0.0.1:8080/profile/user"
}
},
"page": {
"size": 3,
"totalElements": 7,
"totalPages": 3,
"number": 1
}
}
测试完毕
最后再看一下项目结构
这里只是简单的编写了三个类,除去配置类和实体类,其实我们就写了两行代码,就实现了RESTFul风格的接口,这就是Spring Data REST的魅力所在。当然实际生成环境不会那么简单,还需要一些额外的处理,比如在返回的数据中,password这类字段是不应该返回的,DELETE 操作,我们实际需求不是硬删除,SAVE操作之前需要做相应的数据校验和数据格式的转换等等,这类定制化的操作,Spring Data REST也提供了相应的支持。在接下来的篇幅为会大家详细介绍,由于博主水平有限,如有不足,请斧正,谢谢