spring3 的restful API RequestMapping介绍

 

spring3 的restful API RequestMapping介绍

  9770人阅读  评论(1)  收藏  举报
  分类:
 

原文链接:http://www.javaarch.net/jiagoushi/694.htm


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值