restful风格案例
RestFul風格案例
- 理解
- 一、原則:通過四種不同的請求方式來表示CRUD操作
- 問題:頁面請求只支持get/post方式,并不支持其他方式,如何處理?
- 二、實際操作時代碼
- 1.html
- 2.handler
- 3.想要修改_method用其他的name替換,如何做?
- 解決:
- 實現(xiàn)思路:(重寫底層的規(guī)則)
理解
請求的路徑相同,根據(jù)method(提交方式)進行區(qū)分是調用哪個方法
一、原則:通過四種不同的請求方式來表示CRUD操作
請求方式有8中,現(xiàn)在只使用的是其中4種:
get【查詢】
post【保存/新增】
put 【修改】
delete【刪除】
問題:頁面請求只支持get/post方式,并不支持其他方式,如何處理?
解決:根據(jù)隱藏參數(shù)將post/get轉為其他幾種請求方式
post -------> RequestMethod.POST
get --------> RequestMethod.GET
post --------> delete-------> RequestMethod.DELETE
post --------> put----------->RequestMethod.PUT
<input type=“hidden” name="_method" value=“post/get/put/delete…”/>
注:ResutFul默認被底層禁用,需要開啟才能實現(xiàn)以上的轉換,否則請求方式都會為post/get
具體操作,在application.properties中設置
(以下代碼參照于package org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration類)
spring.mvc.hiddenmethod.filter.enabled=true
二、實際操作時代碼
1.html
代碼如下(示例):
注意:轉其他請求方式時form的method屬性用post,用get可能會無效
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><form action="/userManager" method="get"><input type="hidden" name="_method" value="get"/><input type="submit" value="get提交"/></form><form action="/userManager" method="post"><input type="hidden" name="_method" value="post"/><input type="submit" value="post提交"/></form><form action="/userManager" method="post"><input type="hidden" name="_method" value="put"/><input type="submit" value="put提交"/></form><form action="/userManager" method="post"><input type="hidden" name="_method" value="delete"/><input type="submit" value="delete提交"/></form> </body> </html>2.handler
代碼如下(示例):
@Controller public class UserManagerHandler {@GetMapping("/userManager")@ResponseBodyprivate String searchUserManager(){return "Search Data...";}@PostMapping("/userManager")@ResponseBodyprivate String savehUserManager(){return "Save Data...";}@PutMapping("/userManager")@ResponseBodyprivate String updateUserManager(){return "Update Data...";}@DeleteMapping("/userManager")@ResponseBodyprivate String deleteUserManager(){return "Delete Data...";} }3.想要修改_method用其他的name替換,如何做?
解決:
實現(xiàn)思路:(重寫底層的規(guī)則)
參照HiddenHttpMethodFilter類。將其放入容器中管理,通過此類中的setMethodParam("")方法進行覆蓋掉_method即可。具體步驟如下:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.HiddenHttpMethodFilter;/*** @author liucheng* @version 1.0* @date 2021-03-18 21:39* 1.創(chuàng)建一個配置bean*/ @Configuration(proxyBeanMethods = true) public class SpringBootConfigure {/*** 2.創(chuàng)建一個返回類型為HiddenHttpMethodFilter的方法,通過setXXX重新定義請求參數(shù)的規(guī)則* 加入@Bean將其放入容器中管理* @return HiddenHttpMethodFilter*/@Beanpublic HiddenHttpMethodFilter setMethod(){HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();hiddenHttpMethodFilter.setMethodParam("_methodNew");return hiddenHttpMethodFilter;} }此時,HTML中原來的name="_method"已經失去了作用,需改為“_methodNew”
<input type=“hidden” name=_methodNew" value=“put”/>
總結
以上是生活随笔為你收集整理的restful风格案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springBoot静态资源优先级)
- 下一篇: springboot数据访问基本操作步骤