SpringMVC(四):实现Restful风格的增删该查

SpringMVC实现Restful风格的增删该查

文章目录

    • SpringMVC实现Restful风格的增删该查
      • 1. REST概述
        • restful的特性
      • 2. 实现restful风格增删改查
        • 2.1 配置HiddenHttpMethodFilter
        • 2.2 前端页面写restful请求
        • 2.3 controller处理resful请求

1. REST概述

​ REST(英文:Representational State Transfer,简称 REST)描述了一个架构样式的网络系统,比如 web 应用程序。它首次出现在 2000 年 Roy Fielding 的博士论文中,他是 HTTP 规范的主要编写者之一。在目前主流的三种 Web 服务交互方案中,REST 相比于 SOAP(Simple Object Access protocol,简单对象访问协议)以及 XML-RPC 更加简单明了,无论是对 URL 的处理还是对 Payload 的编码,REST 都倾向于用更加简单轻量的方法设计和实现。值得注意的是 REST 并没有一个明确的标准,而更像是一种设计的风格。
​ REST 其核心价值在于如何设计出符合 REST 风格的网络接口。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

restful的特性

1. 资源( Resources)

​ 网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。可以用一个 URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。要获取这个资源,访问它的 URI 就可以,因此URI 即为每一个资源的独一无二的识别符。

2. 表现层( Representation)

​ 把资源具体呈现出来的形式,叫做它的表现层 ( Representation)。比如,文本可以用 txt 格式表现,也可以用 HTML 格式、 XML 格式、 JSON 格式表现,甚至可以采用二进制格式。

3. 状态转化( State Transfer)

​ 每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP 协议,是一个无状态协议,即所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段, 让服务器端发生“状态转化” ( State Transfer)。而这种转化是建立在表现层之上的,所以就是 “表现层状态转化” 。具体说,就是 HTTP 协议里面,四个表示操作方式的动词: GET 、POST 、 PUT、DELETE。它们分别对应四种基本操作: GET 用来获取资源, POST 用来新建资源, PUT用来更新资源, DELETE 用来删除资源

restful示例
/account/1 HTTP GET : 得到 id = 1 的 account
/account/1 HTTP DELETE: 删除 id = 1 的 account
/account/1 HTTP PUT: 更新 id = 1 的 account
/account HTTP POST: 新增 account

2. 实现restful风格增删改查

2.1 配置HiddenHttpMethodFilter

​ 由于浏览器 form 表单只支持 GET 与 POST 请求,而 DELETE、 PUT 等 method 并不支持, Spring3.0 添加了一个过滤器,可以将浏览器请求改为指定的请求方式,发送给我们的控制器方法,使得支持 GET、 POST、 PUT 与 DELETE 请求。过滤器:org.springframework.web.filter.HiddenHttpMethodFilter

  • 配置tomcat的web.xml

    让tomcat支持put和delete方法

<init-param>    
    <param-name>readonly</param-name>    
    <param-value>false</param-value>   
</init-param>  
  • 在web.xml中配置过滤器(其他spring配置省略)


<filter>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter-mapping>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <url-pattern>/*url-pattern>
filter-mapping>

2.2 前端页面写restful请求

​ 每个form表单增加一个隐藏域,name=“_method”, value=“请求方式”。


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>restful请求title>
head>
<body>

<form action="http://localhost:8080/account/100" method="post">
    <input type="hidden" name="_method" value="post">
    <input type="submit" value="新增">
form>

<form action="http://localhost:8080/account/200" method="post">
    <input type="hidden" name="_method" value="get">
    <input type="submit" value="查询">
form>

<form action="http://localhost:8080/account/300" method="post">
    <input type="hidden" name="_method" value="put">
    <input type="submit" value="修改">
form>


<form action="http://localhost:8080/account/400/500" method="post">
    <input type="hidden" name="_method" value="delete">
    <input type="submit" value="删除">
form>
body>
html>

2.3 controller处理resful请求

​ **注意:**put和delete请求方式不能返回逻辑视图,因此用@ResponseBody返回json字符串。

@PathVariable注解

​ 通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。

​ 映射 URL 绑定的占位符​带占位符的 URLSpring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义。

package com.zmysna.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class AccountController {

    @RequestMapping(value = "/account/{id}",method = RequestMethod.POST)
    public String save(@PathVariable("id") String id){
        System.out.println(id);
        System.out.println("新增");
        return "success";
    }

    @RequestMapping(value = "/account/{id}",method = RequestMethod.GET)
    public String get(@PathVariable("id") String id){
        System.out.println(id);
        System.out.println("查询");
        return "success";
    }

    @RequestMapping(value = "/account/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public String update(@PathVariable("id") String id){
        System.out.println(id);
        System.out.println("修改");
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/account/{fid}/{sid}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("fid") String fid, @PathVariable("sid") String sid){
        System.out.println(fid+":"+sid);
        System.out.println("删除");
        return "success";
    }
}

你可能感兴趣的:(java,SSM)