SpringMVC RequestMapping用法详解
来源:互联网 发布:oracle数据库cache 编辑:程序博客网 时间:2024/05/17 01:17
SpringMVC 使用@RequestMapping
注解为Controller(控制器)指定可以处理哪些URL请求。所以,它本质上相当于web.xml
的url-pattern
。
@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Mappingpublic @interface RequestMapping { String name() default ""; @AliasFor("path") String[] value() default {}; @AliasFor("value") String[] path() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {};}
@Target({ElementType.METHOD, ElementType.TYPE})
注解表明:@RequestMapping
可以在方法和类的声明中使用。- 类定义处:提供初步的请求映射信息
- 方法处:提供进一步的细分映射信息
- 用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
@Retention(RetentionPolicy.RUNTIME)
注解表明:RequestMapping
注解将会由虚拟机保留,以便它可以在运行时通过反射读取。@Documented
注解表明:@RequestMapping
注解应该被 javadoc工具记录,也就是说该类注解类型信息也会被包括在生成的文档中。@Mapping
注解表明:@RequestMapping
注解是一个web映射注解。
RequestMapping
注解中除了name()
方法返回字符串,其他都返回数组,也就是说它们可以接收多个参数。
name
String name() default "";
为这个映射指定一个名字,在项目中,我们一般都不使用。
value & path
@AliasFor("path") String[] value() default {}; @AliasFor("value") String[] path() default {};
value和path属性是等价的,可以互换,如果在RequestMapping
注解中仅有这一个属性(@RequestMapping(value = "/spring")
),则可以省略value=
,而只写@RequestMapping("/spring")
。
1、将 @RequestMapping 注解在 test 方法上,而 HelloController 类上不添加 @RequestMapping 注解,这时的请求 URL 是相对于 Web 根目录。
@Controllerpublic class HelloController { @RequestMapping("/test") public String test() { return "hello"; }}
使用 CURL 访问: curl http://localhost:8080/SpringMVCDemo/test
SpringMVCDemo
是我们的项目名称。
2、将 @RequestMapping
注解在 HelloController
类上,这时类的注解是相对于 Web 根目录,而方法上的是相对于类上的路径。
@Controller@RequestMapping("/spring")public class HelloController { @RequestMapping("/test") public String test() { return "hello"; }}
这时,访问应该是这样的:curl http://localhost:8080/SpringMVCDemo/spring/test
method
RequestMethod[] method() default {};
表示接收何种类型的请求。 org.springframework.web.bind.annotation.RequestMethod
枚举类定义了浏览器请求的方式。
public enum RequestMethod { GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE}
如果 @RequestMapping
没有method属性,则接收任意类型的URL请求。
@Controllerpublic class HelloController { @RequestMapping(value = "/test") public String test(HttpServletRequest request) { System.out.println(request.getMethod()); return "hello"; }}
public class HelloController { @RequestMapping(value = "/test", method = RequestMethod.POST) public String test(HttpServletRequest request) { System.out.println(request.getMethod()); return "hello"; }}
如上,则只可以接收POST类型的请求。当我们使用GET方式访问时,则报错。
params
String[] params() default {};
表示接收何种请求参数的请求。
- param1:表示请求必须包含名为 param1 的请求参数
- !param1:表示请求不能包含名为 param1 的请求参数
- param1!=value1:表示请求包含名为 param1 的请求参数,但是其值不能为 value1;或者,请求不包含名为 param1 的请求参数
- {“param1=value1”, “param2”}:表示请求必须包含名为 param1 和 param2 的两个请求参数,且 param1 请求参数的值必须为 value1
@Controllerpublic class HelloController { @RequestMapping(value = "/test", method = RequestMethod.GET, params = {"name!=lgh", "age=24"}) public String test() { System.out.println("Hello World"); return "hello"; }}
使用 http://localhost:8080/SpringMVCDemo/test?age=24&name=lghtest
或者http://localhost:8080/SpringMVCDemo/test?age=24
才可以访问。
headers
String[] headers() default {};
表示接收何种请求头的请求。
使用和param
参数类似。
consumes
String[] consumes() default {};
表示接收何种内容类型(Content-Type)的请求,例如application/json
, text/html
。
@Controllerpublic class HelloController { @RequestMapping(value = "/test", method = RequestMethod.GET, consumes = "text/html") public String test() { System.out.println("Hello World"); return "hello"; }}
curl -H “Content-Type: text/html” http://localhost:8080/SpringMVCDemo/test
又比如:
@RequestMapping(value = "/ceshi", method = RequestMethod.GET, consumes = "application/json") @ResponseBody public String test(@RequestBody String str) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); Person person = objectMapper.readValue(str, Person.class); System.out.println(person); return str; }
produces
String[] produces() default {};
表示接收request请求头中的(Accept)字段中包含该指定类型的请求。同时也暗示返回类型为该指定类型。
@Controllerpublic class HelloController { @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json") public String test() { System.out.println("Hello World"); return "hello"; }}
以上只是演示,真正使用过程应该如下:
@RequestMapping(value = "/produces", method = RequestMethod.GET, produces = "application/json; charset=UTF-8") @ResponseBody public Person testProduces() { Person person = new Person(); person.setName("lgh"); person.setAge(25); Address address = new Address(); address.setProvince("山东"); address.setCity("聊城"); person.setAddress(address); return person; }
Http报头Accept与Content-Type的区别:
1.Accept属于请求头, Content-Type属于实体头。
Http报头分为通用报头,请求报头,响应报头和实体报头。
请求方的http报头结构:通用报头|请求报头|实体报头
响应方的http报头结构:通用报头|响应报头|实体报头
2.Accept代表发送端(客户端)希望接受的数据类型。
比如:Accept: text/xml
代表客户端希望接受的数据类型是xml类型
Content-Type代表发送端(客户端|服务器)发送的实体数据的数据类型。
比如:Content-Type: text/html
代表发送端发送的数据格式是html。
二者合起来,
Accept: text/xml
Content-Type: text/html
即代表希望接受的数据类型是xml格式,本次请求发送的数据的数据格式是html。
- SpringMVC RequestMapping用法详解
- Springmvc @RequestMapping 用法
- SpringMVC -- @RequestMapping用法
- SpringMVC RequestMapping 参数详解
- SpringMVC RequestMapping注解详解
- SpringMvc中@RequestMapping详解
- SpringMVC中@RequestMapping详解
- springmvc(基础一) @RequestMapping 用法详解之地址映射
- @RequestMapping 用法详解
- @RequestMapping的用法详解
- @RequestMapping 用法详解
- @RequestMapping 用法详解
- @RequestMapping用法详解
- @RequestMapping用法详解
- @RequestMapping用法详解
- @RequestMapping用法详解
- SpringMVC的@RequestMapping的用法
- springmvc的requestmapping参数详解
- STM32串口第一个字节丢失问题的分析过程
- 递归
- 集合框架
- 16 创建高级联结
- 序列流(集合流) * 把n个流合并在一起读取
- SpringMVC RequestMapping用法详解
- iOS 开发 图片缩略图的生成,展示图片中间一部分
- tensorflow 学习笔记12 循环神经网络RNN LSTM结构实现MNIST手写识别
- 【Mybatis从0到1-010】一对一查询(resultType和resultMap)
- JavaScript实现简单的图片轮播(通过点击数字切换)
- mybatis常用jdbcType数据类类型对照表
- vs断点失效
- android下adb 命令(转载)
- ViewPager + fragment 中 懒加载的一点经验