日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

@GetMapping和@PostMapping详解

發(fā)布時間:2025/3/12 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 @GetMapping和@PostMapping详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

首先要了解一下@RequestMapping注解。

@RequestMapping用于映射url到控制器類的一個特定處理程序方法。可用于方法或者類上面。也就是可以通過url找到對應(yīng)的方法。

@RequestMapping有8個屬性。

  • value:指定請求的實際地址。

  • method:指定請求的method類型(GET,POST,PUT,DELETE)等。

  • consumes:指定處理請求的提交內(nèi)容類型(Context-Type)。

  • produces:指定返回的內(nèi)容類型,還可以設(shè)置返回值的字符編碼。

  • params:指定request中必須包含某些參數(shù)值,才讓該方法處理。

  • headers:指定request中必須包含某些指定的header值,才讓該方法處理請求。

@getMapping與@postMapping是組合注解。

@getMapping = @requestMapping(method = RequestMethod.GET)。

@postMapping = @requestMapping(method = RequestMethod.POST)。

@GetMapping是一個組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫。該注解將HTTP Get 映射到 特定的處理方法上。

Difference between @GetMapping & @RequestMapping:

@GetMapping does not support the consumes attribute of @RequestMapping.

SpringBoot 中@GetMapping和@PostMapping測試同一方法,為什么用@Get類型的注解可以訪問成功,而@PostMapping用瀏覽器訪問失敗

@Controller public class JspController {@PostMapping("/index")public String index(Model model){model.addAttribute("msg","springboot訪問JSP頁面");return "index";}}

用@PostMapping注解,在瀏覽器輸入訪問地址時,會出現(xiàn)

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Feb 20 09:50:06 CST 2019

There was an unexpected error (type=Method Not Allowed, status=405).

Request method ‘GET’ not supported

而用@GetMapping注解,可以訪問成功

通過瀏覽器的地址欄輸入地址,所訪問的URL都是get請求,因此如果以post定義方法,那么由于請求與實現(xiàn)的不一致,會返回405錯誤。

瀏覽器發(fā)送Get請求有

1.直接在地址欄中輸入地址

2.點擊鏈接

3.表單默認提交方式

瀏覽器發(fā)送post請求有:

1.將表單的method設(shè)置為post

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的@GetMapping和@PostMapping详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。