简介
在使用spring Boot构建RESTful风格API时,需要先了解什么是RESTful以及Spring中控制层的一些常用注解.
参考文章
-
RESTful架构理解 http://blog.csdn.NET/wang_jingj/article/details/51208746
-
RESTful架构实践 http://blog.csdn.net/wang_jingj/article/details/51208848
-
Spring MVC简介以及常用注解 http://blog.csdn.Net/wang_jingj/article/details/52005731
实例
API接口设计
-
GET /students # 获得students列表
-
POST /students # 新增一个student
-
PUT /students/1 # 更新一个student
-
DELETE /students/1 # 删除一个student
Student 实体类设计
省略gettersetter方法
StudentController
-
GET
-
POST
-
PUT
-
DELETE
说明
-
使用PUT时,接收参数不要使用@RequestBody,Spring MVC对PUT的支持仅限于类似GET的方式,即参数要紧接在URL后以http://www.veryjava.cn/sss?name=xxx&age=12的形式表示
-
不过可以使用特定的过滤器来实现接收form表单的请求: 地址:https://my.oschina.net/buwei/blog/191942
-
如果感觉使用RESTful实在困难,那还是使用`GET,POST`吧... 选择适合自己的,才是最好的.
代码
本文限于篇幅原因,不能展示所有的代码,如果想要完整的代码示例,请移步https://github.com/sunshineasbefore/veryjava.spring.boot/tree/master/restful
支持请点赞,不喜勿喷.
每天一点点,成长多一点.