1. 为什么要使用Restful?

 web开发阶段演变:https://www.jianshu.com/p/5e6cb97f9e20

  网站从静态(HTML文档),到CGI、再到JSP等支持session的动态页面,然后演变出web MVC开发模式,有独立的应用服务器,RIA使入门不刷新也能更新局部内容,现在,越来越的网站面向移动设备开发。

 所以,我们要如何开发出在网络环境的软件,用做软件的思维开发出网站,显然,要分层分工,需要相应的API。

2.什么是Restful?

  理解RESTful架构:http://www.ruanyifeng.com/blog/2011/09/restful.html

  (Resources)Representational State Transfer 将资源的表现层转化

 

 Resources: “所谓"资源",就是网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的实在。”

 

Representational :资源的格式,“比如,文本可以用txt格式表现,也可以用HTML格式、XML格式、JSON格式表现,甚至可以采用二进制格式;图片可以用JPG格式表现,也可以用PNG格式表现。”

 

 URI:每种资源都对应一个特定的URI,URI代表资源的实体位置,访问URI就可以获取资源,不表现格式。

 

Representational &URI区分:网站的后缀html属于表现层,不属于URI,“它的具体表现形式,应该在HTTP请求的头信息中用Accept和Content-Type字段指定,这两个字段才是对"表现层"的描述。”

 

URI误区1:URI不应该包含动词,URI只表示资源实体化的位置,动词应该放在HTTP协议中。

eg1:“举例来说,某个URI是/posts/show/1,其中show是动词,这个URI就设计错了,正确的写法应该是/posts/1,然后用GET方法表示show。”

eg2:但“从账户1向账户2汇款500元”HTTP的动词表示不了,我们应该把动词做成资源——一种服务。

“POST /accounts/1/transfer/500/to/2”错误

“POST /transaction HTTP/1.1
  Host: 127.0.0.1
  from=1&to=2&amount=500.00”正确

 

URI误区2:URI中不需要版本号,不同的版本,其实是同一种资源的不同表现形式,正如我打开某网站,不会每次都在地址栏里输入它的版本号。

“版本号可以在HTTP请求头信息的Accept字段中进行区分”

“Accept: vnd.example-com.foo+json; version=1.0”

 

State Transfer:从MVC的角度理解,每个层级只能调用自己层级的方法,其余的留给下一层去做。

“客户端用到的手段,只能是HTTP协议。具体来说,就是HTTP协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源。”

 

3.Restful API设计原则


   RESTful API 设计指南http://www.ruanyifeng.com/blog/2014/05/restful_api.html

   协议、域名、版本、路径、HTTP动词、过滤信息、状态码、错误错、返回结果

 

   RESTful API 最佳实践 http://www.ruanyifeng.com/blog/2018/10/restful-api-best-practices.html

   Restful API设计手册https://wenku.baidu.com/view/4cb6b41c3d1ec5da50e2524de518964bce84d256.html

4.如何使用?

java spring rest 调用 java spring restful_HTTP

 

 

java spring rest 调用 java spring restful_Spring_02

 

 

package edu.example.hello;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@Api("this is a restful controller")
@RestController
@RequestMapping("/api/v1")
public class HelloRestController {
	@Autowired
	private StudentFactory sf;
	@ApiOperation("Show a simple message...")
	@Deprecated
    @GetMapping("/msg")
    public String msg() {
    	return "Hello。。Restful";//返回值,不是调用的页面
    }
	@ApiOperation("Show all students...")
    @GetMapping("/student/")
    public List<Student>students(){
    	return sf.getStudents();
    }
	
	@ApiOperation("Show a student choosed...")
    @GetMapping("/student/{no}/")
    public Student students(@ApiParam("Student no")@PathVariable String no) throws StudentNotFoundException{
    	return sf.SfindBy(no);
    }
}