国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

SpringMvc @Requestmapping參數配置

2024-04-27 15:08:41
字體:
來源:轉載
供稿:網友

1) 普通path路徑@RequestMapping(value = "/foos")@ResponseBodypublic String getFoosBySimplePath() {    return "Get some Foos";}2) 指定RequestMethod.POST@RequestMapping(value = "/foos", method = RequestMethod.POST)@ResponseBodypublic String postFoos() {    return "Post some Foos";}3) 指定http請求頭@RequestMapping(value = "/foos", headers = "key=val")@ResponseBodypublic String getFoosWithHeader() {    return "Get some Foos with Header";}其中在headers可以跟多個了,如:RequestMapping(value = "/foos", headers = { "key1=val1", "key2=val2" })@ResponseBodypublic String getFoosWithHeaders() {    return "Get some Foos with Header";}4)@RequestMapping中的新的PRoduct和consume.   在spring 3.0中,可以指定請求頭的media格式,如:@RequestMapping(value = "/foos", method = RequestMethod.GET, headers = "Accept=application/json")@ResponseBodypublic String getFoosAsJsonFromBrowser() {    return "Get some Foos with Header Old";}如果在3.1中,則有新的 produces和consume的屬性了,如:@RequestMapping(value = "/foos", method = RequestMethod.GET, produces = "application/json")@ResponseBodypublic String getFoosAsJsonFromREST() {    return "Get some Foos with Header New";}如果用3.1,但依然用舊的方式,則舊的方式的請求都會自動變成produces和consume了;@RequestMapping(value="/testMsgConverter",consumes="text/plain",produces="application/json")    表示handlermethod接受的請求的header中的 Content-Type為text/plain; Accept為application/json 5) @PathVariable    1 單一的@RequestMapping(value = "/foos/{id}")@ResponseBodypublic String getFoosBySimplePathWithPathVariable(@PathVariable("id") long id) {   return "Get a specific Foo with id=" + id;} 2 多個@RequestMapping(value = "/foos/{fooid}/bar/{barid}")@ResponseBodypublic String getFoosBySimplePathWithPathVariables(@PathVariable long fooid, @PathVariable long barid) {    return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid;}

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

6) requestparam

@RequestMapping(value = "/bars")@ResponseBodypublic String getBarBySimplePathWithRequestParam(@RequestParam("id") long id) {    return "Get a specific Bar with id=" + id;}@RequestMapping(value = "/bars", params = "id")@ResponseBodypublic String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") long id) {    return "Get a specific Bar with id=" + id;}7) RequestMapping支持多個映射路徑映射到同一個controller,如:@RequestMapping(value = { "/advanced/bars", "/advanced/foos" })@ResponseBodypublic String getFoosOrBarsByPath() {    return "Advanced - Get some Foos or Bars";}curl -i http://localhost:8080/spring-mvc/advanced/fooscurl -i http://localhost:8080/spring-mvc/advanced/bars甚至還支持put,post同時請求,如:@RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST })@ResponseBodypublic String putAndPostFoos() {    return "Advanced - PUT and POST within single method";}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临汾市| 广西| 芒康县| 大方县| 米脂县| 大名县| 凤山县| 贺州市| 香港 | 梅河口市| 汾阳市| 康平县| 宽甸| 崇仁县| 北海市| 通城县| 本溪| 潜江市| 临海市| 新安县| 湘潭县| 灵石县| 抚宁县| 赫章县| 出国| 江安县| 故城县| 腾冲县| 沛县| 仙居县| 金湖县| 深圳市| 武汉市| 赤壁市| 会东县| 铜鼓县| 喀什市| 隆回县| 易门县| 房山区| 图们市|