什么是Restful?

Rest简介

  1. REST是英文representational state transfer(表象性状态转变)或者表述性状态转移。
  2. Rest是web服务的一种架构风格。
  3. 使用HTTP,URI,XML,JSON,HTML等广泛流行的标准和协议。
  4. 轻量级,跨平台,跨语言的架构设计。
  5. 它是一种设计风格,不是一种标准,是一种思想。

Rest架构的主要原则

  1. 网络上的所有事物都被抽象为资源
  2. 每个资源都有一个唯一的资源标识符
  3. 同一个资源具有多种表现形式(xml,json等)
  4. 对资源的各种操作不会改变资源标识符
  5. 所有的操作都是无状态的
  6. 符合REST原则的架构方式即可称为RESTful

什么是Restful

  1. 对应的中文是rest式的
  2. Restful web service是一种常见的rest的应用,是遵守了rest风格的web服务
  3. rest式的web服务是一种ROA(The Resource-Oriented Architecture)(面向资源的架构)

为什么会出现Restful

在Restful之前的操作

  1. http://127.0.0.1/user/query GET 根据用户id查询用户数据
  2. http://127.0.0.1/user/save POST 新增用户
  3. http://127.0.0.1/user/update POST 修改用户信息
  4. http://127.0.0.1/user/delete GET/POST 删除用户信息

Restful用法

  1. http://127.0.0.1/user/1 GET 根据用户id查询用户数据
  2. http://127.0.0.1/user POST 新增用户
  3. http://127.0.0.1/user PUT 修改用户信息
  4. http://127.0.0.1/user DELETE 删除用户信息

常用的HTTP动词有下面五个(括号里是对应的SQL命令)

  1. GET(SELECT):从服务器取出资源(一项或多项)。
  2. POST(CREATE):在服务器新建一个资源。
  3. PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源)。
  4. PATCH(UPDATE):在服务器更新资源(客户端提供改变的属性)。
  5. DELETE(DELETE):从服务器删除资源。

如何使用

http方法 资源操作 幂等 安全
GET SELECT
POST INSERT
PUT UPDATE
DELETE DELETE

幂等性:对同一REST接口的多次访问,得到的资源状态是相同的。

安全性:对该REST接口访问,不会使服务器端资源的状态发生改变。

##6.SpringMVC原生态的支持了REST风格的架构设计

###所涉及的注解

  1. @RequestMapping

  2. @PathVariable

  3. @ResponseBody

    package cn.itcast.mybatis.controller;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;

    import cn.itcast.mybatis.pojo.User;
    import cn.itcast.mybatis.service.NewUserService;

    @RequestMapping("restful/user")
    @Controller
    public class RestUserController {

    复制代码
     @Autowired
     private NewUserService newUserService;
    
     /**
      * 根据用户id查询用户数据
      * 
      * @param id
      * @return
      */
     @RequestMapping(value = "{id}", method = RequestMethod.GET)
     @ResponseBody
     public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {
         try {
             User user = this.newUserService.queryUserById(id);
             if (null == user) {
                 // 资源不存在,响应404
                 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
             }
             // 200
             // return ResponseEntity.status(HttpStatus.OK).body(user);
             return ResponseEntity.ok(user);
         } catch (Exception e) {
             e.printStackTrace();
         }
         // 500
         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
     }
    
     /**
      * 新增用户
      * 
      * @param user
      * @return
      */
     @RequestMapping(method = RequestMethod.POST)
     public ResponseEntity<Void> saveUser(User user) {
         try {
             this.newUserService.saveUser(user);
             return ResponseEntity.status(HttpStatus.CREATED).build();
         } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         // 500
         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
     }
    
     /**
      * 更新用户资源
      * 
      * @param user
      * @return
      */
     @RequestMapping(method = RequestMethod.PUT)
     public ResponseEntity<Void> updateUser(User user) {
         try {
             this.newUserService.updateUser(user);
             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
         } catch (Exception e) {
             e.printStackTrace();
         }
         // 500
         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
     }
    
     /**
      * 删除用户资源
      * 
      * @param user
      * @return
      */
     @RequestMapping(method = RequestMethod.DELETE)
     public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {
         try {
             if (id.intValue() == 0) {
                 // 请求参数有误
                 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
             }
             this.newUserService.deleteUserById(id);
             // 204
             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
         } catch (Exception e) {
             e.printStackTrace();
         }
         // 500
         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
     }

    }

##7.HTTP相应状态码

code HTTP operation Body Contents Description
200 GET,PUT 资源 操作成功
201 POST 资源,元数据 对象创建成功
202 POST,PUT,DELETE,PATCH N/A 请求已经被接受
204 GET N/A 操作已经执行成功,但是没有返回数据
301 GET link 资源已被移除
303 GET link 重定向
304 GET N/A 资源没有被修改
400 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 参数列表错误(缺少,格式不匹配)
401 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 未授权
403 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 访问受限,授权过期
404 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 资源,服务未找到
405 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 不允许的http方法
409 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 资源冲突,或者资源被锁定
415 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 不支持的数据(媒体)类型
429 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 请求过多被限制
500 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 系统内部错误
501 GET,POST,PUT,DELETE,PATCH 错误提示(消息) 接口未实现

原文地址:https://blog.csdn.net/chenxiaochan/article/details/73716617

相关推荐
世纪摆渡人1 小时前
SpringBoot-SpringBoot源码解读
java·spring boot·后端
z人间防沉迷k5 小时前
后端开发概念
java·后端
caihuayuan55 小时前
Vue3响应式数据: 深入分析Ref与Reactive
java·大数据·spring boot·后端·课程设计
ALex_zry8 小时前
Go核心特性与并发编程
开发语言·后端·golang
Kookoos9 小时前
ABP VNext + Orleans:Actor 模型下的分布式状态管理最佳实践
分布式·后端·c#·.net·.netcore·abp vnext
PWRJOY9 小时前
Flask 会话管理:从原理到实战,深度解析 session 机制
后端·python·flask
述雾学java10 小时前
Spring Boot是什么?MybatisPlus常用注解,LambdaQueryWrapper常用方法
java·spring boot·后端
程序员buddha12 小时前
SpringBoot多环境配置文件切换
java·spring boot·后端
twj_one13 小时前
SpringBoot+ELK 搭建日志监控平台
spring boot·后端·elk
咖啡啡不加糖13 小时前
Sentinel原理与SpringBoot整合实战
spring boot·后端·sentinel