RequestMapping注解
- 作用:用于建立请求URL和处理请求方法之间的对应关系。
- 出现位置:在类、接口、方法上
| 出现位置 | 说明 | 举例 |
| 类上 | 请求URL的第一级访问目录; 不写,相当于应用的根目录; 写,要以/开头; 目的是为了使URL可以按照模块化管理。 | 账户模块: /account/add /account/update /account/delete 订单模块: /order/add /order/update /order/delete |
| 方法上 | 请求URL的第二级访问目录; |
源码:
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @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 {};
}
说明:
- java中元注解有四个: @Retention @Target @Documented @Inherited;
| 元注解 | 用途 | 用法 |
|---|---|---|
| @Target | 注解的作用目标 | @Target(ElementType.TYPE) //接口、类、枚举 |
| @Retention | 注解的保留位置 | @Retention(RetentionPolicy.RUNTIME) ,注解会在class字节码文件中存在,在运行时可以通过反射获取到; @Retention(RetentionPolicy.SOURCE),注解仅存在于源码中,在class字节码文件中不包含; @Retention(RetentionPolicy.CLASS),默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得; |
| @Documented | 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档。 | |
| @Inherited | 说明子类可以继承父类中的该注解 |
cn.itcast.controller.RequestMappingController
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class RequestMappingController {
@RequestMapping(path = "/testRequestMapping")
public String testRequestMapping(){
System.out.println("testRequestMapping执行了...");
return "success";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>入门程序</title>
</head>
<body>
<h3>SpringMVC入门</h3>
<a href="/hello">入门程序</a><br>
<a href="/test/testRequestMapping">testRequestMapping</a>
</body>
</html>
RequestMapping注解的属性:
- value:用于指定请求的URL,和path属性的作用一样;属性只有一个value时,可以省略value=不写;
- method:用于指定请求的方法;method属性是RequestMethod类型的数组,可设置多个值,如GET,POST,PUT,DELETE等;
- params:用于指定限制请求参数的条件,支持简单的表达式,要求请求参数的key和value必须和配置的一模一样;
- 如:params={"accountName"},表示请求参数必须有accountName;
- 如:params={"money!100"},表示请求参数中money不能是100;
- headers:用于指定限制请求消息头的条件;
- 注意:以上四个属性只要出现2个或以上时,他们的关系是与的关系;
RequestMethod[] method() default {};
package org.springframework.web.bind.annotation;
public enum RequestMethod {
GET,
HEAD,
POST,
PUT,
PATCH,
DELETE,
OPTIONS,
TRACE;
private RequestMethod() {
}
}
限制请求参数的条件:cn.itcast.controller.RequestMappingController
- params = {"username"},请求参数中要包含username
- params = {"username=heihei"},请求参数中要包含username=heihei
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/test")
public class RequestMappingController {
@RequestMapping(path = "/testRequestMapping",method = {RequestMethod.POST,RequestMethod.GET})
public String testRequestMapping(){
System.out.println("testRequestMapping执行了...");
return "success";
}
@RequestMapping(path = "/testRequestMappingParam",params = {"username=heihei"},method = {RequestMethod.GET})
public String testRequestMappingParam(){
System.out.println("测试testRequestMapping注解的属性,包含参数...");
return "success";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>入门程序</title>
</head>
<body>
<h3>SpringMVC入门</h3>
<a href="/hello">入门程序</a><br>
<a href="/test/testRequestMapping">testRequestMapping</a><br>
<a href="/test/testRequestMappingParam?username=heihei">testRequestMappingParam</a><br>
</body>
</html>
如果index.jsp中,超链接请求没有带上请求参数username,那么点击testRequestMappingParam超链接会报错;
<a href="/test/testRequestMappingParam">testRequestMappingParam</a><br>

如果index.jsp中,超链接请求username取值与Controller限制的不一样,那么点击testRequestMappingParam超链接会报错;
<a href="/test/testRequestMappingParam?username=hehe">testRequestMappingParam</a><br>

限制请求头:请求头中要包含Accept字符串
@RequestMapping(path = "/testRequestMappingParam",headers = "Accept",params = {"username=heihei"},method = {RequestMethod.GET})
本文深入解析了Spring MVC框架中的RequestMapping注解,详细介绍了其在类和方法上的使用,以及如何通过其属性如value、method、params和headers来精确控制请求的URL和方法。同时,文章提供了丰富的代码示例,帮助读者理解注解的实战应用。
3348

被折叠的 条评论
为什么被折叠?



