主页 > 创业  > 

【Java后端】RestfulAPI接口

【Java后端】RestfulAPI接口
Restful API 接口

REST:Representational State Transfer,表现层(前端的视图页面和后端的控制层)资源状态转移。

一种软件架构的风格(格式)

RESTful 是目前最流行的互联网软件架构,如果一个架构符合 REST 原则,则称它为 RESTful 架构。

REST 风格提倡 URL 地址使用统一的风格设计,从前到后各个单词使用斜杠分开,不使用问号键值对方式携带请求参数,而是将要发送给服务器的数据作为 URL 地址的一部分,以斜线分割线形式传参,不同的请求方式来表示对同一资源的不同操作,以保证整体风格的一致性。

具体说,就是 HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE,它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。

操作传统方式REST风格查询操作getUserById?id=1user/1–>get请求方式保存操作saveUseruser–>post请求方式删除操作deleteUser?id=1user/1–>delete请求方式更新操作updateUseruser–>put请求方式

浏览器只支持发送get和post方式的请求

Restful 特点包括:

每一个URI代表1种资源;客户端使用GET、POST、PUT、DELETE4个表示操作方式的动词对服务端资源进行操作:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源;通过操作资源的表现形式来操作资源;资源的表现形式是XML或者HTML;客户端与服务端之间的交互在请求之间是无状态的,从客户端到服务端的每个请求都必须包含理解请求所必需的信息。 术语 资源

资源是一种看待服务器的方式,即,将服务器看作是由很多离散的资源组成。每个资源是服务器上一个可命名的抽象概念。因为资源是一个抽象的概念,所以它不仅仅能代表服务器文件系统中的一个文件、数据库中的一张表等等具体的东西,可以将资源设计的要多抽象有多抽象,只要想象力允许而且客户端应用开发者能够理解。与面向对象设计类似,**资源是以名词为核心来组织的,首先关注的是名词。**一个资源可以由一个或多个URI来标识。URI既是资源的名称,也是资源在Web上的地址。对某个资源感兴趣的客户端应用,可以通过资源的URI与其进行交互。

资源的表述

资源的表述是一段对于资源在某个特定时刻的状态的描述。可以在客户端-服务器端之间转移(交换)。资源的表述可以有多种格式,例如HTML/XML/JSON/纯文本/图片/视频/音频等等。资源的表述格式可以通过协商机制来确定。请求-响应方向的表述通常使用不同的格式。

状态转移

状态就是它的一种表现形式,是页面还是jsp还是xml还是一个json

状态转移说的是:在客户端和服务器端之间转移(transfer)代表资源状态的表述。通过转移和操作资源的表述,来间接实现操作资源的目的。

背景

随着互联网化等逐渐深入,应用系统也变得越来越复杂,系统架构正在朝着微服务化,中台化的方向发展。这就需要各个系统、各个厂家之间的数据频繁交互。所以,使用 Restful 风格构建的应用接口适用于移动互联网厂商的业务场景,正在被越来越多的企业推荐使用。

使用 功能URL 地址请求方式访问首页/GET查询全部数据/employeeGET删除/employee/2DELETE跳转到添加数据页面/toAddGET执行保存/employeePOST跳转到更新数据页面/employee/2GET执行更新/employeePUT

实体类:

public class Employee { private Integer id; private String lastName; private String email; //1 male, 0 female private Integer gender; } 访问首页 1、配置view-controller <mvc:view-controller path="/" view-name="index"/> 2、创建index.html <!DOCTYPE html> <html lang="en" xmlns:th="http:// .thymeleaf.org"> <head> <meta charset="UTF-8" > <title>Title</title> </head> <body> <h1>首页</h1> <a th:href="@{/employee}">访问员工信息</a> </body> </html> 查询所有员工数据 1、控制器方法 @RequestMapping(value = "/employee", method = RequestMethod.GET) public String getEmployeeList(Model model){ Collection<Employee> employeeList = employeeDao.getAll(); model.addAttribute("employeeList", employeeList); return "employee_list"; } 2、创建employee_list.html <!DOCTYPE html> <html lang="en" xmlns:th="http:// .thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Employee Info</title> <script type="text/javascript" th:src="@{/static/js/vue.js}"></script> </head> <body> <table border="1" cellpadding="0" cellspacing="0" style="text-align: center;" id="dataTable"> <tr> <th colspan="5">Employee Info</th> </tr> <tr> <th>id</th> <th>lastName</th> <th>email</th> <th>gender</th> <th>options(<a th:href="@{/toAdd}">add</a>)</th> </tr> <tr th:each="employee : ${employeeList}"> <td th:text="${employee.id}"></td> <td th:text="${employee.lastName}"></td> <td th:text="${employee.email}"></td> <td th:text="${employee.gender}"></td> <td> <a class="deleteA" @click="deleteEmployee" th:href="@{'/employee/'+${employee.id}}">delete</a> <!--''里边内容会被当成请求地址,而后面的内容会当成参数--> <!--<a class="deleteA" @click="deleteEmployee" th:href="@{/employee/}+${employee.id}">--> <a th:href="@{'/employee/'+${employee.id}}">update</a> </td> </tr> </table> </body> </html> 删除 1、创建处理delete请求方式的表单 <!-- 作用:通过超链接控制表单的提交,将post请求转换为delete请求 --> <form id="delete_form" method="post"> <!-- HiddenHttpMethodFilter要求:必须传输_method请求参数,并且值为最终的请求方式 --> <input type="hidden" name="_method" value="delete"/> </form> 2、删除超链接绑定点击事件

引入vue.js

<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>

静态资源没办法被springMVC前端控制器处理,而默认的servlet才是处理静态资源的servlet

<!--开放对静态资源的访问--> <mvc:default-servlet-handler/>

删除超链接

<a class="deleteA" @click="deleteEmployee" th:href="@{'/employee/'+${employee.id}}">delete</a>

通过vue处理点击事件

<script type="text/javascript"> var vue = new Vue({ el:"#dataTable", methods:{ //event表示当前事件 deleteEmployee:function (event) { //通过id获取表单标签 var delete_form = document.getElementById("delete_form"); //将触发事件的超链接的href属性为表单的action属性赋值,如果表单的action没有设置,就会提交到当前页面 //thymeleaf先解析后解析vue delete_form.action = event.target.href; //提交表单 delete_form.submit(); //阻止超链接的默认跳转行为,对于sumit按钮,即使有绑定事件,他会先执行事件再提交表单 event.preventDefault(); } } }); </script> 3、控制器方法 @RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE) public String deleteEmployee(@PathVariable("id") Integer id){ employeeDao.delete(id); return "redirect:/employee"; } 跳转到添加数据页面 1、配置view-controller <mvc:view-controller path="/toAdd" view-name="employee_add"></mvc:view-controller> 2、创建employee_add.html <!DOCTYPE html> <html lang="en" xmlns:th="http:// .thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Add Employee</title> </head> <body> <form th:action="@{/employee}" method="post"> lastName:<input type="text" name="lastName"><br> email:<input type="text" name="email"><br> gender:<input type="radio" name="gender" value="1">male <input type="radio" name="gender" value="0">female<br> <input type="submit" value="add"><br> </form> </body> </html> 执行保存 1、控制器方法 @RequestMapping(value = "/employee", method = RequestMethod.POST) public String addEmployee(Employee employee){ employeeDao.save(employee); return "redirect:/employee"; } 跳转到更新数据页面 1、修改超链接 <a th:href="@{'/employee/'+${employee.id}}">update</a> 2、控制器方法 @RequestMapping(value = "/employee/{id}", method = RequestMethod.GET) public String getEmployeeById(@PathVariable("id") Integer id, Model model){ Employee employee = employeeDao.get(id); model.addAttribute("employee", employee); return "employee_update"; } 3、创建employee_update.html <!DOCTYPE html> <html lang="en" xmlns:th="http:// .thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Update Employee</title> </head> <body> <form th:action="@{/employee}" method="post"> <input type="hidden" name="_method" value="put"> <input type="hidden" name="id" th:value="${employee.id}"> lastName:<input type="text" name="lastName" th:value="${employee.lastName}"><br> email:<input type="text" name="email" th:value="${employee.email}"><br> <!-- th:field="${employee.gender}"可用于单选框或复选框的回显 若单选框的value和employee.gender的值一致,则添加checked="checked"属性 --> gender:<input type="radio" name="gender" value="1" th:field="${employee.gender}">male <input type="radio" name="gender" value="0" th:field="${employee.gender}">female<br> <input type="submit" value="update"><br> </form> </body> </html>

发送put,可以小写或者大写

执行更新 1、控制器方法 @RequestMapping(value = "/employee", method = RequestMethod.PUT) public String updateEmployee(Employee employee){ employeeDao.save(employee); return "redirect:/employee"; }
标签:

【Java后端】RestfulAPI接口由讯客互联创业栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“【Java后端】RestfulAPI接口