RESTful 基础

一、概念

REST(Representational State Transfer,表述性状态转移)
​ REST并不是一种创新技术,它指的是一组架构约束条件和原则
​ 符合REST的约束条件和原则的架构,就称它为RESTful架构

  • RESTful核心内容
  1. 资源与URI
  2. 资源的表述
  3. 状态转移
  • RESTful架构特点
  1. 统一了客户端访问资源的接口
  2. url 更加简洁,易于理解,便于扩展
  3. 有利于不同系统之间的资源共享
  • RESTful 四种基本操作
  1. GET: 获取资源
  2. POST: 新建资源
  3. PUT: 修改资源
  4. DELETE: 删除资源
  • RESTful 请求方式
  1. get 请求

    http:/localhost:8080/id method=‘get’

  2. post 请求

    http://localhost:8080/course method=‘post’

  3. delete 请求

    http://localhost:8080/id method=‘delete’

  4. put 请求

    http://localhost:8080/course method=‘put’

二、RESTful 实现

1、实体类
public class Course {
    private int id;
    private String name;
    private double price;

    public int getId() {return id;}
    public void setId(int id) {this.id = id;}

    public String getName() {return name;}
    public void setName(String name) {this.name = name;}

    public double getPrice() {return price;}
    public void setPrice(double price) {this.price = price;}
}
2、数据获取类
@Repository
public class CourseDAO {
    private Map<Integer,Course> courses = new HashMap<Integer,Course>();

    // 新增课程
    public void add(Course course){
        courses.put(course.getId(),course);
    }
    // 查询全部课程
    public Collection<Course> getAll(){
        return courses.values();
    }
    // 通过id查询课程
    public Course getById(int id){
        return courses.get(id);
    }
    // 修改课程
    public void update(Course course){
        courses.put(course.getId(),course);
    }
    // 删除课程
    public void deleteById(int id){
        courses.remove(id);
    }
}
3、web.xml + html 实现Post 转化 put、delete
  • web.xml 中添加如下过滤器
  <!-- 该过滤器将post请求 转化为 put 请求、delete 请求 -->
  <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>
  • 在 form 表单中添加隐藏域,其 name 属性设置为 “_method” ,值 value 设为转换的 “PUT”、"DELETE"请求
<form action="${pageContext.request.contextPath}/delete/${course.id}" method="post">
    <button class="btn" type="submit">
        <%-- 设置隐藏域 结合过滤器将Post请求转换为put请求 --%>
        <input type="hidden" name="_method" value="DELETE"/>
        <span class="glyphicon glyphicon-trash">删除</span>
    </button>
</form>
  • springmvc.xml 消息转换器等配置
<mvc:annotation-driven >
    <!-- 消息转换器 -->
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<!-- 配置自动扫描 -->
<context:component-scan base-package="com.moc"></context:component-scan>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
4、控制器类处理 RESTful 风格的响应
@Controller
public class CourseController {
    @Autowired
    private CourseDAO courseDAO;

    // 添加课程 (只有 Post 请求才能进入该业务方法)
    @PostMapping(value = "/add")
    public String add(Course course) {
        courseDAO.add(course);
        return "redirect:/getAll";   // 重定向 请求到 /getAll
    }

    // 查询全部课程(只有 Get 请求才能进入该业务方法)
    @GetMapping(value = "getAll")
    public ModelAndView getAll() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("courses", courseDAO.getAll());
        return modelAndView;
    }

    // 通过 id 查询课程(Get 请求进入该业务方法)
    @GetMapping(value = "/getById/{id}")   // id 是传入该业务方法的参数
    public ModelAndView getById(@PathVariable(value = "id") int id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("edit");
        modelAndView.addObject("course", courseDAO.getById(id));
        return modelAndView;
    }

    // 修改课程(Put 请求进入该业务方法)
    @PutMapping(value = "/update")
    public String update(Course course) {
        courseDAO.update(course);
        return "redirect:/getAll";
    }

    // 删除课程(Delete 请求进入该业务方法)
    @DeleteMapping(value = "/delete/{id}")
    public String delete(@PathVariable(value = "id") int id) {
        courseDAO.deleteById(id);
        return "redirect:/getAll";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值