七、RESTFul

1、RESTFUL简介

REST:Representational State Transfer,表现层资源状态转移

a>资源

资源是一种看待服务器的方式,即,将服务器看作是由很多离散的资源组成。每个资源是服务器上可命名的抽象资源。因为资源是一个抽象的概念,所以它不仅仅能代表服务器文件系统中的一个文件,数据库中的一张表等等具体的东西,可以将资源设计的要多抽象有多抽象。一个资源可以由一个或多个URI来表示。URI既是资源的名称,也是资源在Web上的地址。

b>资源的表述

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

c>状态转移

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

2、RESTFUL的实现

具体说,就是HTTP协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。

它们分别对应四种基本操作:GET用来获取资源,POST用来新建资源、PUT用来更新资源、DELETE用来删除资源。

REST风格提倡URL地址使用统一的风格设计,从前到后各个单词使用斜杠分开,不使用问好健值对方式携带请求参数,而是将要发送服务器的数据作为URL地址的一部分,以保证风格的一致性。

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @Author SHKing
 * @Date 2021/11/28 14:42
 * @Version 1.0
 */

@Controller
public class UserController {

    /**
     * 使用RESTFUL模拟用户资源的增删改查
     * /user GET 查询所有用户信息
     * /user/1  GET 根据用户ID查询用户信息
     * /user  POST 添加用户信息
     * /user/1  DELETE 删除用户信息
     * /user  PUT 修改用户信息
     */

    @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("根据id查询用户信息");
        return "success";
    }

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public String insertUser(String username, String password){
        System.out.println("添加用户信息:" + username + ", " + password);
        return "success";
    }

    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public String updateUser(String username, String password){
        System.out.println("修改用户信息:" + username + ", " + password);
        return "success";
    }

}

注意:由于大部分浏览器只能使用GET和POST请求方式,在HTML的form表单中只能填写这两种请求方式,无法实现PUT、DELETE等请求方式,这里需要在web.xml文件中配置过滤器来完成请求方式的转换,内容如下:

    <!--配置HiddenHttpMethodFilter-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

这里要求前端的请求方式为“POST”的情况下才能转换请求方式,同时还需要添加一个请求内容:_method设置为想要的请求方式:

	if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
			String paramValue = request.getParameter(this.methodParam);
			if (StringUtils.hasLength(paramValue)) {
				String method = paramValue.toUpperCase(Locale.ENGLISH);
				if (ALLOWED_METHODS.contains(method)) {
					requestToUse = new HttpMethodRequestWrapper(request, method);
				}
			}

3、案例

package com.example.mvc;

import com.example.dao.EmployeeDao;
import com.example.entity.Employee;
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 java.util.Collection;
import java.util.Map;

/**
 * @Author SHKing
 * @Date 2021/11/28 16:28
 * @Version 1.0
 */

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;

    @RequestMapping(value = "/employee", method = RequestMethod.GET)
    public String getAllEmployee(Model model){
        Collection<Employee> employeeList = employeeDao.getAll();
        model.addAttribute("employeeList", employeeList);
        return "employee_list";
    }

    @RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
    public String deleteEmployee(@PathVariable("id") Integer id){
         employeeDao.delete(id);
         return "redirect:/employee";
    }

    @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 updateEmployee(Employee employee){
        employeeDao.save(employee);
        return "redirect:/employee";
    }
}
package com.example.dao;

import com.example.entity.Employee;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author SHKing
 * @Date 2021/11/28 16:34
 * @Version 1.0
 */

@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employeemap = null;

    static {
        employeemap = new HashMap<Integer, Employee>();
        employeemap.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1));
        employeemap.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1));
        employeemap.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0));
        employeemap.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0));
        employeemap.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++);
        }
        employeemap.put(employee.getId(), employee);
    }

    public Collection<Employee> getAll(){
        return  employeemap.values();
    }

    public Employee get(Integer id){
        return employeemap.get(id);
    }

    public void delete(Integer id){
        employeemap.remove(id);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值