Spring中@RequestMapping、@RestController和Postman
1.@RequestMapping 注解介紹
@RequestMapping
是 Spring Web MVC 引用程序中最常被用到的注解之一,它是用來注冊接口的路由映射的,表示服務(wù)收到請求時,路徑為 /sayHello
的請求就會調(diào)用 sayHi
這個方法的代碼
路由映射:當(dāng)用戶訪問一個 URL 時,將用戶的請求對應(yīng)到程序中某個類的某個方法的過程就叫路由映射
既然 @RequestMapping
已經(jīng)可以達到我們的目的了,我們?yōu)槭裁催€要加 @RestController
呢?
我們把 @RestController
去掉,再來訪問一次
可以看到,程序報了 404
,找不到該頁面,這就是 @RestController
起到的作用
一個項目中,會有很多類,每個類可能會有很多的方法,Spring 程序怎么知道要執(zhí)行哪個方法呢?
Spring 會對所有的類進行掃描,如果類加了注解 @RestController
,Spring 才會去看這個類里面的方法有沒有加 @RequestMapping
這個注解
2. @RequestMapping 使用
@RequestMapping
既可修飾方法,也可修飾類。 當(dāng)修飾類和方法時,訪問的地址是類路徑+方法路徑
@RequestMapping
標識一個類:設(shè)置映射請求的請求路徑的初識信息@RequestMapping
標識一個方法:設(shè)置映射請求請求路徑的具體信息
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/user") @RestController public class UserController { @RequestMapping("/sayHello") public String sayHi() { return "hello, spring mvc"; } }
訪問地址:
http://127.0.0.1:8080/user/sayHello
注意:@RequestMapping
的 URL 路徑最前面加不加 /
都可以,Spring 程序啟動時,會進行判斷,如果前面沒有 /
,Spring 會拼接上一個 /
通常情況下,我們加上 /
,@RequestMapping
的 URL 路徑也可以是多層的,最終訪問時,依然是類路徑+方法路徑
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/user/m1") @RestController public class UserController { @RequestMapping("/sayHello") public String sayHi() { return "hello, spring mvc"; } }
3. @RequestMapping 是 GET 還是 POST 請求?
我們來測試一下就知道了
GET 請求
瀏覽器發(fā)送的請求類型都是 GET
,通過以上案例,可以看出來 @RequestMapping
支持 GET
請求
POST 請求
我們通過 form 表單來構(gòu)造請求:
創(chuàng)建 test.html
,HTML代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="user/sayHello" method="post"> <input type="submit" value="提交"> </form> </body> </html>
前端代碼放在 static
目錄下,訪問方式為:
http://127.0.0.1:8080/test.html
如果有多層目錄,訪問鏈接從 static 目錄開始寫
如上圖,訪問鏈接為:127.0.0.1:8080/html/test.html
從運行結(jié)果可以看出:@RequestMapping
既支持 GET
請求,又支持 POST
請求。同理,也支持其他的請求方式,那如何指定 GET
或者 POST
類型呢?
指定 GET/POST 方法類型
我們可以顯示指定的 @RequestMapping
來接收 POST
的情況,如下所示:
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @RequestMapping(value = "/getRequest", method = RequestMethod.POST) public String sayHi() { return "hello, spring mvc"; } }
2. Postman 介紹
從上面的案例中,發(fā)現(xiàn)了一個新的問題,就是我們測試后端方法時,還需要去寫前端代碼。這對我們來說,是一件麻煩又痛苦的事情。
隨著互聯(lián)網(wǎng)的發(fā)展,也隨著項目難度的增加,企業(yè)也按照開發(fā)的功能,把人員拆分成了不同的團隊。界面顯示交給“前端開發(fā)工程師”,業(yè)務(wù)邏輯的實現(xiàn)交給了“后端開發(fā)工程師”。后端開發(fā)工程師,不要求也不需要掌握前端的技能了。
那后端開發(fā)工程師如何測試自己的程序呢?使用專業(yè)的接口測試工具—— Postman
1. 創(chuàng)建請求
界面介紹
2. 傳參介紹
1. 普通傳參
也就是通過查詢字符串來傳參
學(xué)習(xí) HTTP 的時候,我們通過 URL 來訪問互聯(lián)網(wǎng)上的某一個資源,URL 的格式如下:
其中,查詢字符串就是請求的參數(shù)
2. form-data
完整表示為:multipart/form-data
。表單提交的數(shù)據(jù),在 form
標簽中加上 enctyped="multipart/form-data"
,通常用于提交圖片/文件。對應(yīng) Content-Type: multipart/form-data
3. x-www-form-urlencoded
form
表單,對應(yīng) Content-Type: application/x-www-from-urlencoded
4. raw
可以上傳任意格式的文本,可以上傳 text、json、xml、html 等
到此這篇關(guān)于Spring中@RequestMapping、@RestController和Postman的文章就介紹到這了,更多相關(guān)Spring @RequestMapping @RestController Postman內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中ApplicationEvent的使用步驟詳解
ApplicationEvent類似于MQ,是Spring提供的一種發(fā)布訂閱模式的事件處理方式,本文給大家介紹SpringBoot中ApplicationEvent的使用步驟詳解,感興趣的朋友跟隨小編一起看看吧2024-04-04SpringBoot項目啟動后自動加載系統(tǒng)配置的多種實現(xiàn)方式
這篇文章主要介紹了SpringBoot項目啟動后自動加載系統(tǒng)配置的多種實現(xiàn)方式,并通過代碼示例講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-01-01