一、@RequestMapping属性
@RequestMapping中有六个属性分别是:
value: 指定请求的实际地址,指定的地址可以是URI Template 模式;
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
二、value、method的使用
1)在类上使用
表示此类拦截的根路径为/appointments同时不带任何属性的时候其值就是value的值
@Controller
@RequestMapping("/appointments")
publicclassAppointmentsController{
privateAppointmentBook appointmentBook;
@Autowired
publicAppointmentsController(AppointmentBook appointmentBook){
this.appointmentBook = appointmentBook;
}
@RequestMapping(method =RequestMethod.GET)
publicMap<String,Appointment> get(){
return appointmentBook.getAppointmentsForToday();
}
@RequestMapping(value="/{day}", method =RequestMethod.GET)
publicMap<String,Appointment> getForDay(@PathVariable@DateTimeFormat(iso=ISO.DATE)Date day,Model model){
return appointmentBook.getAppointmentsForDay(day);
}
@RequestMapping(value="/new", method =RequestMethod.GET)
publicAppointmentForm getNewForm(){
returnnewAppointmentForm();
}
@RequestMapping(method =RequestMethod.POST)
publicString add(@ValidAppointmentForm appointment,BindingResult result){
if(result.hasErrors()){
return"appointments/new";
}
appointmentBook.addAppointment(appointment);
return"redirect:/appointments";
}
}
2)在方法上使用 只有value值
@RequestMapping(value="/departments")
publicString simplePattern(){
System.out.println("simplePattern method was called");
return"someResult";
}
则访问http://localhost/xxxx/departments的时候,会调用 simplePattern方法了
3)参数绑定-不用@RequestParam
//商品信息修改提交
@RequestMapping("/editItemsSubmit")
publicString editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom)throwsException{
//调用service更新商品信息,页面需要将商品信息传到此方法
itemsService.updateItems(id, itemsCustom);
//重定向到商品查询列表
// return "redirect:queryItems.action";
//页面转发
//return "forward:queryItems.action";
return"success";
}
如果不使用@RequestParam,要求request传入参数名称和controller方法的形参名称一致,方可绑定成功。
形如这样的访问形式:
/departments?departmentId=23就可以触发访问findDepatment方法了
4)参数绑定-使用@RequestParam
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
//@RequestParam里边指定request传入参数名称和形参进行绑定。
//通过required属性指定参数是否必须要传入
//通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定。
publicString editItems(Model model,@RequestParam(value="id",required=true)Integer items_id)throwsException{
//调用service根据商品id查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
//通过形参中的model将model数据传到页面
//相当于modelAndView.addObject方法
model.addAttribute("itemsCustom", itemsCustom);
return"items/editItems";
}
5)REST风格的参数
@RequestMapping(value="/departments/{departmentId}")
publicString findDepatment(@PathVariableString departmentId){
System.out.println("Find department with ID: "+ departmentId);
return"someResult";
}
形如REST风格的地址访问,比如: /departments/23,其中用(@PathVariable接收rest风格的参数
6)REST风格的参数绑定形式之2
@RequestMapping(value="/departments/{departmentId}")
publicString findDepatmentAlternative(
@PathVariable("departmentId")String someDepartmentId){
System.out.println("Find department with ID: "+ someDepartmentId);
return"someResult";
}
这个有点不同,就是接收形如/departments/23的URL访问,把23作为传入的departmetnId,,但是在实际的方法findDepatmentAlternative中,使用
@PathVariable("departmentId") String someDepartmentId,将其绑定为 someDepartmentId,所以这里someDepartmentId为23
@PathVariable("departmentId") String someDepartmentId,将其绑定为 someDepartmentId,所以这里someDepartmentId为23
7)url中同时绑定多个参数
@RequestMapping(value="/departments/{departmentId}/employees/{employeeId}")
publicString findEmployee(
@PathVariableString departmentId,
@PathVariableString employeeId){
System.out.println("Find employee with ID: "+ employeeId +
" from department: "+ departmentId);
return"someResult";
}
8)支持正则表达式
@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}")
publicString regularExpression(
@PathVariableString textualPart,
@PathVariableString numericPart){
System.out.println("Textual part: "+ textualPart +
", numeric part: "+ numericPart);
return"someResult";
}
比如如下的URL:/sometext.123,则输出:
Textual part: sometext, numeric part: 123.
Textual part: sometext, numeric part: 123.
三、consumes、produces 示例
cousumes的样例:
@Controller
@RequestMapping(value ="/pets", method =RequestMethod.POST, consumes="application/json")
publicvoid addPet(@RequestBodyPet pet,Model model){
// implementation omitted
}
方法仅处理request Content-Type为“application/json”类型的请求。
produces的样例:
@Controller
@RequestMapping(value ="/pets/{petId}", method =RequestMethod.GET, produces="application/json")
@ResponseBody
publicPet getPet(@PathVariableString petId,Model model){
// implementation omitted
}
方法仅处理request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json;
四、params、headers 示例
params的样例:
@Controller
@RequestMapping("/owners/{ownerId}")
publicclassRelativePathUriTemplateController{
@RequestMapping(value ="/pets/{petId}", method =RequestMethod.GET, params="myParam=myValue")
publicvoid findPet(@PathVariableString ownerId,@PathVariableString petId,Model model){
// implementation omitted
}
}
仅处理请求中包含了名为“myParam”,值为“myValue”的请求;
headers的样例:
@Controller
@RequestMapping("/owners/{ownerId}")
publicclassRelativePathUriTemplateController{
@RequestMapping(value ="/pets", method =RequestMethod.GET, headers="Referer=http://www.ifeng.com/")
publicvoid findPet(@PathVariableString ownerId,@PathVariableString petId,Model model){
// implementation omitted
}
}
仅处理request的header中包含了指定“Refer”请求头和对应值为“
http://www.ifeng.com/
”的请求;