spring3 的restful API RequestMapping介绍
原文链接:http://www.javaarch.net/jiagoushi/694.htm
spring3 的restful API RequestMapping介绍 在spring mvc中 @RequestMapping是把web请求映射到controller的方法上。 1.RequestMapping Basic Example 将http请求映射到controller方法的最直接方式 1.1 @RequestMapping by Path @RequestMapping(value = "/foos") @ResponseBody public String getFoosBySimplePath() { return "Get some Foos"; } 可以通过下面的方式测试: curl -i http://localhost:8080/springmvc/foos 1.2 @RequestMapping – the HTTP Method,我们可以加上http方法的限制 @RequestMapping(value = "/foos", method = RequestMethod.POST) @ResponseBody public String postFoos() { return "Post some Foos"; } 可以通过curl i -X POST http://localhost:8080/springmvc/foos测试。 2.RequestMapping 和http header 2.1 @RequestMapping with the headers attribute 当request的header包含某个key value值时 @RequestMapping(value = "/foos", headers = "key=val") @ResponseBody public String getFoosWithHeader() { return "Get some Foos with Header"; } header多个字段满足条件时 @RequestMapping(value = "/foos", headers = { "key1=val1", "key2=val2" }) @ResponseBody public String getFoosWithHeaders() { return "Get some Foos with Header"; } 通过curl -i -H "key:val" http://localhost:8080/springmvc/foos 测试。 2.2 @RequestMapping 和Accept头 @RequestMapping(value = "/foos", method = RequestMethod.GET, headers = "Accept=application/json") @ResponseBody public String getFoosAsJsonFromBrowser() { return "Get some Foos with Header Old"; } 支持accept头为json的请求,通过curl -H "Accept:application/json,text/html" http://localhost:8080/springmvc/foos测试 在spring3.1中@RequestMapping注解有produces和 consumes 两个属性来代替accept头 @RequestMapping(value = "/foos", method = RequestMethod.GET, produces = "application/json") @ResponseBody public String getFoosAsJsonFromREST() { return "Get some Foos with Header New"; } 同样可以通过curl -H "Accept:application/json" http://localhost:8080/springmvc/foos测试 produces可以支持多个 @RequestMapping(value = "/foos", produces = { "application/json", "application/xml" }) 当前不能有两个方法同时映射到同一个请求,要不然会出现下面这个异常 Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'fooController' bean method public java.lang.String org.baeldung.spring.web.controller.FooController.getFoosAsJsonFromREST() to {[/foos],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}: There is already 'fooController' bean method public java.lang.String org.baeldung.spring.web.controller.FooController.getFoosAsJsonFromBrowser() mapped. 3.RequestMapping with Path Variables 3.1我们可以把@PathVariable把url映射到controller方法 单个@PathVariable参数映射 @RequestMapping(value = "/foos/{id}") @ResponseBody public String getFoosBySimplePathWithPathVariable(@PathVariable("id") long id) { return "Get a specific Foo with id=" + id; } 通过curl http://localhost:8080/springmvc/foos/1试试 如果参数名跟url参数名一样,可以省略为 @RequestMapping(value = "/foos/{id}") @ResponseBody public String getFoosBySimplePathWithPathVariable(@PathVariable String id) { return "Get a specific Foo with id=" + id; } 3.2 多个@PathVariable @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/springmvc/foos/1/bar/2测试。 3.3支持正则的@PathVariable @RequestMapping(value = "/bars/{numericId:[\\d]+}") @ResponseBody public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) { return "Get a specific Bar with id=" + numericId; } 这个url匹配:http://localhost:8080/springmvc/bars/1 不过这个不匹配:http://localhost:8080/springmvc/bars/abc 4.RequestMapping with Request Parameters 我们可以使用 @RequestParam注解把请求参数提取出来 比如url:http://localhost:8080/springmvc/bars?id=100 @RequestMapping(value = "/bars") @ResponseBody public String getBarBySimplePathWithRequestParam(@RequestParam("id") long id) { return "Get a specific Bar with id=" + id; } 我们可以通过RequestMapping定义参数列表 @RequestMapping(value = "/bars", params = "id") @ResponseBody public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") long id) { return "Get a specific Bar with id=" + id; } 和 @RequestMapping(value = "/bars", params = { "id", "second" }) @ResponseBody public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id") long id) { return "Narrow Get a specific Bar with id=" + id; } 比如http://localhost:8080/springmvc/bars?id=100&second=something会匹配到最佳匹配的方法上,这里会映射到下面这个。 5.RequestMapping Corner Cases 5.1 @RequestMapping多个路径映射到同一个controller的同一个方法 @RequestMapping(value = { "/advanced/bars", "/advanced/foos" }) @ResponseBody public String getFoosOrBarsByPath() { return "Advanced - Get some Foos or Bars"; } 下面这两个url会匹配到同一个方法 curl -i http://localhost:8080/springmvc/advanced/foos curl -i http://localhost:8080/springmvc/advanced/bars 5.2@RequestMapping 多个http方法 映射到同一个controller的同一个方法 @RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST }) @ResponseBody public String putAndPostFoos() { return "Advanced - PUT and POST within single method"; } 下面这两个url都会匹配到上面这个方法 curl -i -X POST http://localhost:8080/springmvc/foos/multiple curl -i -X PUT http://localhost:8080/springmvc/foos/multiple 5.3@RequestMapping 匹配所有方法 @RequestMapping(value = "*") @ResponseBody public String getFallback() { return "Fallback for GET Requests"; } 匹配所有方法 @RequestMapping(value = "*", method = { RequestMethod.GET, RequestMethod.POST ... }) @ResponseBody public String allFallback() { return "Fallback for All Requests"; } 6.Spring Configuration controller的annotation @Controller public class FooController { ... } spring3.1 @Configuration @EnableWebMvc @ComponentScan({ "org.baeldung.spring.web.controller" }) public class MvcConfig { // } 可以从这里看到所有的示例https://github.com/eugenp/tutorials/tree/master/springmvc
spring3 的restful API RequestMapping介绍相关推荐
- rest api是什么_一文搞懂什么是RESTful API
RESTful接口实战 首发公众号:bigsai 转载请附上本文链接 文章收藏在回车课堂 前言 在学习RESTful 风格接口之前,即使你不知道它是什么,但你肯定会好奇它能解决什么问题?有什么应用场景 ...
- php slim 教程,Slim - 超轻量级PHP Restful API构建框架
下载源码包: http://www.slimframework.com/ 基于Slim的Restful API Sample: require '/darjuan/Slim/Slim.php'; us ...
- 4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍
使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支付宝支付(暂不支持微信支付),支持手机短信验证码注册, ...
- 【Go API 开发实战 2】RESTful API 介绍
RESTful API 介绍 API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数或者接口,目的是提供应用程序与开发人员基于某软件或硬件得 ...
- RESTful API介绍
什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为"表征状态转移"或&q ...
- AgileConfig - RESTful API 介绍
AgileConfig AgileConfig是一个基于.net core开发的轻量级配置中心. AgileConfig秉承轻量化的特点,部署简单.配置简单.使用简单.学习简单,它只提取了必要的一些功 ...
- 关于RestFul API 介绍与实践
之前演示的PPT,直接看图... •参考链接: •RESTful API 设计最佳 实践 •RESTful API 设计 指南 • SOAP webserivce 和 RESTful webservi ...
- springboot集成swagger2构建RESTful API文档
在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...
- Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
最新文章
- Cnnot find System Java Compiler Ensure that you have installed a JDK
- 【转】linux服务器性能查看
- java判断是否为数组_JS如何判断是否是数组?
- 关于事件相关电位P300应用于视频游戏的研究
- 概述 互联网时代的商业挑战
- (转)Javascript面向对象编程(二):构造函数的继承
- MFC空间几何变换之图像平移、镜像、旋转、缩放
- keytool错误: java.lang.RuntimeException: 用法错误,and 不是合法的命令【转】
- Ubuntu 14.10/15.04/15.10 安装docker
- shell unexpected operator
- 优雅降级实现IE8的transform平移属性
- 自己动手写CPU(6)简单算术操作指令
- android 视频连续播放,VideoView实现视频无缝连续播放
- 阿里在线笔试算法工程师附加题
- 红米9A成功root.9秒解锁BL MIUI12 root权限刷 Magisk面具 TWRP
- OKHttp 可能你从来没用过这样的拦截器
- Linux运维常见面试题之精华收录
- Microsoft Graph PowerShell v2 发布公开预览版 - 一半的大小,加速的自动化体验
- 服务端使用Axis2-1.6.3发布webservice服务、客户端使用Axis1.4实现调用
- 前端开发 html第二课 自结束标签 注释 标签中的属性 文档声明 进制 字符编码 文档使用 VScode 实体 meta标签 语义化标签 块元素和行内元素 布局标签
热门文章
- teamviewer设备数量上限怎么解决_会议音响设备出现啸叫怎么办?不要担心,这3个方法帮你解决...
- 中国慕课java_回收的吸油毡通常应放置一边以备再次使用。
- 服务器虚拟化techtarget技术社区,服务器上的应用程序虚拟化
- catia2017安装包打开没反应_云顶手游10.19安装包,9月16日
- 使用waitgroup控制协程退出
- java接收post文件_java – 如何发送POST请求并获取文件响应?
- TCL微型计算机如何投屏,TCL电视怎么投屏?3个办法帮助你完美解决
- linux 查看命令帮助,Linux中查看帮助相关的命令整理
- php删除表格命令,数据表格-删除
- 26 Socket Addressing and Client Socket Programming