javascript
Spring MVC常用注解,你会几个?
轉載自?Spring MVC常用注解,你會幾個?
常用注解
Controller
注解一個類表示控制器,Spring MVC會自動掃描標注了這個注解的類。
RequestMapping
請求路徑映射,可以標注類,也可以是方法,可以指定請求類型,默認不指定為全部接收。
RequestParam
放在參數前,表示只能接收參數a=b格式的數據,即 Content-Type為 application/x-www-form-urlencoded類型的內容。
RequestBody
放在參數前,表示參數從request body中獲取,而不是從地址欄獲取,所以這肯定是接收一個POST請求的非a=b格式的數據,即 Content-Type不為 application/x-www-form-urlencoded類型的內容。
ResponseBody
放在方法上或者返回類型前,表示此方法返回的數據放在response body里面,而不是跳轉頁面。一般用于ajax請求,返回json數據。
RestController
這個是Controller和ResponseBody的組合注解,表示@Controller標識的類里面的所有返回參數都放在response body里面。
PathVariable
路徑綁定變量,用于綁定restful路徑上的變量。
@RequestHeader
放在方法參數前,用來獲取request header中的參數值。
@CookieValue;
放在方法參數前,用來獲取request header cookie中的參數值。
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常用注解,你会几个?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想拯救者如何查看配置?
- 下一篇: 什么是Spring Boot?