RESTFul
RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。RESTFUL适用于移动互联网厂商作为业务接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。
RESTFul的实现
具体说,就是HTTP协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE
他们分别体现GET(查找)、post(添加新建)、put(修改)、delete(删除)
同时我们可以通过对一个资源进行命名来对资源进行状态转移,就如我们要修改user中的信息,我们有两个不同的方法(一个客户端另一个为管理端。)我们可以通过同一个命名(url)来对一个资源进行状态转移,其实也就是增删查改。
RESTFul风格提倡URL地址使用统一风格设计,从前到后各个单词使用斜杠分开,不使用问号键值对方式携带请求参数,而是将要发送给服务器的数据作为Url地址的一部分,以保证整体风格的一致性。
操作 | 传统方式 | REST风格 |
---|---|---|
查询操作 | getUserById?id=1 | user/1–>get请求 |
保存操作 | saveUser | user–>post请求 |
删除操作 | deleteUser?id=1 | user/1–>delete请求 |
更新操作 | updateUser | user–>put请求方式 |
Restful使用(GET、POST)
创建UserController
@RequestMapping("/test_restful")
public String test(){
return "test_restful";
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String getAllUser(){
System.out.println("查询所有用户信息");
return "success";
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public String getUserById(){
System.out.println("查询单个用户");
return "success";
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public String addUser(HttpServletRequest reqs){
String username = reqs.getParameter("username");
String password = reqs.getParameter("password");
System.out.println("添加用户 "+" 账号为:"+username+"密码为:"+password);
return "success";
}
创建test_restful.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a th:href="@{/user}">查询所有信息</a>
<a th:href="@{/user/1}">查询单个信息</a>
<form th:action="@{/user}" method="post">
用户名<input type="text" name = "username"><br>
密码<input type="password" name = "password"><br>
<input type="submit" value="添加">
</form>
</body>
</html>
我们点击查询所有信息
控制台输出
点击查找单个信息
在文本框中输入自己的信息
点击提交,跳转成功
我们看向控制台
RESTFul案例
创建employee类
package com.jin.rest.bean;
public class Employee {
private Integer id;
private String lastName;
private String email;
//1 male, 0 female
private Integer gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Employee(Integer id, String lastName, String email, Integer gender) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
public Employee() {
}
}
创建对应的持久层
package com.jin.rest.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.jin.rest.bean.Employee;
import org.springframework.stereotype.Repository;
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
static{
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1));
employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1));
employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0));
employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0));
employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1));
}
private static Integer initId = 1006;
public void save(Employee employee){
if(employee.getId() == null){
employee.setId(initId++);
}
employees.put(employee.getId(), employee);
}
public Collection<Employee> getAll(){
return employees.values();
}
public Employee get(Integer id){
return employees.get(id);
}
public void delete(Integer id){
employees.remove(id);
}
}
创建对应的控制器
package com.jin.rest.controller;
import com.jin.rest.bean.Employee;
import com.jin.rest.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public String emmpll(Model model){
Collection<Employee> employeeList = employeeDao.getAll();
model.addAttribute("employeeList",employeeList);
return "employee_list";
}
@RequestMapping(value = "/employee" , method = RequestMethod.POST)
public String addEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/employee";
}
@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";
}
@RequestMapping(value = "/employee" , method = RequestMethod.PUT)
public String updateById(Employee employee){
employeeDao.save(employee);
return"redirect:/employee";
}
}
创建html文件
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/employee}">查看员工信息</a>
</body>
</html>
employee_list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Employee info</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0" style="text-align: center;">
<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 th:href="@{/employee/}+${employee.id}">detele</a>
<a th:href="@{/employee/}+${employee.id}">update</a>
</td>
</tr>
</table>
<form method="post">
<input type="hidden" name="_method" value="delete"
</form>
</body>
</html>
employee_add
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>add</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="提交"><br>
</form>
</body>
</html>
employee_update
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</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>
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>