天天看点

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

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

概述

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

你能get的知识点

1、什么是Rest风格?

2、基于springmvc实现REST风格的四种请求方式

3、post请求转换为delete与put请求

4、解决请求乱码问题

5、RequestMapping注解的属性

@

目录

壹:rest风格

一:什么是Rest风格?

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

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

一:@GetMapping请求

二:@PostMapping请求

三:@DeleteMapping请求

四:@PutMapping请求

叁:问题

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

二:解决请求乱码问题

肆:RequestMapping注解的属性

伍:测试

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

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

请求 说明 用于 例子 例子说明

@GetMapping 匹配GET方式的请求; 一般用于读取数据 /user/1 获取一号用户信息

@PostMapping 匹配POST方式的请求; 一般用于新增数据 /user/1 新增一号用户

@PutMapping 匹配PUT方式的请求; 一般用于更新数据 /user/1 修改一号用户

@DeleteMapping 匹配DELETE方式的请求; 一般用于删除数据 /user/1 删除一号用户

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

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为例,该组合注解是@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")

System.out.println("Get请求,hello.....");
return "hello";           
@PostMapping("/rest")
public String restPost(){
    System.out.println("Post请求,hello.....");
    return "hello";
}           
@DeleteMapping("/rest")
public String restDelete(){
    System.out.println("Delete请求,hello.....");
    return "redirect:rest";
}           
@PutMapping("/rest")
public String restPut(){
    System.out.println("Put请求,hello.....");
    return "redirect:rest";
}           

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

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

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

<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>           
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>           

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

<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-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>           

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

属性名 类型 描述

name String 可选属性,用于为映射地址指定别名。

value String[] 可选属性,同时也是默认属性,用于映射一-个请求和一种方法,可以标注在一-个方法或一个类上。

method RequestMethod[] 可选属性,用于指定该方法用于处理哪种类型的请求方式,其请求方式包括GET、POST、HEAD、OPTIONS、PUT、PATCH、DELETE和TRACE例如method=RequestMethod.GET表示只支持GET请求,如果需要支持多个请求方式则需要通过{}写成数组的形式,并且多个请求方式之间是有英文逗号分隔。

params String[] 可选属性,用于指定Request中必须包含某些参数的值,.才可以通过其标注的方法处理。

headers String[] 可选属性,用于指定Request中必须包含某些指定的header的值,才可以通过其标注的方法处理。。

consumes String[] 可选属性,用于指定处理请求的提交内容类型(Content-type),比如application/json,text/html等。

produces String[] 可选属性,用于指定返回的内容类型,返回的内容类型必,须是request请求头(Accept)中所包含的类型。

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

例如,下面两种标注的含义相同:

@RequestMapping(value="/rest")

@RequestMapping("/rest")

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

各种请求

hello Get请求
<button type="submit">hello Post请求</button>           
<input type="hidden" name="_method" value="delete">
<button type="submit">hello Delete请求</button>           
<input type="hidden" name="_method" value="put">
<button type="submit">hello put请求</button>           

结果:

原文地址

https://www.cnblogs.com/lomtom/p/12591836.html