以前的文章springmvc使用注解声明控制器与请求映射有简单提到过控制器与请求映射,这一次就详细讲解一下SpringMVC
的REST
风格的四种请求方式及其使用方法。html
一、什么是Rest风格?
二、基于springmvc
实现REST风格的四种请求方式
三、post请求转换为delete
与put
请求
四、解决请求乱码问题
五、RequestMapping
注解的属性
@java
REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,能够下降开发的复杂性,提升系统的可伸缩性。web
简单来讲:使用URL定位资源,用HTTP动词(例如GET,POST,DELETE,DETC等)描述操做。spring
请求 | 说明 | 用于 | 例子 | 例子说明 |
---|---|---|---|---|
@GetMapping | 匹配GET方式的请求; | 通常用于读取数据 | /user/1 | 获取一号用户信息 |
@PostMapping | 匹配POST方式的请求; | 通常用于新增数据 | /user/1 | 新增一号用户 |
@PutMapping | 匹配PUT方式的请求; | 通常用于更新数据 | /user/1 | 修改一号用户 |
@DeleteMapping | 匹配DELETE方式的请求; | 通常用于删除数据 | /user/1 | 删除一号用户 |
也就是说,咱们再也不使用/user/getuser?user=1
、/user/deleteuser?user=1
等来区分使用者的行为操做,而是使用不一样的请求方式来描述行为。json
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
为例,该组合注解是@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)
的缩写。
因此咱们能够将以上代码简单的写成:mvc
@GetMapping("/rest") public String restGet(){ 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请求每每会报错找不到方法。app
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>
注意:编码的过滤器应该放在全部的过滤器前,不然不生效
属性名 | 类型 | 描述 |
---|---|---|
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,用于请求后的页面跳转。
<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>
结果: