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后,不需要为每个方法单独命一个名字,而是通过判断不同的提交方式来区分这些方法,采用统一命名的方式。
阅读全文
0 0
- Restful
- restful
- RESTful
- restful
- RESTful
- RESTful
- RESTful
- RESTful
- RESTful
- RESTful
- RESTful
- RESTful
- restful
- Restful
- Restful
- RESTful
- RESTful
- Restful
- ActionContext与ServletActionContext的区别及获取request、session等对象
- tomcat 开机自启动配置
- 浅谈对DHCP的理解
- 10.12
- Java 内部类笔记
- RESTful
- 1026. 程序运行时间(15)
- 我的第一个程序
- 作业一顺序表
- 比特币的密钥(yue)、公钥、和地址之间的概念理解
- CorelDRAW 足球制作完你一个自己绘出足球的梦
- css如何不显示元素?
- sweetalert使用
- HDU 3613 Manacher