文章目錄
  1. 1. create Entity
    1. 1.1. create Person Entity
    2. 1.2. create Person Repository
      1. 1.2.1. 一个小Bug
    3. 1.3. make the application executable
  2. 2. Test the application
    1. 2.1. show what happend
    2. 2.2. 保存数据 POST
    3. 2.3. 查看保存数据
    4. 2.4. 更新数据 PUT
    5. 2.5. 删除数据 DELETE
  3. 3. Code download
    1. 3.1. Download ZIP
  4. 4. 参考文章

对于熟悉MVC思想的人来时(前后台完全分离的情况下),如果前台向后台发送ajax请求的话,我们惯性的思想来说都是要在C层接受转发数据,将数据交给Service层处理这里我们将M层分为—Entity(元数据)与Service(处理实体数据的方法)。但是Spring中对于crud操作却是基于REST思想的 —URL定位资源,用HTTP动词(GET,POST,DELETE,DETC)描述操作。下面就让我们来看一下在spring怎么实现吧!

create Entity

create Person Entity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.mengyunzhi.Repository;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
class Person {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String lastName;
private String firstName;
public Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", firstName='" + firstName + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}

id的两个注解分别对应了声明此字段是主健和自动生成的策略,我们可以不用管这些问题

create Person Repository

1
2
3
4
5
6
7
8
package com.mengyunzhi.Repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
interface PersonRepository extends CrudRepository<Person, Long> {
}

当程序运行的时候,然后它使用@RepositoryRestResource注解来引导Spring MVC创建 "/people" RESTful endpoints。

一个小Bug

我用的是ideaIDE,但我初始化一个项目的时候就会发现无法引入@RepositoryRestResource这个注解,如图所示

google关键字: @RepositoryRestResource can't find in spring
然后我们参考Stack Overflow中这篇文章解决Unable to import Spring RepositoryRestResource
在项目pom.xml中加$

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>

重新导入依赖即可

make the application executable

运行程序

Test the application

show what happend

现在程序已经开始运行了,我们可以用postman发送请求,也可以自己用js代码写ajax请求,但是在这里我们将使用 *nix tool curl

我们在终端(Linux)或者git Bash(Windows)中输入

1
$ curl http://localhost:8080

然后就会发现spring已经为我们写好了url:

1
2
3
4
5
6
7
8
9
10
{
"_links" : {
"people" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile"
}
}
}

在这里我们看到了服务器能提供给我们什么。我们可以看到一个people link :
http://localhost:8080/people

然后让我们在终端输入url http://localhost:8080/people" 看看会有什么结果

1
$ curl http://localhost:8080/people

result:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"_embedded" : {
"people" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile/people"
}
}
}

保存数据 POST

我们发现数据库还是空的,一起让我们来添加一条数据吧!

1
$ curl -i -X POST -H "Content-Type:application/json" -d '{ "lastName" : "Frodo", "firstName" : "Baggins" }' http://localhost:8080/people

让我们来解释一下这都是什么意思吧!

  • - i 使我们能够看到响应的headers
  • - X POST 使用POST方法创建实体
  • -H "Content-Type:application/json" content type是json对象
  • -d '{ "lastName" : "Frodo", "firstName" : "Baggins" }' json数据格式的Person对象

然后让我们一起来查看服务器给我们返回了什么?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
HTTP/1.1 201
Location: http://localhost:8080/people/1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 28 May 2017 00:47:48 GMT
{
"lastName" : "Frodo",
"firstName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
},
"person" : {
"href" : "http://localhost:8080/people/1"
}
}
}

查看保存数据

然后让我们查询一下person数据表中到底存储了多少条记录呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ curl http://localhost:8080/people
{
"_embedded" : {
"people" : [ {
"lastName" : "Frodo",
"firstName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
},
"person" : {
"href" : "http://localhost:8080/people/1"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile/people"
}
}
}

然后我们想要查询某一条记录呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
$ curl http://localhost:8080/people/1
{
"lastName" : "Frodo",
"firstName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
},
"person" : {
"href" : "http://localhost:8080/people/1"
}
}
}

更新数据 PUT

让我们来将刚才保存数据的firstName改为Gao试一下吧!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
curl -i -X PUT -H "Content-Type:application/json" -d '{ "lastName" : "Gao", "firstName" : "Baggins" }' http://localhost:8080/people/1
HTTP/1.1 200
Location: http://localhost:8080/people/1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 28 May 2017 01:05:09 GMT
{
"lastName" : "Gao",
"firstName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/1"
},
"person" : {
"href" : "http://localhost:8080/people/1"
}
}
}

是的,现在这条记录已经被我们更新啦!

删除数据 DELETE

让我们把唯一的一条数据删除吧!

1
$ curl -X DELETE http://localhost:8080/people/1

让我们来查看一下吧!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ curl http://localhost:8080/people
{
"_embedded" : {
"people" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile/people"
}
}
}

是的,记录被我们删除了。

Code download

Download ZIP

参考文章

Accessing JPA Data with REST Spring Guides
Representational State Transfer (REST)
REST 架构该怎么生动地理解? 知乎
使用 Spring Data JPA 简化 JPA 开发
Linux curl命令详解 Linux系统教程
Spring Boot之@RepositoryRestResource注解入门使用教程 简书
Unable to import Spring RepositoryRestResource Stack Overflow

文章目錄
  1. 1. create Entity
    1. 1.1. create Person Entity
    2. 1.2. create Person Repository
      1. 1.2.1. 一个小Bug
    3. 1.3. make the application executable
  2. 2. Test the application
    1. 2.1. show what happend
    2. 2.2. 保存数据 POST
    3. 2.3. 查看保存数据
    4. 2.4. 更新数据 PUT
    5. 2.5. 删除数据 DELETE
  3. 3. Code download
    1. 3.1. Download ZIP
  4. 4. 参考文章