当前位置: 首页 > 工具软件 > jfinal-rest > 使用案例 >

[Java web]-- spring 官方对REST的定义

李烨烁
2023-12-01

一、 Understanding REST(理解REST)

原文:
REST (Representational State Transfer----》具象状态传输) was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation.
REST is an architectural style for designing distributed systems. It is not a standard but a set of constraints, 
such as being stateless, having a client/server relationship, and a uniform interface. REST is not strictly related to HTTP,
    but it is most commonly associated with it.

译文:
   2000年在罗伊菲尔丁的博士论文中引入和定义了REST。
   REST是用于设计分布式系统的体系结构样式,他不是一种标准,但是是一种规范,比如无状态,但具有客户机/服务器关系,有一个统一的接口,但是REST不直接关联HTTP。


二、Principles of REST(REST的设计原则)
原文:
Resources expose easily understood directory structure URIs.
Representations transfer JSON or XML to represent data objects and attributes.
Messages use HTTP methods explicitly (for example, GET, POST, PUT, and DELETE).
Stateless interactions store no client context on the server between requests.
State dependencies limit and restrict scalability. The client holds session state.
译文:
    通过非常容易理解的URIs目录结构去暴露资源。
一种传输JSON或者XML格式的数据去代表对象和属性的表现形式。
消息明确地使用 HTTP 方法(例如:GET,POST, PUT, DELETE )。
在服务器请求之间的无状态交互存储没有客户端环境。


三、HTTP methods(HTTP方法)
注意:REST服务中虽然建议使用HTTP协议中四种标准方法POST、DELETE、PUT、GET来分别实现常见的“增删改查”,
      但实际中,建议直接用POST来实现“增改”,GET来实现“删查”即可(原因:DELETE和PUT甚至会被一些防火墙阻挡)。

Use HTTP methods to map CRUD (create, retrieve, update, delete) operations to HTTP requests.

GET
Retrieve information. GET requests must be safe and idempotent, meaning regardless of how many times it 
repeats with the same parameters, the results are the same. They can have side effects,
 but the user doesn't expect them, so they cannot be critical to the operation of the system. 
 Requests can also be partial or conditional.

Retrieve an address with an ID of 1:
GET /addresses/1


POST
Request that the resource at the URI do something with the provided entity. Often POST is used to 
create a new entity, but it can also be used to update an entity.
Create a new address:
POST /addresses


PUT
Store an entity at a URI. PUT can create a new entity or update an existing one. A PUT request is idempotent.
 Idempotency is the main difference between the expectations of PUT versus a POST request.
Modify the address with an ID of 1:
PUT /addresses/1
Note: PUT replaces an existing entity. If only a subset of data elements are provided,
 the rest will be replaced with empty or null.


PATCH
Update only the specified fields of an entity at a URI. A PATCH request is idempotent.
 Idempotency is the main difference between the expectations of PUT versus a POST request.
PATCH /addresses/1


DELETE
Request that a resource be removed; however, the resource does not have to be removed immediately. 
It could be an asynchronous or long-running request.
Delete an address with an ID of 1:
DELETE /addresses/1
HTTP status codes

Status codes indicate the result of the HTTP request.
译文:状态代码表示HTTP请求的结果。
1XX - informational
2XX - success
3XX - redirection
4XX - client error
5XX - server error


四、Media types(数据类型)

The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request. 
The client may set Accept to application/json if it is requesting a response in JSON. 
Conversely, when sending data, setting the Content-Type to  application/xml tells the client that the data being sent in the request is XML.

译文:
    接受和Content-Type HTTP头,可以用来描述发送的或者在HTTP请求中被请求的内容。
如果客户端请求是一个JSON的响应类型,那么客户端也可以设置接收类型是JSON

 

原文官方网站:http://spring.io/understanding/REST点击打开链接

 

 

 

 

 

 





 

 类似资料: