關(guān)于SPRingMVC中的@ResquestMapping,@ResponseBody,@PathVariable,@ResquestParam三個(gè)注解的用法總是記不住,今天總結(jié)一下。
一、@ResquestMapping
先來(lái)說(shuō)一說(shuō)@ResquestMapping RequestMapping是一個(gè)用來(lái)處理請(qǐng)求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應(yīng)請(qǐng)求的方法都是以該地址作為父路徑。
例如下面的代碼:
@Controller//此處是作為類上的請(qǐng)求地址映射@RequestMapping("/hello")public class HelloMvcController { //此處是作為方法上的地址映射 @RequestMapping("/mvc") //localhost:8080/hello/mvc public String helloMvc(){ return "home"; }}@RequestMapping的屬性(分為三大類):
1、 value, method;
value: 指定請(qǐng)求的實(shí)際地址,指定的地址可以是URI Template 模式(后面將會(huì)說(shuō)明);
method: 指定請(qǐng)求的method類型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定處理請(qǐng)求的提交內(nèi)容類型(Content-Type),例如application/json, text/html;
produces: 指定返回的內(nèi)容類型,僅當(dāng)request請(qǐng)求頭中的(Accept)類型中包含該指定類型才返回;
3、 params,headers;
params: 指定request中必須包含某些參數(shù)值是,才讓該方法處理。
headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請(qǐng)求。
當(dāng)然,我們最常用的參數(shù)應(yīng)該就是value和method。例如:
//這里的value + method,value為訪問(wèn)方法的路徑,method是該方法的請(qǐng)求方式是get @RequestMapping(value = "/list", method = RequestMethod.GET) public String list(Model model) { // 獲取列表頁(yè) List<Seckill> list = seckillService.getSeckillList(); model.addAttribute("list", list); // list.jsp + model = ModelAndView return "list"; // WEB-INF/jsp/"list".jsp }還有一種用法是Produces屬性:如下: 在springMVC controller中返回json數(shù)據(jù)出現(xiàn)亂碼問(wèn)題,因?yàn)闆](méi)有進(jìn)行編碼,只需要簡(jiǎn)單的注解就可以了
// @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" }) @ResponseBody // 返回?cái)?shù)據(jù)類型封裝成json二、@ResponseBody
作用: 該注解用于將Controller的方法返回的對(duì)象,通過(guò)適當(dāng)?shù)腍ttpMessageConverter轉(zhuǎn)換為指定格式后,寫入到Response對(duì)象的body數(shù)據(jù)區(qū)。 使用時(shí)機(jī): 返回的數(shù)據(jù)不是html標(biāo)簽的頁(yè)面,而是其他某種格式的數(shù)據(jù)時(shí)(如json、xml等)使用;
@ResponseBody // 返回?cái)?shù)據(jù)類型封裝成json public SeckillResult<Exposer> exposer(@PathVariable Long seckillId) { SeckillResult<Exposer> result; try { Exposer exposer = seckillService.exportSeckillUrl(seckillId); result = new SeckillResult<Exposer>(true, exposer); } catch (Exception e) { logger.error(e.getMessage(), e); result = new SeckillResult<Exposer>(false, e.getMessage()); } return result; }在使用
<mvc:annotation-driven />標(biāo)簽配置時(shí),默認(rèn)配置了RequestMappingHandlerAdapter
ResourceHttpMessageConverter:負(fù)責(zé)讀取資源文件和寫出資源文件數(shù)據(jù);
FormHttpMessageConverter: 負(fù)責(zé)讀取form提交的數(shù)據(jù)(能讀取的數(shù)據(jù)格式為 application/x-www-form-urlencoded,不能讀取multipart/form-data格式數(shù)據(jù));負(fù)責(zé)寫入application/x-www-from-urlencoded和multipart/form-data格式的數(shù)據(jù);
MappingJacksonHttpMessageConverter: 負(fù)責(zé)讀取和寫入json格式的數(shù)據(jù);
SouceHttpMessageConverter: 負(fù)責(zé)讀取和寫入 xml 中javax.xml.transform.Source定義的數(shù)據(jù); Jaxb2RootElementHttpMessageConverter: 負(fù)責(zé)讀取和寫入xml 標(biāo)簽格式的數(shù)據(jù);
AtomFeedHttpMessageConverter: 負(fù)責(zé)讀取和寫入Atom格式的數(shù)據(jù); rssChannelHttpMessageConverter: 負(fù)責(zé)讀取和寫入RSS格式的數(shù)據(jù);
當(dāng)使用@RequestBody和@ResponseBody注解時(shí),RequestMappingHandlerAdapter就使用它們來(lái)進(jìn)行讀取或者寫入相應(yīng)格式的數(shù)據(jù)。
三、@PathVariable
當(dāng)使用@RequestMapping URI template 樣式映射時(shí), 即 /{seckillId}/detail, 這時(shí)的seckillId可通過(guò) @Pathvariable注解綁定它傳過(guò)來(lái)的值到方法的參數(shù)上。
@RequestMapping(value = "/{seckillId}/detail", method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId, Model model) { return "detail"; }@RequestMapping(value = “/{seckillId}/detail”, method = RequestMethod.GET)中的{seckillId}與@PathVariable Long seckillId 一 一 對(duì)應(yīng),按名匹配, 這是restful式風(fēng)格。 如果映射名稱有所不一,可以參考如下方式:
@RequestMapping(value = "/{seckillId}/detail", method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long Id, Model model) { return "detail"; }GET模式下,這里使用了@PathVariable綁定輸入?yún)?shù),非常適合Restful風(fēng)格。因?yàn)殡[藏了參數(shù)與路徑的關(guān)系,可以提升網(wǎng)站的安全性,靜態(tài)化頁(yè)面,降低惡意攻擊風(fēng)險(xiǎn)。
POST模式下,使用@RequestBody綁定請(qǐng)求對(duì)象,Spring會(huì)幫你進(jìn)行協(xié)議轉(zhuǎn)換,將Json、Xml協(xié)議轉(zhuǎn)換成你需要的對(duì)象。
四、@ResquestParam
在SpringMVC后臺(tái)控制層獲取參數(shù)的方式主要有兩種,一種是request.getParameter(“xx”),另外一種是用注解@RequestParam直接獲取。 A) 常用來(lái)處理簡(jiǎn)單類型的綁定,通過(guò)Request.getParameter() 獲取的String可直接轉(zhuǎn)換為簡(jiǎn)單類型的情況( String–> 簡(jiǎn)單類型的轉(zhuǎn)換操作由ConversionService配置的轉(zhuǎn)換器來(lái)完成);因?yàn)槭褂胷equest.getParameter()方式獲取參數(shù),所以可以處理get 方式中queryString的值,也可以處理post方式中 body data的值;
B)用來(lái)處理Content-Type: 為 application/x-www-form-urlencoded編碼的內(nèi)容,提交方式GET、POST;
C) 該注解有兩個(gè)屬性: value、required; value用來(lái)指定要傳入值的id名稱,required用來(lái)指示參數(shù)是否必須綁定; – value:參數(shù)名
– required:是否必須。默認(rèn)為 true, 表示請(qǐng)求參數(shù)中必須包含對(duì)應(yīng)的參數(shù),若不存在,將拋出異常
@RequestMapping(value = "/testRequestParam", method = RequestMethod.GET) public String test(@RequestParam String inputStr){ System.out.println(inputStr); return "index"; }參考博客:
http://blog.csdn.net/pnoter/article/details/49680789
http://blog.csdn.net/kobejayandy/article/details/12690041
http://snowolf.iteye.com/blog/1628861
http://blog.csdn.net/u010127245/article/details/51774074
http://825635381.iteye.com/blog/2196911
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注