网站功能项目报价,WordPress文章多图分页,学校网站建设步骤过程,网站程序上传REST#xff08;Representational State Transfer#xff0c;表现形式状态转换#xff09;是一种访问网络资源的格式。传统的资源描述方式通常如下#xff1a;
http://localhost/user/getById?id1http://localhost/user/saveUser
而 REST 风格的描述则更简洁#xff1a…RESTRepresentational State Transfer表现形式状态转换是一种访问网络资源的格式。传统的资源描述方式通常如下
http://localhost/user/getById?id1http://localhost/user/saveUser
而 REST 风格的描述则更简洁
http://localhost/user/1http://localhost/user
采用 REST 风格的优势包括
隐藏资源访问的具体行为使得从 URL 无法判断对资源的操作类型简化书写提高可读性
在 REST 风格中通过行为动作来区分对资源的操作类型
url含义动作http://localhost/users查询全部用户信息GET查询http://localhost/users/1查询指定用户信息GET查询http://localhost/users添加用户信息POST新增/保存http://localhost/users修改用户信息PUT修改/更新http://localhost/users/1删除用户信息DELETE删除
上述行为是一种约定而非严格的规范因此称为 REST 风格而不是 REST 规范。在描述模块名称时通常使用复数形式即加 “s”以表示这一类资源而非单个资源例如users、books、accounts 等。 根据 REST 风格对资源进行访问的方式称为 RESTful 快速入门
REST 风格通常包含两个步骤
设定 HTTP 请求动作动词通过 RequestMapping 注解的 method 参数来指定。设定请求参数路径变量通过 PathVariable 注解来定义路径变量。
以下是使用 REST 风格的代码示例
Controller
public class UserController {RequestMapping(value /users, method RequestMethod.POST)ResponseBodypublic String save() {System.out.println(user save ...);return {module: user save};}RequestMapping(value /users/{id}, method RequestMethod.DELETE)ResponseBodypublic String delete(PathVariable Integer id) {System.out.println(user delete ... id);return {module: user delete};}RequestMapping(value /users, method RequestMethod.PUT)ResponseBodypublic String update(RequestBody User user) {System.out.println(user update ... user);return {module: user update};}RequestMapping(value /users/{id}, method RequestMethod.GET)ResponseBodypublic String getById(PathVariable Integer id) {System.out.println(user getById ... id);return {module: user getById};}RequestMapping(value /users, method RequestMethod.GET)ResponseBodypublic String getAll() {System.out.println(user getAll ...);return {module: user getAll};}
}RequestBody vs RequestParam vs PathVariable 区别 RequestParam用于接收 url 地址传参或表单传参RequestBody用于接收 JSON 数据PathVariable用于接收路径参数使用 {参数名称} 描述路径参数 应用 后期开发中发送请求参数超过 1 个时以 JSON 格式为主RequestBody 应用较广若发送非 JSON 格式数据选用 RequestParam 接收请求参数采用 RESTful 开发当参数数量较少例如1个可采用 PathVariable 接收请求路径变量通常用于传递 id 值
代码优化
从上面的 RESTful 风格代码中可以看到一些冗余之处例如每个方法都使用了 ResponseBody 注解并且每个方法的 RequestMapping 都以 /users 为前缀。为了减少代码的重复性可以将 ResponseBody 和 RequestMapping(/users) 注解统一放置在类定义上并利用特定的动作注解替代方法中的 RequestMapping。
另外使用 RestController 注解可以将当前控制器类设置为 RESTful 风格这个注解等同于 Controller 和 ResponseBody 的组合。因此可以用 RestController 来代替这两个注解从而简化代码。
//Controller
//ResponseBody
RestController
RequestMapping(/users)
public class UserController {PostMappingpublic String save() {System.out.println(user save ...);return {module: user save};}DeleteMapping(/{id})public String delete(PathVariable Integer id) {System.out.println(user delete ... id);return {module: user delete};}PutMappingpublic String update(RequestBody User user) {System.out.println(user update ... user);return {module: user update};}GetMapping(/{id})public String getById(PathVariable Integer id) {System.out.println(user getById ... id);return {module: user getById};}GetMappingpublic String getAll() {System.out.println(user getAll ...);return {module: user getAll};}
}