Restful风格

打印 上一主题 下一主题

主题 870|帖子 870|积分 2610

Restful

1.REST架构的主要原则

1.1 对网络上所有的资源都有一个资源标志符

1.2 对资源的操作不会改变标识符

1.3 同一资源有多种表现形式(xml、json)、

1.4 所有操作都是无状态的(Stateless)

符合上述REST原则的架构方式称为Restful
2.URI和URL区别
  1. URI:http://example.com/users/
复制代码
  1. URL:http://example.com/users/{user} (one for each user)
复制代码
2.1.什么是无状态性

​        使得客户端和服务器端不必保存对方的详细信息,服务器只需要处理当前的请求,不需了解请求的历史。可以更容易的释放资源,让服务器利用Pool(连接池)技术来提高稳定性和性能。
3.Restful操作

RESTful是一种常见的REST应用,是遵循REST风格的web服务,REST式的web服务是一种ROA(面向资源的架构)。更加安全!!
http方法资源操作幂等安全GETSELECT是是POSTINSERT否否PUTUPDATE是否DELETEDELETE是否注:幂等性:对同一REST接口的多次访问,得到的资源状态是相同的。
​                安全性:对该REST接口访问,不会使服务器端资源的状态发生改变。
3.1接口示例

传统URL请求格式:
http:/127.0.0.1/test/query/2    http:/127.0.0.1/test/query?id=1  GET 根据用户id查询用户数据
http:/127.0.0.1/test/save   POST 新增用户
http:/127.0.0.1/test/update   POST 修改用户信息
http:/127.0.0.1/test/delete   GET/POST 删除用户信息
RESTful请求格式:
http:/127.0.0.1/test/1   GET 根据用户id查询用户数据
http:/127.0.0.1/test     POST 新增用户
http:/127.0.0.1/test   PUT 修改用户信息
http:/127.0.0.1/test/1    DELETE 删除用户信息
4.Http状态码

4.1一般情况:

200:请求响应成功 200
3xx:请求重定向 重定向:你重新到我给你新位置去;
4xx:找不到资源 404 资源不存在;
5xx:服务器代码错误 500 502:网关错误
4.2具体情况:
  1. HttpStatus = {  
  2.         //Informational 1xx  信息
  3.         '100' : 'Continue',  //继续
  4.         '101' : 'Switching Protocols',  //交换协议
  5.         //Successful 2xx  成功
  6.         '200' : 'OK',  //OK
  7.         '201' : 'Created',  //创建
  8.         '202' : 'Accepted',  //已接受
  9.         '203' : 'Non-Authoritative Information',  //非权威信息
  10.         '204' : 'No Content',  //成功,但没有内容
  11.         '205' : 'Reset Content',  //重置内容
  12.         '206' : 'Partial Content',  //部分内容
  13.         //Redirection 3xx  重定向
  14.         '300' : 'Multiple Choices',  //多种选择
  15.         '301' : 'Moved Permanently',  //永久移动
  16.         '302' : 'Found',  //找到
  17.         '303' : 'See Other',  //参见其他
  18.         '304' : 'Not Modified',  //未修改
  19.         '305' : 'Use Proxy',  //使用代理
  20.         '306' : 'Unused',  //未使用
  21.         '307' : 'Temporary Redirect',  //暂时重定向
  22.         //Client Error 4xx  客户端错误
  23.         '400' : 'Bad Request',  //错误的请求
  24.         '401' : 'Unauthorized',  //未经授权
  25.         '402' : 'Payment Required',  //付费请求
  26.         '403' : 'Forbidden',  //禁止
  27.         '404' : 'Not Found',  //没有找到
  28.         '405' : 'Method Not Allowed',  //方法不允许
  29.         '406' : 'Not Acceptable',  //不可接受
  30.         '407' : 'Proxy Authentication Required',  //需要代理身份验证
  31.         '408' : 'Request Timeout',  //请求超时
  32.         '409' : 'Conflict',  //指令冲突
  33.         '410' : 'Gone',  //文档永久地离开了指定的位置
  34.         '411' : 'Length Required',  //需要Content-Length头请求
  35.         '412' : 'Precondition Failed',  //前提条件失败
  36.         '413' : 'Request Entity Too Large',  //请求实体太大
  37.         '414' : 'Request-URI Too Long',  //请求URI太长
  38.         '415' : 'Unsupported Media Type',  //不支持的媒体类型
  39.         '416' : 'Requested Range Not Satisfiable',  //请求的范围不可满足
  40.         '417' : 'Expectation Failed',  //期望失败
  41.         //Server Error 5xx  服务器错误
  42.         '500' : 'Internal Server Error',  //内部服务器错误
  43.         '501' : 'Not Implemented',  //未实现
  44.         '502' : 'Bad Gateway',  //错误的网关
  45.         '503' : 'Service Unavailable',  //服务不可用
  46.         '504' : 'Gateway Timeout',  //网关超时
  47.         '505' : 'HTTP Version Not Supported'  //HTTP版本不支持
  48. };  
复制代码
5.测试
  1. @Controller
  2. public class RestFulController {
  3.     //映射访问路径
  4.     @RequestMapping("/commit/{p1}/{p2}")
  5.     //在SpringMVC中可以使用 @PathVariable,让方法参数的值对应绑定到一个URI变量上
  6.     public ModelAndView index(@PathVariable int p1, @PathVariable int p2, ModelAndView mv){
  7.         int result = p1 + p2;
  8.         //实例化一个ModelAndView对象用于向视图中传值
  9.         mv.addObject("msg","结果:" + result);
  10.         //返回视图
  11.         mv.setViewName("test");
  12.         return mv;
  13.     }
  14. }
复制代码

也可以使用method属性指定类型
  1. @RequestMapping(value = "/hello",method = {RequestMethod.POST})
  2. public String index2(Model model){
  3.    model.addAttribute("msg", "hello!");
  4.    return "test";
  5. }
复制代码

但浏览器地址栏进行访问是通过GET方式进行的 ,我们定义的是POST方法-所以报错不匹配
改为GET
  1. @RequestMapping(value = "/hello",method = {RequestMethod.GET})
  2. //我们一般采用这种方式:@GetMapping("/hello")
  3. public String index2(Model model){
  4.    model.addAttribute("msg", "hello!");
  5.    return "test";
  6. }
复制代码

我们可以看到Method 方式太长,不方便,我们可以用延伸的方法
  1. @GetMapping:扮演的是@RequestMapping(method =RequestMethod.GET) 的快捷方式。
  2. @PostMapping (method =RequestMethod.GET)
  3. @PutMapping  (method =RequestMethod.POST)
  4. @DeleteMapping (method =RequestMethod.DELETE)
  5. @PatchMapping  (method =RequestMethod.PATCH)
复制代码
如:
  1. 如果是地址栏输入(GET)可以调用 index方法:
  2. @GetMapping("/commit/{p1}/{p2}")
  3.     //在SpringMVC中可以使用 @PathVariable,让方法参数的值对应绑定到一个URI变量上
  4.     public ModelAndView index(@PathVariable int p1, @PathVariable int p2, ModelAndView mv){
  5.         int result = p1 + p2;
  6.         //实例化一个ModelAndView对象用于向视图中传值
  7.         mv.addObject("msg","结果1:" + result);
  8.         //返回视图
  9.         mv.setViewName("test");
  10.         return mv;
  11.     }
  12. 如果前端页面form表单 method为POST,则调用index2方法
  13. @PostMapping("/commit/{p1}/{p2}")
  14.     //在SpringMVC中可以使用 @PathVariable,让方法参数的值对应绑定到一个URI变量上
  15.     public ModelAndView index2(@PathVariable int p1, @PathVariable int p2, ModelAndView mv){
  16.         int result = p1 + p2;
  17.         //实例化一个ModelAndView对象用于向视图中传值
  18.         mv.addObject("msg","结果2:" + result);
  19.         //返回视图
  20.         mv.setViewName("test");
  21.         return mv;
  22.     }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

怀念夏天

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表