What is RESTful? RESTfule style

REVIEW

Understand what is before REST, go to make up the brain what is HTTP, refer to [ Http protocol ]

What is REST?

  REST (English: Representational State Transfer, referred to as REST , meaning: Representational state transition, describes an architecture style of network systems, such as web applications).

  It is a style of software architecture, design, rather than the standard, but provides a set of design principles and constraints, it is mainly used for client and server interaction class software. Based on this style of software can be more concise, more structured and easier to implement caching mechanisms.

  It itself is not of use, the core value lies in how to design a network interface complies with REST-style.

What is RESTful?

  REST : refers to a set of architectural constraints and principles. These applications meet the constraints and principles or design is RESTful .

The characteristics of RESTful

  Resources (Resources) : an entity on the network, or is a specific information on the network. It can be a piece of text, a picture, a song, a service, in short, is a concrete existence. It can be directed by a URI (Uniform Resource Locator), a URI corresponding to each resource characteristic. To obtain this resource, you can access its URI, so URI is the unique identifier for each resource.

  Presentation layer (Representation) : The resources presented in the form of concrete, called its presentation layer (Representation). For example, you can use the text format txt performance, you can also use HTML format, XML format, JSON format performance, even in binary format.

  State transition (State Transfer) : Each issue a request on behalf of a client interaction and servers. HTTP protocol is a stateless protocol, i.e. all states are stored in the server. Therefore, if the client wants to operate the server, must by some means, let the server take place "state" (State Transfer). And this conversion is based on the presentation layer, so is the "presentation layer state transitions." Specifically, there is the HTTP protocol, four verb indicates the operation mode: GET, POST, PUT, DELETE . They correspond to four basic operations: GET used to obtain resources, POST is used to create a new resource, PUT for updating resources, DELETE to delete the resource.

How to design the API RESTful application?

  Path Design : After the database is designed, basically you can determine what resources to operate the corresponding path can be designed.

  Design verb : that is specific for the type of operation resources, there are HTTP verb commonly used HTTP verbs as follows: POST, DELETE, PUT, GET

RESTful example

  1. / Account / 1 HTTP GET: id = account 1 to give the
  2. / Account / 1 HTTP DELETE: Delete the id = account 1
  3. / Account / 1 HTTP PUT: Update id = account 1 of

SpringMvc support for the RESTful

RESTful URL path variable

The PATTERN-the URL : set / easy RESTful intercept request.

@PathVariable : template variables can be parsed (URL of {ID} / {name} )

URL:http://localhost:8080/ssm/cyb/item/1/chenyanbin


Controller layer:
@RequestMapping("{id}/{name}")
@ResponseBody
public Item queryItemById (@PathVariable Integer id, @ PathVariable String name) {
.............
}

Figure

Of RESTful CRUD

@RequestMapping : By setting method attributes the CRUD , you may be the same URL mapping to a different of HandlerMethod the method.

@ GetMapping, @ PostMapping, @ PutMapping , @ DeleteMapping annotation method property with @RequestMapping annotations.

Of RESTful resource representation

RESTful service is an important feature is a resource that can be manifested in many forms , can be used in SpringMvc in ContentNegotiatingManager this content negotiation manager to achieve in this way.

There are three ways for content negotiation

  1. Extension , such as .json want to express my JSON data format, .xml mean I want to xml format data
  2. Request parameters : The default is "format"
  3. Accept request header parameter settings , such as set Accept application / json JSON-formatted data to be represented

现在一般RESTful风格响应的数据一般都是JSON格式,所以一般也不使用内容协商管理器,直接使用@ResponseBody注解将数据按照JSON格式返回

静态资源访问<mvc:resources>

在Springmvc.xml文件中,使用mvc:resources标签,具体如下:

<!-- 当DispatcherServlet配置为/来拦截请求的时候,需要配置静态资源的访问映射 -->
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
....
....
....
....

如图

SpringMvc会把mapping映射到ResourceHttpRequestHandler,这样静态资源在经过DispatcherServlet转发时就可以找到对应的Handler了。

Guess you like

Origin www.cnblogs.com/chenyanbin/p/12040880.html