首页 > 其他分享 >RESTful 风格

RESTful 风格

时间:2022-08-28 22:25:12浏览次数:37  
标签:URL ResultVo gid 风格 RESTful id localhost

一、RESTful

前后端分离开发的项目中,前后端是通过接口规范进行请求和响应,后端向前端提供请求时就要对外暴露一个URL;

URL的设计不是随意的,需要遵从一定的设计规范—RESTful

RESTful是一种Web api的标准,也就是一种url设计风格规范

  • 每个URL请求路径代表服务器上唯一资源
传统的url
		http://localhost:8080/goods?name=1
RestFul
		http://localhost:8080/goods/1
    @RequestMapping(value = "/del/{gid}")
    public ResultVo delGoods(@PathVariable("gid") int id) {
        System.out.println("----" + id);
        return new ResultVo(1000, "Success", null);
    }
  • 使用不同的请求方式表示不同的操作

    SpringMVC对RESTful风格提供了很好的支持,在我们定义一个接口的URL时,可以通过@RequestMapping(value = "/del/{gid}", method = RequestMethod.DELETE)形式指定请求方式,也可以使用特定的方式的注解设定URL

    PostMapping("/{}")

    GetMapping("/{}")

    DeleteMapping("/{}")

    PutMapping("/{}")

    • POST 添加
    • GET 查询
    • PUT 修改
    • DELETE删除
    • OPTION(预检)
    • 根据ID删除一个商品
  //http://localhost:8080/goods/1  [delete]
  @RequestMapping(value = "/del/{gid}", method = RequestMethod.DELETE)
  public ResultVo delGoods(@PathVariable("gid") int id) {
      System.out.println("----" + id);
      return new ResultVo(1000, "Success", null);
  }
  • 根据ID查询一个商品
  //http://localhost:8080/goods/1  [get]
  @RequestMapping(value = "{id}", method = RequestMethod.GET)
  public ResultVo getGoods(@PathVariable("id") int id) {
      System.out.println("----" + id);
      return new ResultVo(1000, "Success", null);
  }
  • 接口响应的资源表现形式采用JSON
    • 在每个接口添加@ResponseBody注解将返回的对象格式为JSON
    • 或者直接在控制器类使用@RestController注解声明控制器
  • 前端(Android\ios\pc)通过无状态的HTTP协议与后端接口进行交互

标签:URL,ResultVo,gid,风格,RESTful,id,localhost
From: https://www.cnblogs.com/wanwanmeixiangdao/p/16633867.html

相关文章

  • Java开发学习(二十七)----SpringMVC之Rest风格解析及快速开发
    一、REST简介REST(RepresentationalStateTransfer),表现形式状态转换,它是一种软件架构风格当我们想表示一个网络资源的时候,可以使用两种方式:传统风格资源描述形式......
  • 【转】YApi结合swag管理和生成go项目restful API文档
     原文:https://blog.csdn.net/tuobicui6522/article/details/102980653 swag命令安装: goinstallgithub.com/swaggo/swag/cmd/swag@latest swag命令对应的githu......
  • flask-restful使用指南
    flask-restful是flask模块的一个扩展,能够快速构建restful风格的api。对于其他的扩展也有很高的兼容性。安装flask_restfulpipinstallflask_restful简单使用fromfl......
  • restfulAPI接口规范/django rest_Framework_环境
    一.RESTFUlAPI设计1).域名应该将api部署在专用域名下https://www.baidu.com/api/2).版本应该将API版本号放到路径中https://www.baidu.com/api/1.0/loginhttps:......
  • springMvc34-restful的put
    创建maven项目就不说了,需要的找我前面的博客pom.xml文件   <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins......
  • springMvc38-restful的crud实现删除方式
    上图·是目录结构,本节是有问同学的,当好好总结pom.xml   <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan......
  • springMvc39-restful的crud实现增加方式
    上图·是目录结构,本节是有问同学的,当好好总结pom.xml   <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan......
  • springMvc40-restful的crud的项目原型介绍
    上图·是目录结构,本节是有问同学的,当好好总结pom.xml   <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan......
  • Python入门系列(二)语法风格
    python缩进Python使用缩进来表示代码块,例如if5>2:print("Fiveisgreaterthantwo!")如果跳过缩进,Python将给您一个错误。#下边的写法将会报错if5>2:pri......
  • RESTful和RPC
    RESTful架构是对MVC架构改进后所形成的一种架构,通过使用事先定义好的接口与不同的服务联系起来。在RESTful架构中,【浏览器使用POST,DELETE,PUT和GET四种请求方式分别对指定的......