Spring mvc中@Requestmapping再探
2015-10-17 11:12
309 查看
这次继续复习spring mvc中的@requestmapping;
1) 普通path路径
Java代码
@RequestMapping(value = "/foos")
@ResponseBody
public String getFoosBySimplePath() {
return "Get some Foos";
}
然后尝试用curl请求下
curl -i http://localhost:8080/spring-mvc/foos
2) 指定RequestMethod.POST
Java代码
@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请求头
Java代码
@RequestMapping(value = "/foos", headers = "key=val")
@ResponseBody
public String getFoosWithHeader() {
return "Get some Foos with Header";
}
其中在headers可以跟多个了,如:
Java代码
@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中的新的product和consume.
在spring 3.0中,可以指定请求头的media格式,如:
Java代码
@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中,则有新的 produces和consume的属性了,如:
Java代码
@RequestMapping(value = "/foos", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public 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 单一的
Java代码
@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 多个
Java代码
@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 也支持正则表达式
Java代码
@RequestMapping(value = "/bars/{numericId:[\\d]+}")
@ResponseBody
public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) {
return "Get a specific Bar with id=" + numericId;
}
则参数只接受数字了
6) requestparam
Java代码
http://localhost:8080/spring-mvc/bars?id=100
@RequestMapping(value = "/bars")
@ResponseBody
public String getBarBySimplePathWithRequestParam(@RequestParam("id") long id) {
return "Get a specific Bar with id=" + id;
}
Java代码
@RequestMapping(value = "/bars", params = "id")
@ResponseBody
public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") long id) {
return "Get a specific Bar with id=" + id;
}
7) RequestMapping支持多个映射路径映射到同一个controller,如:
Java代码
@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同时请求,如:
Java代码
@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
1) 普通path路径
Java代码
@RequestMapping(value = "/foos")
@ResponseBody
public String getFoosBySimplePath() {
return "Get some Foos";
}
然后尝试用curl请求下
curl -i http://localhost:8080/spring-mvc/foos
2) 指定RequestMethod.POST
Java代码
@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请求头
Java代码
@RequestMapping(value = "/foos", headers = "key=val")
@ResponseBody
public String getFoosWithHeader() {
return "Get some Foos with Header";
}
其中在headers可以跟多个了,如:
Java代码
@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中的新的product和consume.
在spring 3.0中,可以指定请求头的media格式,如:
Java代码
@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中,则有新的 produces和consume的属性了,如:
Java代码
@RequestMapping(value = "/foos", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public 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 单一的
Java代码
@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 多个
Java代码
@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 也支持正则表达式
Java代码
@RequestMapping(value = "/bars/{numericId:[\\d]+}")
@ResponseBody
public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) {
return "Get a specific Bar with id=" + numericId;
}
则参数只接受数字了
6) requestparam
Java代码
http://localhost:8080/spring-mvc/bars?id=100
@RequestMapping(value = "/bars")
@ResponseBody
public String getBarBySimplePathWithRequestParam(@RequestParam("id") long id) {
return "Get a specific Bar with id=" + id;
}
Java代码
@RequestMapping(value = "/bars", params = "id")
@ResponseBody
public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") long id) {
return "Get a specific Bar with id=" + id;
}
7) RequestMapping支持多个映射路径映射到同一个controller,如:
Java代码
@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同时请求,如:
Java代码
@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
相关文章推荐
- Android音频输入通道的底层硬件和软件开发分析
- ios 调节器 modal 得知
- Android数据存储之SD卡
- android 软键盘 InputMethodManager
- Android APP build报错:Unable to resolve target 'android-17'
- cocos2d-x Vector的使用
- Android监听器
- cocos2d-x游戏得分显示
- Application Lifecycle Management
- Android 动画效果弹出PopupWindow,同时屏幕背景变暗
- android中checkbox自定义样式
- Android之对话框的使用
- IOS中延时执行的几种方式的比较和汇总
- android 更新并安装应用
- Android面试常见问题:如何在不发一个新版本的情况下更新App布局?
- DOM -----Document Object Model
- 在Mac上配置Android adb命令
- 在Mac上配置Android adb命令
- iOS开发见闻-第1期
- Android四大组件详解