Article

【spring springmvc】这里有你想要的SpringMVC的REST风格的四种请求方式

围绕工程实践、云原生与开发经验的深度整理。

【spring springmvc】这里有你想要的SpringMVC的REST风格的四种请求方式

概述

之前的文章springmvc 使用注解声明控制器与请求映射 🔗有简单提到过控制器与请求映射,这一次就详细讲解一下SpringMVCREST风格的四种请求方式及其使用方法。

你能 get 的知识点

1、什么是 Rest 风格? 2、基于springmvc实现 REST 风格的四种请求方式 3、post 请求转换为deleteput请求 4、解决请求乱码问题 5、RequestMapping注解的属性

壹:rest 风格

一:什么是 Rest 风格?

REST 即表述性状态传递(英文:Representational State Transfer,简称 REST)是 Roy Fielding 博士在 2000 年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。

简单来说:使用 URL 定位资源,用 HTTP 动词(例如 GET,POST,DELETE,DETC 等)描述操作。

二:REST 风格的四种请求方式

请求说明用于例子例子说明
@GetMapping匹配 GET 方式的请求;一般用于读取数据/user/1获取一号用户信息
@PostMapping匹配 POST 方式的请求;一般用于新增数据/user/1新增一号用户
@PutMapping匹配 PUT 方式的请求;一般用于更新数据/user/1修改一号用户
@DeleteMapping匹配 DELETE 方式的请求;一般用于删除数据/user/1删除一号用户

也就是说,我们不再使用/user/getuser?user=1/user/deleteuser?user=1等来区分使用者的行为操作,而是使用不同的请求方式来描述行为。

贰:基于springmvc实现 REST 风格的四种请求方式

Spring 框架的 4.3 版本后,引入了新的组合注解,来帮助简化常用的HTTP方法的映射,并更好的表达被注解方法的语义。

@GetMapping = @requestMapping(method = RequestMethod.GET)。
@PostMapping = @requestMapping(method = RequestMethod.POST)。
@DeleteMapping = @requestMapping(method = RequestMethod.DELETE)。
@PutMapping = @requestMapping(method = RequestMethod.PuT)。

一:@GetMapping 请求

@GetMapping为例,该组合注解是@RequestMapping(method = RequestMethod.GET)的缩写,它会将 HTTP GET 请求映射到特定的处理方法上。

RequestMapping后所有属性都是可选的,但其默认属性是value。当value是其唯一属性时,可以省略属性名。

@RequestMapping(value = "/rest",method = RequestMethod.GET)
public String restGet(){
	System.out.println("Get请求,hello.....");
    return "hello";
}

@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。 所以我们可以将以上代码简单的写成:

@GetMapping("/rest")
public String restGet(){
    System.out.println("Get请求,hello.....");
    return "hello";
}

二:@PostMapping 请求

    @PostMapping("/rest")
    public String restPost(){
        System.out.println("Post请求,hello.....");
        return "hello";
    }

三:@DeleteMapping 请求

    @DeleteMapping("/rest")
    public String restDelete(){
        System.out.println("Delete请求,hello.....");
        return "redirect:rest";
    }

四:@PutMapping 请求

    @PutMapping("/rest")
    public String restPut(){
        System.out.println("Put请求,hello.....");
        return "redirect:rest";
    }

叁:问题

一:post 请求转换为 delete 与 put 请求

由于 form 表单只支持 GET 和 POST 请求,而不支持 DELETE 和 PUT 等请求方式,所以我们实现 delete 和 put 请求往往会报错找不到方法。

Spring 提供了一个过滤器HiddenHttpMethodFilter,可以将 DELETE 和 PUT 请求转换为标准的 HTTP 方式,即能将 POST 请求转为 DELETE 或 PUT 请求。

具体实现:在 web.xml 文件中配置过滤器 HiddenHttpMethodFilter

<!--使用Rest风格的URI,将页面普通的post请求转为delete或者put请求-->
  <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>

注意:delete 和 put 请求最好使用 Redirect(重定向),不然会报 404 错误。

二:解决请求乱码问题

 <!--  解决乱码问题-->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

注意:编码的过滤器应该放在所有的过滤器前,否则不生效

肆:RequestMapping 注解的属性

属性名类型描述
nameString可选属性,用于为映射地址指定别名。
valueString[]可选属性,同时也是默认属性,用于映射一-个请求和一种方法,可以标注在一-个方法或一个类上。
methodRequestMethod[]可选属性,用于指定该方法用于处理哪种类型的请求方式,其请求方式包括 GET、POST、HEAD、OPTIONS、PUT、PATCH、DELETE 和 TRACE 例如 method=RequestMethod.GET 表示只支持 GET 请求,如果需要支持多个请求方式则需要通过{}写成数组的形式,并且多个请求方式之间是有英文逗号分隔。
paramsString[]可选属性,用于指定 Request 中必须包含某些参数的值,.才可以通过其标注的方法处理。
headersString[]可选属性,用于指定 Request 中必须包含某些指定的 header 的值,才可以通过其标注的方法处理。。
consumesString[]可选属性,用于指定处理请求的提交内容类型(Content-type),比如 application/json,text/html 等。
producesString[]可选属性,用于指定返回的内容类型,返回的内容类型必,须是 request 请求头(Accept)中所包含的类型。

表中所有属性都是可选的,但其默认属性是 value。当 value 是其唯一属性时, 可以省略属性名。 例如,下面两种标注的含义相同: @RequestMapping(value=“/rest”) @RequestMapping(“/rest”)

伍:测试

新建 index.html 文件,加入以下代码。新建 hello.html,用于请求后的页面跳转。

<h2>各种请求</h2>
<a href="rest">hello Get请求</a>
<form action="rest" method="post">
  <button type="submit">hello Post请求</button>
</form>
<form action="rest" method="post">
  <input type="hidden" name="_method" value="delete" />
  <button type="submit">hello Delete请求</button>
</form>
<form action="rest" method="post">
  <input type="hidden" name="_method" value="put" />
  <button type="submit">hello put请求</button>
</form>

结果:

标题:【spring springmvc】这里有你想要的SpringMVC的REST风格的四种请求方式

作者:lomtom

链接:https://lomtom.cn /spring-springmvc这里有你想要的springmvc的rest风格的四种请求方式