日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring MVC常用注解,你会几个?

發(fā)布時間:2023/12/3 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC常用注解,你会几个? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉載自?Spring MVC常用注解,你會幾個?


常用注解

  • Controller

注解一個類表示控制器,Spring MVC會自動掃描標注了這個注解的類。

  • RequestMapping

請求路徑映射,可以標注類,也可以是方法,可以指定請求類型,默認不指定為全部接收。

  • RequestParam

放在參數(shù)前,表示只能接收參數(shù)a=b格式的數(shù)據(jù),即 Content-Typeapplication/x-www-form-urlencoded類型的內容。

  • RequestBody

放在參數(shù)前,表示參數(shù)從request body中獲取,而不是從地址欄獲取,所以這肯定是接收一個POST請求的非a=b格式的數(shù)據(jù),即 Content-Type不為 application/x-www-form-urlencoded類型的內容。

  • ResponseBody

放在方法上或者返回類型前,表示此方法返回的數(shù)據(jù)放在response body里面,而不是跳轉頁面。一般用于ajax請求,返回json數(shù)據(jù)。

  • RestController

這個是Controller和ResponseBody的組合注解,表示@Controller標識的類里面的所有返回參數(shù)都放在response body里面。

  • PathVariable

路徑綁定變量,用于綁定restful路徑上的變量。

  • @RequestHeader

放在方法參數(shù)前,用來獲取request header中的參數(shù)值。

  • @CookieValue;

放在方法參數(shù)前,用來獲取request header cookie中的參數(shù)值。

  • GetMapping PostMapping PutMapping.. *Mapping的是Spring4.3加入的新注解,表示特定的請求類型路徑映射,而不需要寫RequestMethod來指定請求類型。

演示

import org.dom4j.util.UserDataElement; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/test") public class TestController { ? ?@RequestMapping(value = "/get/{no}", method = RequestMethod.GET)@ResponseBodypublic Object get(@PathVariable("no") String no) {return new UserDataElement("");} ? ?@RequestMapping(value = "/save", method = RequestMethod.POST)public void save(@RequestBody UserDataElement user) { ? ?} }

總結

以上是生活随笔為你收集整理的Spring MVC常用注解,你会几个?的全部內容,希望文章能夠幫你解決所遇到的問題。

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