springboot中restful風格請求的使用方法示例
restful風格
Rest風格支持(使用HTTP請求方式動詞來表示對資源的操作)
- 以前:/getUser 獲取用戶 /deleteUser 刪除用戶 /editUser 修改用戶 /saveUser 保存用戶
- 現(xiàn)在: /user GET-獲取用戶 DELETE-刪除用戶 PUT-修改用戶 POST-保存用戶
springboot中的使用
1.創(chuàng)建html表單頁面
因為html表單只支持發(fā)送get和post請求,所以當發(fā)送delete,put請求時,需要設定一個隱藏域,其name值必須為_method,value值為表單的請求方式(且delete,put的表單的method為post請求)。
用法: 表單method=post,隱藏域<input type="hidden" name="_method" value="PUT|DELETE">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
<form action="/user" method="get">
<input type="submit" value="GET提交">
</form>
<hr>
<form action="/user" method="post">
<input type="submit" value="POST提交">
</form>
<hr>
<form action="/user" method="post">
<input type="hidden" name="_method" value="DELETE"><br>
<input type="submit" value="DELETE提交">
</form>
<hr>
<form action="/user" method="post">
<input type="hidden" name="_method" value="PUT"><br>
<input type="submit" value="PUT提交">
</form>
</body>
</html>
2.在yml配置文件中開啟rest表單支持
# RestFul風格開啟,開啟支持表單的rest風格
spring:
mvc:
hiddenmethod:
filter:
enabled: true
3.編寫controller層及對應映射處理
package com.robin.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestTestController {
@RequestMapping(value = "/user",method = RequestMethod.GET)
public String getUser(){
return "GET user , 獲取用戶成功";
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public String saveUser(){
return "POST user, 保存用戶成功";
}
@RequestMapping(value = "/user",method = RequestMethod.DELETE)
public String delUser(){
return "DELETE user, 刪除用戶成功";
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String updateUser(){
return "PUT user, 修改用戶成功";
}
}4.啟動服務,逐個訪問

訪問成功,對同一請求/user實現(xiàn)了,不同方式提交的不同處理。




總結
到此這篇關于springboot中restful風格請求使用的文章就介紹到這了,更多相關springboot restful風格請求使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java?精煉解讀數(shù)據(jù)結構的順序表如何操作
程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要創(chuàng)建這種元素組,用變量記錄它們,傳進傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲區(qū)里,元素間的順序關系由它們的存儲順序自然表示2022-03-03

