mvc可通过此配置方式开启
@Import(RepositoryRestMvcConfiguration.class)
Spring-Boot无须任何操作,引入包后默认自动配置(数据响应格式为application/hal+json)
spring.data.rest.base-path= /api/${version} # rest的基础路径(默认"") 路径应该加上一个版本号,避免版本更新导致旧服务失效
spring.data.rest.default-page-size= # 分页数量(默认20)
spring.data.rest.enable-enum-translation= # 启用通过Spring数据REST缺省资源束枚举值转换。*将使用完全合格的枚举名作为关键字。(默认false)
spring.data.rest.limit-param-name= # 分页的key名称(默认size)
spring.data.rest.max-page-size= # 最大分页数量(默认1000)
spring.data.rest.page-param-name= # 页码的key名称(默认page)
spring.data.rest.return-body-on-create= # 创建实体后是否返回(默认null)
spring.data.rest.return-body-on-update= # 更新实体后是否返回(默认null)
spring.data.rest.sort-param-name= # 排序的key名称(默认sort)
GET 获取,查找
POST 新增创建
PUT 更新
PATCH 部分更新
DELETE 删除
- 状态码&错误处理
Code HTTP Operation Body Contents Description
102 Processing GET, POST, PUT, DELETE, PATCH 处理状态的信息 当前请求正在处理
200 Ok GET, PUT 资源 操作成功
201 Created POST, PUT 资源, 元数据 对象创建成功
202 Accepted POST, PUT, DELETE, PATCH 处理信息 请求已经被接受
204 No Content DELETE, PUT, PATCH N/A 操作已经执行成功,但是没有返回数据
301 Moved Permanently GET link 资源已被移除
303 See Other GET link 重定向
304 Not Modified GET N/A 资源没有被修改
400 Bad Request GET, POST, PUT, DELETE, PATCH 错误提示 参数列表错误(缺少,格式不匹配)
401 Unauthorized GET, POST, PUT, DELETE, PATCH 错误提示 未授权
403 Forbidden GET, POST, PUT, DELETE, PATCH 错误提示 访问受限,授权过期
404 Not Found GET, POST, PUT, DELETE, PATCH 错误提示 资源,服务未找到
405 Method Not Allowed GET, POST, PUT, DELETE, PATCH 错误提示 不允许的http方法
406 Not Acceptable GET, POST, PUT, DELETE, PATCH 错误提示 媒体内容不符合要求
408 Request Timeout GET, POST, PUT, DELETE, PATCH 错误提示 请求超时
409 Conflict GET, POST, PUT 错误提示 资源冲突,重复的资源
415 Unsupported Media Type GET, POST, PUT, DELETE, PATCH 错误提示 不支持的数据(媒体)类型
422 Unprocessable Entity GET, POST, PUT, PATCH 错误提示 请求格式正确,但是由于含有语义错误,无法响应。
423 Locked GET, POST, PUT, DELETE, PATCH 错误提示 当前资源被锁定
429 Too Many Requests GET, POST, PUT, DELETE, PATCH 错误提示 请求过多被限制
500 Internal Server Error GET, POST, PUT, DELETE, PATCH 错误提示 系统内部错误
501 Not Implemented GET, POST, PUT, DELETE, PATCH 错误提示 接口未实现
- 数据获取
${ctx}/api/roles?page=0&size=2&sort=rolename,desc
表示获取每页2条数据,获取第一页并按照rolename的desc进行排序
{
"_embedded" : {
"roles" : [ {
"available" : "1",
"desc" : "22",
"rolename" : "测试",
"_links" : {
"self" : {//当前节点本身的链接
"href" : "http://127.0.0.1:8099/AutoDeploy/api/roles/1"
},
"role" : {
"href" : "http://127.0.0.1:8099/AutoDeploy/api/roles/1"
},
"resources" : {//资源内部节点链接(0~N个),组合资源要避免路径嵌套(个人想法:使用REST提供的服务,尽量精简,不需要嵌套额外数据)
"href" : "http://127.0.0.1:8099/AutoDeploy/api/roles/1/resources"
}
}
},
...此处为更多节点信息
]
},
"_links" : {
"first" : {//首页链接
"href" : "http://127.0.0.1:8099/AutoDeploy/api/roles?page=0&size=2"
},
"self" : {//本身资源链接
"href" : "http://127.0.0.1:8099/AutoDeploy/api/roles"
},
"next" : {//下一页链接
"href" : "http://127.0.0.1:8099/AutoDeploy/api/roles?page=1&size=2"
},
"last" : {//最后一页链接
"href" : "http://127.0.0.1:8099/AutoDeploy/api/roles?page=2&size=2"
},
"profile" : {//聚合资源链接
"href" : "http://127.0.0.1:8099/AutoDeploy/api/profile/roles"
},
"search" : {//查询条件链接
"href" : "http://127.0.0.1:8099/AutoDeploy/api/roles/search"
}
},
"page" : {
"size" : 2,//分页数量
"totalElements" : 6,//总记录数
"totalPages" : 3,//总页数
"number" : 0 //当前页数()
}
}
${ctx}/api/roles/search/findByAvailable?available=1 //调用某个方法传入参数进行查询 available 必须在方法参数里使用@Param("available")来标识参数名
${ctx}/api/roles/1 //根据主键进行查询
- 数据插入
POST请求 ${ctx}/api/roles
新增body-raw(不是单纯的表单数据)
{"available":"1","desc":"99","rolename":"rest测试3"}
发送格式为application/json(不用使用其它格式,不然无法解析,application/xml没测试,构建xml麻烦)
响应结果:
{
"available": "1",
"desc": "99",
"rolename": "rest测试3",
"_links": {
"self": {
"href": "http://127.0.0.1:8099/AutoDeploy/api/roles/9"
},
"role": {
"href": "http://127.0.0.1:8099/AutoDeploy/api/roles/9"
},
"resources": {
"href": "http://127.0.0.1:8099/AutoDeploy/api/roles/9/resources"
}
}
}
表示插入成功,可以看到返回的主键是9
- 数据更新
PUT请求 ${ctx}/api/roles/9
body-raw json格式参数
{"available":"0","desc":"33","rolename":"rest测试更新"}
成功时返回更新后的数据(PUT对应完整更新)
- PATCH请求
{"rolename":"rest测试更新666"}
可以更新部分数据(使用PUT将出现错误)
数据删除
DELETE请求 ${ctx}/api/roles/9
删除主键id为9的数据
删除成功没有任何响应
如果对应数据不存在,返回
{
"timestamp": 1470973132316,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.data.rest.webmvc.ResourceNotFoundException",
"message": "Resource not found!",
"path": "/AutoDeploy/api/roles/9"
}
- 自定义REST路径
@RepositoryRestResource(path="aaa")
public interface RoleRepository extends JpaRepository<Role,String>,JpaSpecificationExecutor<Role>{
@RestResource(path="bbb")
List<Role> findByAvailable(@Param("available") String available);
}
经过上述设置,访问路径变成 ctx/api/aaa/search/bbb?available=1等价于{ctx}/api/roles/search/bbb?available=1
rest的默认访问路径为实体类的名称+s 即Role.java 为roles Project.java 为projects
- 数据校验
@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application extends RepositoryRestMvcConfiguration {
@Override
protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
validatingListener.addValidator("beforeCreate", 自定义的校验类);
//可选的事件类型(参考ValidatingRepositoryEventListener的源码)
//event {"beforeCreate","afterCreate","beforeSave","afterSave","beforeLinkSave","afterLinkSave","beforeDelete","afterDelete"}
}
}