第一步将环境搭好以后开始CRUD操作。
使用工具postman,eclipse,navicat。
在配置文件上增加了一个/api的basepath。
数据库:
一、post新增
使用postman工具进行相关操作:
遇到的错误:No suitable HttpMessageConverter found to read request body into object of type class test.entity.User from request with content type of application/x-www-form-urlencoded;charset=UTF-8!
解决:将content-type设置为application/json
成功提交截图:
二、更新
post方式更新:
put方式更新:
三、delete删除
四、get查询
1)查询全部数据:http://localhost:8081/api/user
返回内容:
{
"_embedded": {
"users": [
{
"name": "张三",
"password": "11345",
"age": 16,
"sex": "男",
"datatime": "2020-02-03 01:52:14",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/1"
},
"user": {
"href": "http://localhost:8081/api/user/1"
}
}
},
{
"name": "李四",
"password": "222",
"age": 25,
"sex": "男",
"datatime": "2020-02-20 01:52:26",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/2"
},
"user": {
"href": "http://localhost:8081/api/user/2"
}
}
},
{
"name": "王五",
"password": "4223",
"age": 32,
"sex": "女",
"datatime": "2020-02-03 06:03:45",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/3"
},
"user": {
"href": "http://localhost:8081/api/user/3"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8081/api/user{?page,size,sort}",
"templated": true
},
"profile": {
"href": "http://localhost:8081/api/profile/user"
}
},
"page": {
"size": 20,
"totalElements": 3,
"totalPages": 1,
"number": 0
}
}
2)主键查询
http://localhost:8081/api/user/2
返回内容:
{
"name": "李四",
"password": "222",
"age": 25,
"sex": "男",
"datatime": "2020-02-20 01:52:26",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/2"
},
"user": {
"href": "http://localhost:8081/api/user/2"
}
}
}
3)分页排序查询
http://localhost:8081/api/user?page=0&size=2&sort=age,desc
返回内容:
{
"_embedded": {
"users": [
{
"name": "王五",
"password": "4223",
"age": 32,
"sex": "女",
"datatime": "2020-02-03 06:03:45",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/3"
},
"user": {
"href": "http://localhost:8081/api/user/3"
}
}
},
{
"name": "李四",
"password": "222",
"age": 25,
"sex": "男",
"datatime": "2020-02-20 01:52:26",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/2"
},
"user": {
"href": "http://localhost:8081/api/user/2"
}
}
}
]
},
"_links": {
"first": {
"href": "http://localhost:8081/api/user?page=0&size=2&sort=age,desc"
},
"self": {
"href": "http://localhost:8081/api/user"
},
"next": {
"href": "http://localhost:8081/api/user?page=1&size=2&sort=age,desc"
},
"last": {
"href": "http://localhost:8081/api/user?page=1&size=2&sort=age,desc"
},
"profile": {
"href": "http://localhost:8081/api/profile/user"
}
},
"page": {
"size": 2,
"totalElements": 3,
"totalPages": 2,
"number": 0
}
}