Restful 架构风格使用入门

一. Restful 是什么

REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移。 它首次出现在2000年Roy Fielding的博士论文中,Roy Fielding是HTTP规范的主要编写者之一。 他在论文中提到:“我这篇文章的写作目的,就是想在符合架构原理的前提下,理解和评估以网络为基础的应用软件的架构设计,得到一个功能强、性能好、适宜通信的架构。REST指的是一组架构约束条件和原则。” 如果一个架构符合REST的约束条件和原则,我们就称它为RESTful架构。

REST本身并没有创造新的技术、组件或服务,而隐藏在RESTful背后的理念就是使用Web的现有特征和能力, 更好地使用现有Web标准中的一些准则和约束。

详细介绍:
菜鸟教程:https://www.runoob.com/w3cnote/restful-architecture.html
百度百科:https://baike.baidu.com/item/RESTful/4406165?fr=aladdin

  1. 对URL地址的一种规范
    普通url:http://localhost/student?id=1
    Restful风格:http://localhost/student/1
    特点:url简洁,将参数通过url传到服务端
  2. http的方法规范
    对

二. Restful 案例

2.1 需求

根据视频 id 查询信息,返回Json格式的数据

2.2 Controller

这里使用@RestController 注解,返回 json 字符串

/**
 * 视频控制层
 * 查询属于普通用户功能
 * @author hp
 * @date 2020/04/01
 */
@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 根据id查询视频信息
     * Restful 风格
     * @GetMapping("/find_by_id/{id}") 里边的{id}表示占位符,通过@PathVariable获取占位符中的参数
     * 如果占位符中的名称和形参名一致,在@PathVariable可以不指定名称
     * @param id
     * @return
     */
    @GetMapping("/find_by_id/{id}")
    public Object find_by_id(@PathVariable Integer id){
        Video byId = videoService.findById(id);
        return byId;
    }

}

访问示例:
在这里插入图片描述

发布了70 篇原创文章 · 获赞 114 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43330884/article/details/105430611