zoukankan      html  css  js  c++  java
  • @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射

    简介:

    1、 value, method;

    2、 consumes,produces;

    3、 params,headers;

    示例:

    1、value  / method 示例

    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. @Controller  
    2. @RequestMapping("/appointments")  
    3. public class AppointmentsController {  
    4.   
    5.     private AppointmentBook appointmentBook;  
    6.       
    7.     @Autowired  
    8.     public AppointmentsController(AppointmentBook appointmentBook) {  
    9.         this.appointmentBook = appointmentBook;  
    10.     }  
    11.   
    12.     @RequestMapping(method = RequestMethod.GET)  
    13.     public Map<String, Appointment> get() {  
    14.         return appointmentBook.getAppointmentsForToday();  
    15.     }  
    16.   
    17.     @RequestMapping(value="/{day}", method = RequestMethod.GET)  
    18.     public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {  
    19.         return appointmentBook.getAppointmentsForDay(day);  
    20.     }  
    21.   
    22.     @RequestMapping(value="/new", method = RequestMethod.GET)  
    23.     public AppointmentForm getNewForm() {  
    24.         return new AppointmentForm();  
    25.     }  
    26.   
    27.     @RequestMapping(method = RequestMethod.POST)  
    28.     public String add(@Valid AppointmentForm appointment, BindingResult result) {  
    29.         if (result.hasErrors()) {  
    30.             return "appointments/new";  
    31.         }  
    32.         appointmentBook.addAppointment(appointment);  
    33.         return "redirect:/appointments";  
    34.     }  
    35. }  
    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. @RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)  
    2. public String findOwner(@PathVariable String ownerId, Model model) {  
    3.   Owner owner = ownerService.findOwner(ownerId);    
    4.   model.addAttribute("owner", owner);    
    5.   return "displayOwner";   
    6. }  

    example C)
    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. @RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:d.d.d}.{extension:.[a-z]}")  
    2.   public void handle(@PathVariable String version, @PathVariable String extension) {      
    3.     // ...  
    4.   }  
    5. }  


    2 consumes、produces 示例

    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. @Controller  
    2. @RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")  
    3. public void addPet(@RequestBody Pet pet, Model model) {      
    4.     // implementation omitted  
    5. }  
    方法仅处理request Content-Type为“application/json”类型的请求。
    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. @Controller  
    2. @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")  
    3. @ResponseBody  
    4. public Pet getPet(@PathVariable String petId, Model model) {      
    5.     // implementation omitted  
    6. }  

    3 params、headers 示例

    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. @Controller  
    2. @RequestMapping("/owners/{ownerId}")  
    3. public class RelativePathUriTemplateController {  
    4.   
    5.   @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")  
    6.   public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {      
    7.     // implementation omitted  
    8.   }  
    9. }  
    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. @Controller  
    2. @RequestMapping("/owners/{ownerId}")  
    3. public class RelativePathUriTemplateController {  
    4.   
    5. @RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")  
    6.   public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {      
    7.     // implementation omitted  
    8.   }  
    9. }  
  • 相关阅读:
    null和undefined的区别
    减少页面加载时间的方法
    html5有哪些新特性、移除了那些元素?
    cookies,sessionStorage 和 localStorage 的区别
    小程序页面
    快速保存网页图片的工具
    Flex 布局教程
    第一阶段:Python开发基础 day08 Python基础语法入门--列表元组字典集合类型的内置方法
    第一阶段:Python开发基础 day08 数据类型的内置方法 课后作业
    python学习第一周知识内容回顾与小结
  • 原文地址:https://www.cnblogs.com/caijiankai/p/10156257.html
Copyright © 2011-2022 走看看