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介绍的更多相关文章
- OpenStack Restful API框架介绍
1 pecan框架介绍 1.1 什么是pecan pecan是一个轻量级的python web框架,最主要的特点是提供了简单的配置即可创建一个wsgi对象并提供了基于对象的路由方式. 主要提供的功 ...
- RESTful api风格介绍
RESTful 接口是目前来说比较流行的一种接口,平常在开发中会非常常见. 有过和后端人员对接接口的小伙伴都应该知道,我们所做的大多数操作都是对数据库的四格操作 “增删改查” 对应到我们的接口操作分别 ...
- kafka restful api功能介绍与使用
前述 采用confluent kafka-rest proxy实现kafka restful service时候(具体参考上一篇笔记),通过http协议数据传输,需要注意的是采用了base64编码(或 ...
- Spring Boot入门系列(二十)快速打造Restful API 接口
spring boot入门系列文章已经写到第二十篇,前面我们讲了spring boot的基础入门的内容,也介绍了spring boot 整合mybatis,整合redis.整合Thymeleaf 模板 ...
- 一文搞懂RESTful API
RESTful接口实战 原创公众号:bigsai 转载请联系bigsai 文章收藏在回车课堂 前言 在学习RESTful 风格接口之前,即使你不知道它是什么,但你肯定会好奇它能解决什么问题?有什么应用 ...
- Spring Boot 2.x 编写 RESTful API (一) RESTful API 介绍 & RestController
用Spring Boot编写RESTful API 学习笔记 RESTful API 介绍 REST 是 Representational State Transfer 的缩写 所有的东西都是资源,所 ...
- python 全栈开发,Day95(RESTful API介绍,基于Django实现RESTful API,DRF 序列化)
昨日内容回顾 1. rest framework serializer(序列化)的简单使用 QuerySet([ obj, obj, obj]) --> JSON格式数据 0. 安装和导入: p ...
- GoldenGate 12.3 MA架构介绍系列(4)–Restful API介绍
OGG 12.3 MA中最大的变化就是使用了restful api,在前面介绍的各个服务模块,其实就是引用restful api开发而来,这些API同时也提供对外的集成接口,详细接口可参考: http ...
- 4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍
4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍 天涯明月笙 关注 2018.02.20 19:23* 字数 762 阅读 135 ...
随机推荐
- QT入门
QT += core gui widgets //引入需要用到的库 qDebug()<<"t="<<t<<QTime::currentTime( ...
- 工作中linux定时任务的设置及相关配置
工作中会用到定时任务,来处理以前采集来的数据备份, 每周一凌晨4点执行一次 0 4 * * */1 find/data/templatecdr/oracle/dcndatabak/ -type ...
- mapreduce 本地调试需要注意的问题
1.写好的程序直接在hadoop集群里面执行 2.如果需要在本地调试,需要注释掉mapred-site.xml <configuration> <!-- <property&g ...
- 如何清理WindowsXp的临时文件
软件限制策略中的 "路径规则" 不允许的, 是指在 这个路径中的程序都 不准运行! 这就限制了 : 通常电脑中病毒, 都是通过上网感染病毒的 -> 病毒/恶意软件通过 &qu ...
- [译]JavaScript:将字符串两边的双引号转换成单引号
原文:http://ariya.ofilabs.com/2012/02/from-double-quotes-to-single-quotes.html 代码的不一致性总是让人发狂,如果每位开发者都能 ...
- 11个很棒的 jQuery 图表库
如果你曾经使用过任何类型的数据,你应该知道阅读一排排数据的痛苦.通过所有这些数据弄清楚他们的意思是非常不容易的.可视化对于解决这个问题起到了重要的作用.可视化降低了数据阅读的难度,帮助决策者获得可操作 ...
- PHP 线程安全与非线程安全版本的区别深入解析
Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分,这两者不同在于何处?到底应该用哪种?这里做一个简单的介绍 ...
- [译]在Mac上运行ASP.NET 5
原文:http://stephenwalther.com/archive/2015/02/03/asp-net-5-and-angularjs-part-7-running-on-a-mac 这篇文章 ...
- JQuery data方法的使用-遁地龙卷风
(-1)说明 我用的是chrome49,这个方法涉及到JQuery版本问题,我手里有3.0的,有1.9.1,后面将1.9.1及其以前的称为低版本,3.0称为高版本 测试例子用到的showMessage ...
- PL/sql developer连接数据库的问题以及oracle数据库中文乱码的问题
今天第二次配置PL/sql developer,表示很蛋疼,昨天因为动了一个东西然后莫名其妙的就再也连接不了数据库,总是显示各种错误,我动的东西是因为中文会显示乱码,(因为我是用32位的PL/sql ...