spring data rest 入门(二)CRUD基本使用

第一步将环境搭好以后开始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
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值