360免费建站系统,百度网站官网,设计网站要多少钱,做游戏人设计网站Rest简介
REST是英文representational state transfer(表象性状态转变)或者表述性状态转移。Rest是web服务的一种架构风格。使用HTTP,URI,XML,JSON,HTML等广泛流行的标准和协议。轻量级,跨平台,跨语言的架构设计。它是一种设计风格,不是一种标准,是一种思想。
Rest架构的主要…Rest简介
REST是英文representational state transfer(表象性状态转变)或者表述性状态转移。Rest是web服务的一种架构风格。使用HTTP,URI,XML,JSON,HTML等广泛流行的标准和协议。轻量级,跨平台,跨语言的架构设计。它是一种设计风格,不是一种标准,是一种思想。
Rest架构的主要原则
网络上的所有事物都被抽象为资源每个资源都有一个唯一的资源标识符同一个资源具有多种表现形式(xml,json等)对资源的各种操作不会改变资源标识符所有的操作都是无状态的符合REST原则的架构方式即可称为RESTful
什么是Restful
对应的中文是rest式的Restful web service是一种常见的rest的应用,是遵守了rest风格的web服务rest式的web服务是一种ROA(The Resource-Oriented Architecture)(面向资源的架构)
为什么会出现Restful
在Restful之前的操作
http://127.0.0.1/user/query GET 根据用户id查询用户数据http://127.0.0.1/user/save POST 新增用户http://127.0.0.1/user/update POST 修改用户信息http://127.0.0.1/user/delete GET/POST 删除用户信息
Restful用法
http://127.0.0.1/user/1 GET 根据用户id查询用户数据http://127.0.0.1/user POST 新增用户http://127.0.0.1/user PUT 修改用户信息http://127.0.0.1/user DELETE 删除用户信息
常用的HTTP动词有下面五个括号里是对应的SQL命令
GETSELECT从服务器取出资源一项或多项。POSTCREATE在服务器新建一个资源。PUTUPDATE在服务器更新资源客户端提供改变后的完整资源。PATCHUPDATE在服务器更新资源客户端提供改变的属性。DELETEDELETE从服务器删除资源。
如何使用
http方法资源操作幂等安全GETSELECT是否POSTINSERT否否PUTUPDATE是否DELETEDELETE是否 幂等性对同一REST接口的多次访问得到的资源状态是相同的。 安全性对该REST接口访问不会使服务器端资源的状态发生改变。 ##6.SpringMVC原生态的支持了REST风格的架构设计 ###所涉及的注解
RequestMappingPathVariableResponseBody
package cn.itcast.mybatis.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import cn.itcast.mybatis.pojo.User;
import cn.itcast.mybatis.service.NewUserService;RequestMapping(restful/user)
Controller
public class RestUserController {Autowiredprivate NewUserService newUserService;/*** 根据用户id查询用户数据* * param id* return*/RequestMapping(value {id}, method RequestMethod.GET)ResponseBodypublic ResponseEntityUser queryUserById(PathVariable(id) Long id) {try {User user this.newUserService.queryUserById(id);if (null user) {// 资源不存在响应404return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);}// 200// return ResponseEntity.status(HttpStatus.OK).body(user);return ResponseEntity.ok(user);} catch (Exception e) {e.printStackTrace();}// 500return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);}/*** 新增用户* * param user* return*/RequestMapping(method RequestMethod.POST)public ResponseEntityVoid saveUser(User user) {try {this.newUserService.saveUser(user);return ResponseEntity.status(HttpStatus.CREATED).build();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}// 500return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);}/*** 更新用户资源* * param user* return*/RequestMapping(method RequestMethod.PUT)public ResponseEntityVoid updateUser(User user) {try {this.newUserService.updateUser(user);return ResponseEntity.status(HttpStatus.NO_CONTENT).build();} catch (Exception e) {e.printStackTrace();}// 500return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);}/*** 删除用户资源* * param user* return*/RequestMapping(method RequestMethod.DELETE)public ResponseEntityVoid deleteUser(RequestParam(value id, defaultValue 0) Long id) {try {if (id.intValue() 0) {// 请求参数有误return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();}this.newUserService.deleteUserById(id);// 204return ResponseEntity.status(HttpStatus.NO_CONTENT).build();} catch (Exception e) {e.printStackTrace();}// 500return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);}
}##7.HTTP相应状态码
codeHTTP operationBody ContentsDescription200GET,PUT资源操作成功201POST资源元数据对象创建成功202POST,PUT,DELETE,PATCHN/A请求已经被接受204GETN/A操作已经执行成功但是没有返回数据301GETlink资源已被移除303GETlink重定向304GETN/A资源没有被修改400GET,POST,PUT,DELETE,PATCH错误提示消息参数列表错误缺少格式不匹配401GET,POST,PUT,DELETE,PATCH错误提示消息未授权403GET,POST,PUT,DELETE,PATCH错误提示消息访问受限授权过期404GET,POST,PUT,DELETE,PATCH错误提示消息资源服务未找到405GET,POST,PUT,DELETE,PATCH错误提示消息不允许的http方法409GET,POST,PUT,DELETE,PATCH错误提示消息资源冲突或者资源被锁定415GET,POST,PUT,DELETE,PATCH错误提示消息不支持的数据媒体类型429GET,POST,PUT,DELETE,PATCH错误提示消息请求过多被限制500GET,POST,PUT,DELETE,PATCH错误提示消息系统内部错误501GET,POST,PUT,DELETE,PATCH错误提示消息接口未实现 原文地址https://blog.csdn.net/chenxiaochan/article/details/73716617