@requestmapping 是 spring mvc 中 定义请求映射规则的核心注解,用于将 http 请求映射到 controller 处理方法。以下通过场景化解释其功能、参数及使用技巧:
一、核心作用
url 路径映射
将特定 url 请求路由到对应的 controller 方法。
@controller
@requestmapping("/user") // 类级别路径,所有方法路径前添加 /user
public class usercontroller {
@requestmapping("/profile") // 完整路径:/user/profile
public string profile() {
return "user/profile";
}
}支持多种http方法
通过 method 参数限定请求方法(get、post等)。
@requestmapping(value = "/create", method = requestmethod.post)
public string createuser(user user) {
userservice.save(user);
return "redirect:/user/list";
}多请求处理
支持同时响应多个 url 路径或请求参数。
@requestmapping(value = {"/list", "/all"}, method = requestmethod.get)
public string listusers(model model) {
model.addattribute("users", userservice.findall());
return "user/list";
}二、关键参数说明
| 参数 | 作用描述 | 示例 |
|---|---|---|
value / path | 指定映射的url路径(可多路径) | @requestmapping("/api/user") |
method | 限制http请求方法(get、post等) | method = requestmethod.put |
params | 要求请求必须包含特定参数 | params = "type=admin" (参数须为 type=admin) |
headers | 限制请求头条件 | headers = "content-type=application/json" |
consumes | 指定处理的请求内容类型(content-type) | consumes = mediatype.application_json_value |
produces | 指定响应内容的类型(accept头匹配) | produces = "text/plain;charset=utf-8" |
三、快捷组合注解
spring 4.3+ 提供简化的http方法注解,替代 method 参数配置:
@getmapping→@requestmapping(method = get)@postmapping→@requestmapping(method = post)@putmapping、@deletemapping、@patchmapping
用法示例:
@restcontroller
@requestmapping("/api/v1")
public class userapicontroller {
@getmapping("/users/{id}")
public user getuser(@pathvariable long id) {
return userservice.findbyid(id);
}
@postmapping("/users")
public responseentity<?> createuser(@requestbody user user) {
userservice.save(user);
return responseentity.created(uri.create("/users/" + user.getid())).build();
}
}四、动态路径参数(@pathvariable)
通过 {变量名} 语法捕获 url 路径中的动态值,搭配 @pathvariable 使用:
@getmapping("/detail/{userid}/{type}")
public string userdetail(
@pathvariable("userid") long id,
@pathvariable string type // 变量名相同可省略参数
) {
// 示例url:/user/detail/123/admin
// id=123, type="admin"
return "user/detail";
}五、匹配请求参数(params条件)
要求存在某个参数
@requestmapping(value = "/search", params = "keyword")
public string searchbykeyword(string keyword) { ... }参数值匹配指定模式
@getmapping(value = "/filter", params = "status=active")
public list<user> getactiveusers() { ... }六、常见问题与解决
1. 路径冲突问题
- 问题:多个处理方法映射到同一路径导致冲突。
- 解决:通过
method或params进一步区分请求。
@getmapping("/edit")
public string editform() { ... }
@postmapping("/edit")
public string saveedit(user user) { ... }2. 模糊匹配优先问题
- 问题:
/user/*和/user/123同时存在时,优先匹配更具体的路径。 - 规则:spring 优先匹配精确路径,再匹配通配符路径。
七、最佳实践
restful 风格设计
使用http方法区分操作:
get→ 查询资源post→ 新增资源put→ 更新完整资源patch→ 部分更新资源delete→ 删除资源
显式指定 content-type
使用 consumes 和 produces 明确请求与响应格式:
@postmapping(value = "/create", consumes = "application/json", produces = "application/json")
public user createuserjson(@requestbody user user) { ... }推荐使用组合注解
优先用 @getmapping、@postmapping,提升代码可读性。
八、其他特性
ant风格通配符支持:
?: 匹配单个字符*: 匹配任意数量的字符(不包含路径分隔符)**: 跨多级路径匹配(例如/api/**匹配/api/users/123)
@getmapping("/files/*.txt") // 匹配 /files/note.txt 或 /files/data.txt
public string handletextfiles() { ... }总结
- 核心定位:
@requestmapping是定义 http 请求入口的关键注解。 - 简化开发:通过 组合注解 + pathvariable + 参数条件 实现 restful 接口。
- 注意点:确保路径唯一性,避免冲突;优先使用组合注解提升代码清晰度。
到此这篇关于spring @requestmapping 注解及使用技巧详解的文章就介绍到这了,更多相关spring @requestmapping 注解内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论