Of RESTful style

RESTful Web Service Description

Dr. Roy Thomas Fielding made in 2000

REST is Representational English

The acronym State Transfer

Representational State Transfer or Representational State Transfer

REST is an architectural style Web services

Widespread use of HTTP, URI and other standards and protocols

Lightweight, cross-platform, cross-architecture design language.

img

REST in the end is what?

REST is a style. It is not a standard, nor is it a piece of software, but a thought.

REST is usually based on the use of standard protocols and HTTP, URI, and XML, JSON and HTML existing widespread.

What is RESTful

RESTful corresponding REST in Chinese style.

RESTful Web Service REST is a common application is in compliance with REST-style web service.

Two main web service

JAX-RS

RESTful Web Service

JAX-WS Web Service

The main principles of REST architecture

Everything on the network can be abstracted as resources (Resource)

Each resource has a unique resource identifier (Resource

Identifier)

The same resource with a variety of forms (xml, json, etc.)

Various operations on resources does not change the resource identifier

All operations are stateless (Stateless)

REST architecture in a manner consistent principles can be called RESTful

Statelessness

Stateless makes the client and the server does not have to save the details of the other side, the server only needs to deal with the current

Request, without having to know the front

Request history.

So that makes it easier to free up resources.

Make full use of the server

Pool technology to improve stability and performance.

Resource operation

http://taotao.com/item/

GET: Gets a resource

POST: creates a new resource

PUT: Modify the state of a resource

DELETE: Delete a resource

Resources show

XML

JSON

……

The original way

http://127.0.0.1/user/queryUser/ {id} the GET method, the user **** acquired data id

http://127.0.0.1/user/update the User the POST method **** modified by the user

http://127.0.0.1/user/save the User POST **** method, new user

http://127.0.0.1/user/deleteUser/ {ID} the GET / the POST method, according to a user delete id ****

RESTful

[ Http://127.0.0.1/user/ ] ( http://127.0.0.1/user/ {ID}) [{ID}] ( http://127.0.0.1/user/ {ID}) the GET method, The user acquires data id ****

http://127.0.0.1/user/ the PUT **** method, the user modifies

http://127.0.0.1/user/ POST **** method, new user

} http://127.0.0.1/user/{id the DELETE method, according to a user delete id ****

Development of interfaces, web services more concise

REST interface definition

img

Idempotence: many visits to the same REST interface, resource state get is the same.

Security: the interface to access REST, does not make the server-side resource state changes.

Best Practices

Best practice: REST interface design

• URL consisting of

- network protocol (http, https)

- server address

- interface name

- parameter list

• URL defined limit

- Do not use capital letters

- Use the center line - instead of an underscore _

- encode parameter list should be too

Best practice: design response

Content body is only used to transmit data

Data can be brought to do with principle, you do not need to "unpacking" process

Used to describe the requested data or put the Header Metadata

img

img

http response status code

img

SpringMVC implement RESTful services

SpringMVC original ecological support REST-style architecture design.

Involved Note:

@RequestMapping

@PathVariable

@ResponseBody

ResponseEntity

……

RESTful style interfaces for product development

According RESTful style, written in commodity function CRUD interfaces

Commodity write query interface

Written in ItemInterfaceController

@Controller
@RequestMapping("item/interface")
public class
ItemInterfaceController {

    @Autowired
    private ItemService itemService;

    // http://manager.taotao.com/rest/item/interface/{id}
    /**
     * 根据id查询用户
     * 
     * @param id
     * @return 返回的类型是ResponseEntity,泛型声明为需要返回的数据类型
     */
    @RequestMapping(value = "{id}", method =
RequestMethod.GET) 
// 返回的是ResponseEntity或者加上@ResponseBody注解的效果是一样的,任选其一即可,也可以都设置。
// @ResponseBody
    public
ResponseEntity<Item> queryItemById(@PathVariable("id") Long id) {
       try {
           Item item = this.itemService.queryById(id);
           // 查询成功,响应的状态码应为200
           // 可以设置HttpStatus枚举的OK
           // return
ResponseEntity.status(HttpStatus.OK).body(Item);
           // 也可以使用ok()方法,效果和上面是一样的
           return
ResponseEntity.ok().body(item);
       } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       // 如果有异常,设置状态码为500
       return
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}

Use Google browser testing tool for testing

img

img

Guess you like

Origin blog.csdn.net/weixin_40160543/article/details/92388870