spring3 的restful API RequestMapping介绍

spring3 的restful API RequestMapping介绍  
  1.   
  2. 在spring mvc中 @RequestMapping是把web请求映射到controller的方法上。  
  3.   
  4. 1.RequestMapping Basic Example  
  5.   将http请求映射到controller方法的最直接方式  
  6. 1.1 @RequestMapping  by Path  
  7.    
  8.     @RequestMapping(value = "/foos")  
  9.     @ResponseBody  
  10.     public String getFoosBySimplePath() {  
  11.         return "Get some Foos";  
  12.     }  
  13.       
  14. 可以通过下面的方式测试: curl -i http://localhost:8080/springmvc/foos  
  15.   
  16. 1.2 @RequestMapping – the HTTP Method,我们可以加上http方法的限制  
  17.   
  18.     @RequestMapping(value = "/foos", method = RequestMethod.POST)  
  19.     @ResponseBody  
  20.     public String postFoos() {  
  21.         return "Post some Foos";  
  22.     }  
  23.       
  24. 可以通过curl i -X POST http://localhost:8080/springmvc/foos测试。  
  25.   
  26. 2.RequestMapping 和http header  
  27.   
  28. 2.1 @RequestMapping with the headers attribute  
  29.   当request的header包含某个key value值时  
  30.       
  31.     @RequestMapping(value = "/foos", headers = "key=val")  
  32.     @ResponseBody  
  33.     public String getFoosWithHeader() {  
  34.         return "Get some Foos with Header";  
  35.     }  
  36.       
  37.   header多个字段满足条件时  
  38.     
  39.     @RequestMapping(value = "/foos", headers = { "key1=val1""key2=val2" })  
  40.     @ResponseBody  
  41.     public String getFoosWithHeaders() {  
  42.         return "Get some Foos with Header";  
  43.     }  
  44.       
  45. 通过curl -i -H "key:val" http://localhost:8080/springmvc/foos 测试。  
  46.   
  47. 2.2 @RequestMapping 和Accept头  
  48.   
  49.     @RequestMapping(value = "/foos", method = RequestMethod.GET, headers = "Accept=application/json")  
  50.     @ResponseBody  
  51.     public String getFoosAsJsonFromBrowser() {  
  52.         return "Get some Foos with Header Old";  
  53.     }  
  54. 支持accept头为json的请求,通过curl -H "Accept:application/json,text/html" http://localhost:8080/springmvc/foos测试  
  55.   
  56. 在spring3.1@RequestMapping注解有produces和 consumes 两个属性来代替accept头  
  57.   
  58.     @RequestMapping(value = "/foos", method = RequestMethod.GET, produces = "application/json")  
  59.     @ResponseBody  
  60.     public String getFoosAsJsonFromREST() {  
  61.         return "Get some Foos with Header New";  
  62.     }  
  63. 同样可以通过curl -H "Accept:application/json" http://localhost:8080/springmvc/foos测试  
  64.   
  65. produces可以支持多个  
  66.   
  67.     @RequestMapping(value = "/foos", produces = { "application/json""application/xml" })  
  68.       
  69. 当前不能有两个方法同时映射到同一个请求,要不然会出现下面这个异常  
  70.   
  71.     Caused by: java.lang.IllegalStateException: Ambiguous mapping found.   
  72.     Cannot map 'fooController' bean method  
  73.     public java.lang.String org.baeldung.spring.web.controller.FooController.getFoosAsJsonFromREST()  
  74.     to {[/foos],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}:   
  75.     There is already 'fooController' bean method  
  76.     public java.lang.String org.baeldung.spring.web.controller.FooController.getFoosAsJsonFromBrowser()   
  77.     mapped.  
  78.       
  79. 3.RequestMapping with Path Variables  
  80. 3.1我们可以把@PathVariable把url映射到controller方法  
  81.     单个@PathVariable参数映射  
  82.       
  83.     @RequestMapping(value = "/foos/{id}")  
  84.     @ResponseBody  
  85.     public String getFoosBySimplePathWithPathVariable(@PathVariable("id"long id) {  
  86.        return "Get a specific Foo with id=" + id;  
  87.     }  
  88.   
  89. 通过curl http://localhost:8080/springmvc/foos/1试试  
  90. 如果参数名跟url参数名一样,可以省略为  
  91.   
  92.     @RequestMapping(value = "/foos/{id}")  
  93.     @ResponseBody  
  94.     public String getFoosBySimplePathWithPathVariable(@PathVariable String id) {  
  95.        return "Get a specific Foo with id=" + id;  
  96.     }  
  97. 3.2 多个@PathVariable  
  98.     
  99.     @RequestMapping(value = "/foos/{fooid}/bar/{barid}")  
  100.     @ResponseBody  
  101.     public String getFoosBySimplePathWithPathVariables(@PathVariable long fooid, @PathVariable long barid) {  
  102.         return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid;  
  103.     }  
  104.       
  105. 通过curl http://localhost:8080/springmvc/foos/1/bar/2测试。  
  106.   
  107. 3.3支持正则的@PathVariable   
  108.     
  109.     @RequestMapping(value = "/bars/{numericId:[\\d]+}")  
  110.     @ResponseBody  
  111.     public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) {  
  112.         return "Get a specific Bar with id=" + numericId;  
  113.     }  
  114.   
  115. 这个url匹配:http://localhost:8080/springmvc/bars/1  
  116. 不过这个不匹配:http://localhost:8080/springmvc/bars/abc  
  117.   
  118. 4.RequestMapping with Request Parameters  
  119.   
  120. 我们可以使用 @RequestParam注解把请求参数提取出来  
  121. 比如url:http://localhost:8080/springmvc/bars?id=100  
  122.   
  123.     @RequestMapping(value = "/bars")  
  124.     @ResponseBody  
  125.     public String getBarBySimplePathWithRequestParam(@RequestParam("id"long id) {  
  126.         return "Get a specific Bar with id=" + id;  
  127.     }  
  128.       
  129. 我们可以通过RequestMapping定义参数列表  
  130.   
  131.     @RequestMapping(value = "/bars", params = "id")  
  132.     @ResponseBody  
  133.     public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id"long id) {  
  134.         return "Get a specific Bar with id=" + id;  
  135.     }  
  136.       
  137. 和  
  138.   
  139.     @RequestMapping(value = "/bars", params = { "id""second" })  
  140.     @ResponseBody  
  141.     public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id"long id) {  
  142.         return "Narrow Get a specific Bar with id=" + id;  
  143.     }  
  144.       
  145. 比如http://localhost:8080/springmvc/bars?id=100&second=something会匹配到最佳匹配的方法上,这里会映射到下面这个。  
  146.   
  147. 5.RequestMapping Corner Cases  
  148.   
  149. 5.1 @RequestMapping多个路径映射到同一个controller的同一个方法  
  150.   
  151.     @RequestMapping(value = { "/advanced/bars""/advanced/foos" })  
  152.     @ResponseBody  
  153.     public String getFoosOrBarsByPath() {  
  154.         return "Advanced - Get some Foos or Bars";  
  155.     }  
  156.       
  157. 下面这两个url会匹配到同一个方法  
  158.   
  159.     curl -i http://localhost:8080/springmvc/advanced/foos  
  160.     curl -i http://localhost:8080/springmvc/advanced/bars  
  161.       
  162. 5.2@RequestMapping 多个http方法 映射到同一个controller的同一个方法  
  163.   
  164.     @RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST })  
  165.     @ResponseBody  
  166.     public String putAndPostFoos() {  
  167.         return "Advanced - PUT and POST within single method";  
  168.     }  
  169. 下面这两个url都会匹配到上面这个方法  
  170.   
  171.     curl -i -X POST http://localhost:8080/springmvc/foos/multiple  
  172.     curl -i -X PUT http://localhost:8080/springmvc/foos/multiple  
  173.       
  174. 5.3@RequestMapping 匹配所有方法  
  175.   
  176.     @RequestMapping(value = "*")  
  177.     @ResponseBody  
  178.     public String getFallback() {  
  179.         return "Fallback for GET Requests";  
  180.     }  
  181.   
  182. 匹配所有方法  
  183.       
  184.     @RequestMapping(value = "*", method = { RequestMethod.GET, RequestMethod.POST ... })  
  185.     @ResponseBody  
  186.     public String allFallback() {  
  187.         return "Fallback for All Requests";  
  188.     }  
  189.       
  190. 6.Spring Configuration  
  191.   
  192. controller的annotation  
  193.   
  194.     @Controller  
  195.     public class FooController { ... }  
  196.       
  197. spring3.1  
  198.   
  199.     @Configuration  
  200.     @EnableWebMvc  
  201.     @ComponentScan({ "org.baeldung.spring.web.controller" })  
  202.     public class MvcConfig {  
  203.         //  
  204.     }  
  205.       
  206. 可以从这里看到所有的示例https://github.com/eugenp/tutorials/tree/master/springmvc 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值