【REST】REST、RESTFUL的理解以及SpringMVC实现Restful编程

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。

网上有相关很多rest的文章,本人看着实在是晦涩,太抽象了,很难看懂。

其实HTTP 1.1协议的整体软件架构就可以说是REST架构

了解REST得知道5个名字:

1、资源 Resource

资源就是服务器上获取到的东西都可以说是资源,一条用户记录,一个用户的密码,一张图片等等都是

2、资源的表述 Representation

就是资源的格式,是HTML、XML、JSON、纯文本、图片等等,可以用各种各样的格式来表述你获取到的资源,这就是资源的表述

3、状态转移 State Transfer

URL定位资源,用HTTP动词(GET,POST,DELETE,DETC)描述操作。操作是动词,资源是名词

4、统一接口 Uniform Interface

REST必须通过统一的接口对资源进行操作,HTTP1.1协议定义了资源的统一接口:

·GET/POST/PUT/DELETE/PATCH/HEAD/OPTIONS

`HTTP  HEAD可以自定义

·HTTP STATUS响应状态 可自定义

·一套标准的内容协商机制

·一套标准的缓存机制

·一套标准的客户端身份认证机制

5、超文本驱动 Hypertext Driven

资源之间通过超链接相互关联,超链接代表资源之间的关系,也代表可执行的状态迁移

 

Restful API就是按照REST架构的思想设计出来的API

核心就是:将API拆分为逻辑上的资源,这些资源通过http(GET/POST/PUT/DELETE等)被操作

SpringMVC的 Restful API

这里就是个简单的例子,URL中tests表示资源,使用GET方法操作,得到资源对象,这里除了GET以外的HTTP方法是404的结果

从以上实例想想,其实挺合理的,这么丰富的HTTP方法,我们干嘛还通过我们的自定义url来定义各种操作呢!!

 

有人说Restful API最好做到Hypermedia,超媒体,即返回结果提供链接,对应下一步的API,即使用户不查文档,也知道下一步该做什么,不过个人觉得,不是刚需,或者我还没遇到这种需求。github就是这样的

上面的实例是最基本的API设计了,有个很明显的缺陷,就是API升级时会遇到问题,因为没有版本的概念,导致服务端升级的话,所有使用的人都受到影响,但是往往实际中,有些用户可能对新版本的API不太需要,仍然想沿用旧的,那么唯一的办法就是部署两套服务端,一个旧的,一个新的,显然成本太高了。

所以需要在API中引入版本的概念

将版本号作为API的URL中的一个资源来设计

将来如果升级版本了,你可以通过在代码中新增一个else分支即可,原本的版本号的API该怎样继续保持不动,新的版本的逻辑新增代码即可,这样可以继续支持旧版本,将来可以通过监控来检测旧版本的使用率,做到平滑过渡,慢慢废弃旧版本。

 

 

 

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Building RESTful Web Services with Spring 5 – Second Edition: Leverage the power of Spring 5.0, Java SE 9, and Spring Boot 2.0 Find out how to implement the REST architecture to build resilient software in Java with the help of the Spring 5.0 framework. REST is an architectural style that tackles the challenges of building scalable web services. In today’s connected world, APIs have taken a central role on the web. APIs provide the fabric through which systems interact, and REST has become synonymous with APIs.The depth, breadth, and ease of use of Spring makes it one of the most attractive frameworks in the Java ecosystem. Marrying the two technologies is therefore a very natural choice.This book takes you through the design of RESTful web services and leverages the Spring Framework to implement these services. Starting from the basics of the philosophy behind REST, you’ll go through the steps of designing and implementing an enterprise-grade RESTful web service. Taking a practical approach, each chapter provides code samples that you can apply to your own circumstances.This second edition brings forth the power of the latest Spring 5.0 release, working with MVC built-in as well as the front end framework. It then goes beyond the use of Spring to explores approaches to tackle resilience, security, and scalability concerns. Improve performance of your applications with the new HTTP 2.0 standards. You’ll learn techniques to deal with security in Spring and discover how to implement unit and integration test strategies.Finally, the book ends by walking you through building a Java client for your RESTful web service, along with some scaling techniques using the new Spring Reactive libraries. What You Will Learn Deep dive into the principles behind REST Expose CRUD operations through RESTful endpoints with the Spring Framework Devise response formats and error handling strategies, offering a consistent and flexible structure to simplify integration for service consumers Follow the best approaches for dealing with a service’s evolution while maintaining backward compatibility Understand techniques to secure web services Comply with the best ways to test RESTful web services, including tips for load testing Optimise and scale web services using techniques such as caching and clustering

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值