RESTful

来源:互联网 发布:蓝果网络 编辑:程序博客网 时间:2024/06/11 01:31

RESTful:一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

REST :指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是 RESTful。

网络应用程序,分为前端和后端两个部分。当前的发展趋势,就是前端设备层出不穷(手机、平板、桌面电脑、其他专用设备……)。因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信。后台提供服务的项目提供服务给各种不同的”端”调用。这个可以通过RESTful来完成。

我们可以这样理解RESTful架构:
(1)每一个URI代表一种资源;
(2)客户端和服务器之间,传递这种资源的某种表现层;
(3)客户端通过四个HTTP动词,对服务器端资源进行操作,实现”表现层状态转化”。

我举个例子:

package com.danni.web.controller;@RestController @RequestMapping(value = "dep")public class DepartmentController {    @Autowired    private DeaprtmentService deaprtmentService;    @RequestMapping(value = "", method = RequestMethod.POST)    public JsonResult add(@RequestBody Department department) {        JsonResult jsonResult = new JsonResult();        Integer row = deaprtmentService.add(department);        if (row > 0) {            jsonResult.setResultCode(200);            jsonResult.setResultName("success");        }        return jsonResult;    }    //这里不需要使用@ResponseBody注解,因为@RestController中已经包含了    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)    public JsonResult deleteById(@PathVariable Integer id) {        JsonResult jsonResult = new JsonResult();        Integer row = deaprtmentService.delete(id);        if (row > 0) {            jsonResult.setResultCode(200);            jsonResult.setResultName("success");        }        return jsonResult;    }    @RequestMapping(value = "{id}", method = RequestMethod.PUT)    public JsonResult update(@PathVariable Integer id, @RequestBody Department department) {        //根据id更新数据,        //因此要传递数据,需要使用@RequestBody指明请求体。是以json数据的形式进行传递        //因为需要参数id,所以使用@PathVariable来定义id        JsonResult jsonResult = new JsonResult();        department.setDid(id);        Integer row = deaprtmentService.update(department);        if (row > 0) {            jsonResult.setResultCode(200);            jsonResult.setResultName("success");        }        return jsonResult;    }    @RequestMapping(value = "{id}", method = RequestMethod.GET)    public JsonResult selectById(@PathVariable Integer id) {        //GET提交,不需要提交数据,所以不需要使用@RequestBody,只需要使用@PathVariable指明参数变量即可(即id)        JsonResult jsonResult = new JsonResult();        Department department = deaprtmentService.queryById(id);        if (department != null) {            jsonResult.setResultCode(200);            jsonResult.setResultName("success");            jsonResult.setObject(department);        }        return jsonResult;    }    @RequestMapping(value = "", method = RequestMethod.GET)    public JsonResult selectAll() {        JsonResult jsonResult = new JsonResult();        List<Department> lists = deaprtmentService.query();        if (lists != null && lists.size() > 0) {            jsonResult.setResultCode(200);            jsonResult.setResultName("success");            jsonResult.setObject(lists);        }        return jsonResult;    }}

可以发现,我们指定某个controller后,不需要为每个方法单独命一个名字,而是通过判断不同的提交方式来区分这些方法,采用统一命名的方式。

请求地址 请求方式 提交数据 实现功能 dep GET 无 查询所有 dep/1 GET 无 查询id为1的数据 dep/1 DELETE 无 删除id为1的数据 dep POST { “departname”:”研发部”} 删除id为1的数据 dep/1 PUT { “departname”:”产品部”} 修改id为1的数据