您的位置:首页 > 移动开发

springMVC的@RequestMapping

2015-09-17 23:15 316 查看
@RequestMapping

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为基路径。

各个属性含义:

1.value:

请求的uri,类型是String[],可以指定一个string数组表示匹配数组里面的uri字符串的请求会执行该方法。

例如:

@RequestMapping(value={"/user/list","/user"})

publicStringuserList(){

...

return"user/userList";

}

2.method:指定请求的method类型,GET、POST、PUT、DELETE等;

3.params:指定request中必须包含某些参数值是,才让该方法处理。同一个url,只要params不同,sringmvc是会区分匹配的。


4.headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。

@Controller
@RequestMapping("/owners/{ownerId}")
publicclassRelativePathUriTemplateController{

@RequestMapping(value="/pets",method=RequestMethod.GET,headers="Referer=http://www.xx.com/")
publicvoidfindPet(@PathVariableStringownerId,@PathVariableStringpetId,Modelmodel){
//implementationomitted
}
}


仅处理request的header中包含了指定“Refer”请求头和对应值为“
http://www.xx.com/
”的请求;

5.consumes:指定处理请求的提交内容类型(Content-Type),例如application/json,text/html;

@Controller
@RequestMapping(value="/pets",method=RequestMethod.POST,consumes="application/json")
publicvoidaddPet(@RequestBodyPetpet,Modelmodel){
//implementationomitted
}


方法仅处理requestContent-Type为“application/json”类型的请求

6.produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

@Controller
@RequestMapping(value="/pets/{petId}",method=RequestMethod.GET,produces="application/json")
@ResponseBody
publicPetgetPet(@PathVariableStringpetId,Modelmodel){
//implementationomitted
}


方法仅处理request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: