Spring mvc中@Requestmapping參數配置

 

原文標題[ Spring mvc中@Requestmapping再探] html

1) 普通path路徑
@RequestMapping(value = "/foos")
@ResponseBody
public String getFoosBySimplePath() {
    return "Get some Foos";
}

而後嘗試用curl請求下
curl -i http://localhost:8080/spring-mvc/foos

2)
指定RequestMethod.POST
@RequestMapping(value = "/foos", method = RequestMethod.POST)
@ResponseBody
public String postFoos() {
    return "Post some Foos";
}
curl i -X POST http://localhost:8080/spring-mvc/foos
3)
指定http請求頭
@RequestMapping(value = "/foos", headers = "key=val")
@ResponseBody
public String getFoosWithHeader() {
    return "Get some Foos with Header";
}

其中在headers能夠跟多個了,:
RequestMapping(value = "/foos", headers = { "key1=val1", "key2=val2" })
@ResponseBody
public String getFoosWithHeaders() {
    return "Get some Foos with Header";
}

注意curl的請求爲:
curl -i -H "key:val" http://localhost:8080/spring-mvc/foos

4)@RequestMapping
中的新的productconsume.
   
spring 3.0,能夠指定請求頭的media格式,:
@RequestMapping(value = "/foos", method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public String getFoosAsJsonFromBrowser() {
    return "Get some Foos with Header Old";
}
curl
測試:
curl -H "Accept:application/json,text/html" http://localhost:8080/spring-mvc/foos

若是在3.1,則有新的 producesconsume的屬性了,:
@RequestMapping(value = "/foos", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String getFoosAsJsonFromREST() {
    return "Get some Foos with Header New";
}

若是用3.1,但依然用舊的方式,則舊的方式的請求都會自動變成producesconsume;
@RequestMapping(value="/testMsgConverter",consumes="text/plain",produces="application/json")  
  
表示handlermethod接受的請求的header中的 Content-Typetext/plain; 
Accept
application/json 

5) @PathVariable 
   1
單一的
@RequestMapping(value = "/foos/{id}")
@ResponseBody
public String getFoosBySimplePathWithPathVariable(@PathVariable("id") long id) {
   return "Get a specific Foo with id=" + id;
}

測試:curl http://localhost:8080/spring-mvc/foos/1
 2
多個
@RequestMapping(value = "/foos/{fooid}/bar/{barid}")
@ResponseBody
public String getFoosBySimplePathWithPathVariables(@PathVariable long fooid, @PathVariable long barid) {
    return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid;
}
curl http://localhost:8080/spring-mvc/foos/1/bar/2
正則表達式


3
也支持正則表達式
 @RequestMapping(value = "/bars/{numericId:[\\d]+}")
@ResponseBody
public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) {
    return "Get a specific Bar with id=" + numericId;
}
參數只接受數字 spring


6) requestparam
 http://localhost:8080/spring-mvc/bars?id=100

json

@RequestMapping(value = "/bars")
@ResponseBody
public String getBarBySimplePathWithRequestParam(@RequestParam("id") long id) {
    return "Get a specific Bar with id=" + id;
}

@RequestMapping(value = "/bars", params = "id")
@ResponseBody
public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") long id) {
    return "Get a specific Bar with id=" + id;
}


7) RequestMapping
支持多個映射路徑映射到同一個controller,:
@RequestMapping(value = { "/advanced/bars", "/advanced/foos" })
@ResponseBody
public String getFoosOrBarsByPath() {
    return "Advanced - Get some Foos or Bars";
}

curl -i http://localhost:8080/spring-mvc/advanced/foos
curl -i http://localhost:8080/spring-mvc/advanced/bars

甚至還支持put,post同時請求,:
@RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST })
@ResponseBody
public String putAndPostFoos() {
    return "Advanced - PUT and POST within single method";
}

curl -i -X POST http://localhost:8080/spring-mvc/foos/multiple
curl -i -X PUT http://localhost:8080/spring-mvc/foos/multiple
spring-mvc

相關文章
相關標籤/搜索