0

0

@RequestMapping 的作用是什么?

星降

星降

发布时间:2025-09-11 08:34:01

|

854人浏览过

|

来源于php中文网

原创

@RequestMapping的主要作用是将HTTP请求映射到Controller的处理方法,支持URL路径、HTTP方法、参数、请求头等匹配规则,可应用于类或方法级别,并支持路径变量、请求参数、请求体处理及文件上传等功能。

@requestmapping 的作用是什么?

@RequestMapping的主要作用,简单说,就是把HTTP请求映射到相应的处理方法上。它像一个交通调度员,根据请求的URL、HTTP方法(GET、POST等)以及其他条件,将请求分配给正确的Controller方法来处理。

解决方案:

RequestMapping注解是Spring MVC框架中一个核心的注解,用于将特定的HTTP请求映射到Controller中的某个方法。它提供了灵活的机制来定义请求的匹配规则,包括URL路径、HTTP方法、请求参数、请求头等。

RequestMapping可以应用在类级别和方法级别。应用在类级别时,它定义了Controller的基础URL路径;应用在方法级别时,它定义了处理该请求的具体方法。

例如:

@Controller
@RequestMapping("/users")
public class UserController {

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String getUser(@PathVariable("id") Long id, Model model) {
        // 根据id获取用户信息
        User user = userService.getUserById(id);
        model.addAttribute("user", user);
        return "user/detail";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addUser(@ModelAttribute("user") User user) {
        // 添加用户
        userService.addUser(user);
        return "redirect:/users";
    }
}

在这个例子中,

@RequestMapping("/users")
定义了UserController的基础URL路径为
/users
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
将GET请求
/users/{id}
映射到
getUser
方法,其中
{id}
是一个路径变量,通过
@PathVariable("id")
注解可以获取它的值。
@RequestMapping(value = "/add", method = RequestMethod.POST)
将POST请求
/users/add
映射到
addUser
方法。

RequestMapping还支持更复杂的匹配规则,例如:

  • 请求参数匹配: 可以通过

    params
    属性指定请求必须包含的参数,例如
    @RequestMapping(value = "/search", params = "keyword")
    表示只有包含
    keyword
    参数的
    /search
    请求才会被映射到该方法。

  • 请求头匹配: 可以通过

    headers
    属性指定请求必须包含的Header,例如
    @RequestMapping(value = "/download", headers = "Accept=application/pdf")
    表示只有
    Accept
    Header为
    application/pdf
    /download
    请求才会被映射到该方法。

  • Consumes和Produces:

    consumes
    属性用于指定Controller方法可以处理的请求内容类型,
    produces
    属性用于指定Controller方法返回的内容类型。例如
    @RequestMapping(value = "/upload", consumes = "multipart/form-data")
    表示该方法只能处理
    multipart/form-data
    类型的请求。
    @RequestMapping(value = "/data", produces = "application/json")
    表示该方法返回的内容类型为JSON。

RequestMapping的灵活性使得开发者可以根据不同的需求,定义各种复杂的请求映射规则。

RequestMapping 还有一些简化的变体,例如

@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
,它们分别对应于 HTTP 的 GET、POST、PUT、DELETE、PATCH 方法。 使用这些注解可以更简洁地表达请求映射关系。 例如,上面的
getUser
方法可以改写为:

@GetMapping("/{id}")
public String getUser(@PathVariable("id") Long id, Model model) {
    // ...
}

如何处理RequestMapping中的路径变量?

路径变量是RequestMapping中非常重要的一个特性,它允许我们将URL中的一部分作为参数传递给Controller方法。 使用

@PathVariable
注解可以方便地获取路径变量的值。

例如:

@GetMapping("/products/{category}/{id}")
public String getProduct(@PathVariable("category") String category, @PathVariable("id") Long id, Model model) {
    // 根据分类和ID获取产品信息
    Product product = productService.getProductByCategoryAndId(category, id);
    model.addAttribute("product", product);
    return "product/detail";
}

在这个例子中,URL

/products/{category}/{id}
中的
{category}
{id}
都是路径变量。通过
@PathVariable("category") String category
@PathVariable("id") Long id
,我们可以分别获取分类和ID的值。

需要注意的是,

@PathVariable
注解中的参数名必须与URL中的路径变量名一致。如果参数名不一致,可以使用
@PathVariable(value = "urlVariableName")
来指定URL中的变量名。

如果路径变量是可选的,可以使用

required = false
属性:

@GetMapping("/articles/{id}")
public String getArticle(@PathVariable(value = "id", required = false) Long id, Model model) {
    if (id != null) {
        // 根据ID获取文章信息
        Article article = articleService.getArticleById(id);
        model.addAttribute("article", article);
    } else {
        // 处理没有ID的情况
        model.addAttribute("message", "Please provide an article ID.");
    }
    return "article/detail";
}

RequestMapping如何处理请求参数?

除了路径变量,RequestMapping还可以通过

@RequestParam
注解来处理请求参数。
@RequestParam
用于获取URL中的查询参数或表单数据。

例如:

@GetMapping("/search")
public String search(@RequestParam("keyword") String keyword, @RequestParam(value = "page", defaultValue = "1") int page, Model model) {
    // 根据关键字和页码搜索
    List products = productService.searchProducts(keyword, page);
    model.addAttribute("products", products);
    model.addAttribute("keyword", keyword);
    model.addAttribute("page", page);
    return "product/list";
}

在这个例子中,

@RequestParam("keyword") String keyword
获取名为
keyword
的请求参数的值。
@RequestParam(value = "page", defaultValue = "1") int page
获取名为
page
的请求参数的值,如果没有
page
参数,则使用默认值
1

@RequestParam
也支持
required
属性,用于指定参数是否是必需的。如果
required = true
且参数不存在,则会抛出
MissingServletRequestParameterException
异常。

@GetMapping("/profile")
public String getProfile(@RequestParam(value = "userId", required = true) Long userId, Model model) {
    // 根据用户ID获取用户信息
    User user = userService.getUserById(userId);
    model.addAttribute("user", user);
    return "user/profile";
}

如果需要获取所有请求参数,可以使用

@RequestParam Map params

@GetMapping("/settings")
public String getSettings(@RequestParam Map params, Model model) {
    // 获取所有请求参数
    model.addAttribute("params", params);
    return "settings/index";
}

如何处理RequestMapping中的请求体?

RequestMapping可以通过

@RequestBody
注解来处理请求体。
@RequestBody
用于获取POST、PUT等请求中的JSON、XML等数据。

例如:

@PostMapping("/users")
public ResponseEntity createUser(@RequestBody User user) {
    // 创建用户
    User createdUser = userService.createUser(user);
    return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
}

在这个例子中,

@RequestBody User user
将请求体中的JSON数据转换为
User
对象。

需要注意的是,要使用

@RequestBody
,需要在项目中引入相应的JSON或XML处理库,例如Jackson或Gson。

如果需要对请求体进行校验,可以使用

@Valid
注解:

@PostMapping("/products")
public ResponseEntity createProduct(@Valid @RequestBody Product product, BindingResult result) {
    if (result.hasErrors()) {
        // 处理校验错误
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
    // 创建产品
    Product createdProduct = productService.createProduct(product);
    return new ResponseEntity<>(createdProduct, HttpStatus.CREATED);
}

在这个例子中,

@Valid @RequestBody Product product
表示对
Product
对象进行校验。
BindingResult
对象用于获取校验结果。

RequestMapping中的通配符使用

RequestMapping支持使用通配符来匹配URL。常用的通配符有:

  • *
    :匹配零个或多个字符
  • ?
    :匹配一个字符
  • **
    :匹配零个或多个目录

例如:

@GetMapping("/products/*")
public String getProductsByCategory(String category, Model model) {
    // 根据分类获取产品列表
    List products = productService.getProductsByCategory(category);
    model.addAttribute("products", products);
    return "product/list";
}

@GetMapping("/files/**")
public String getFiles(String path, Model model) {
    // 获取文件
    File file = new File(path);
    model.addAttribute("file", file);
    return "file/detail";
}

需要注意的是,使用通配符可能会导致URL匹配的模糊性,因此应该谨慎使用。

RequestMapping的优先级问题

当多个RequestMapping匹配同一个URL时,Spring MVC会根据一定的规则来选择优先级最高的RequestMapping。

通常情况下,更具体的RequestMapping具有更高的优先级。例如,

/users/{id}
/users/*
更具体,因此具有更高的优先级。

如果多个RequestMapping的优先级相同,则Spring MVC会选择第一个匹配的RequestMapping。

可以使用

@RequestMapping
priority
属性来显式指定RequestMapping的优先级。
priority
属性的值越小,优先级越高。

@GetMapping(value = "/users/{id}", priority = 1)
public String getUserById(@PathVariable("id") Long id, Model model) {
    // ...
}

@GetMapping(value = "/users/*", priority = 2)
public String getUsersByCategory(String category, Model model) {
    // ...
}

如何处理RequestMapping中的文件上传?

RequestMapping可以通过

MultipartFile
参数来处理文件上传。

例如:

@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
    if (!file.isEmpty()) {
        try {
            // 保存文件
            byte[] bytes = file.getBytes();
            String filename = file.getOriginalFilename();
            Path path = Paths.get("/upload/" + filename);
            Files.write(path, bytes);

            model.addAttribute("message", "File uploaded successfully: " + filename);
        } catch (IOException e) {
            model.addAttribute("message", "File upload failed: " + e.getMessage());
        }
    } else {
        model.addAttribute("message", "Please select a file to upload.");
    }
    return "upload/result";
}

在这个例子中,

@RequestParam("file") MultipartFile file
获取名为
file
的文件。

需要注意的是,要使用

MultipartFile
,需要在项目中配置
MultipartResolver

RequestMapping与拦截器的关系

RequestMapping与拦截器是Spring MVC中两个重要的组件。 拦截器可以在请求到达Controller之前或之后进行拦截,从而实现一些通用的功能,例如权限验证、日志记录等。

可以通过实现

HandlerInterceptor
接口来创建拦截器。

public class LogInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 在请求处理之前执行
        System.out.println("Request URL: " + request.getRequestURL());
        return true; // 返回true表示继续执行,返回false表示中断执行
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 在请求处理之后,但在视图渲染之前执行
        System.out.println("Response Status: " + response.getStatus());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 在整个请求完成之后执行
        if (ex != null) {
            System.err.println("Exception: " + ex.getMessage());
        }
    }
}

然后,需要在Spring MVC的配置中注册拦截器。

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LogInterceptor logInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(logInterceptor).addPathPatterns("/**"); // 拦截所有请求
    }
}

通过配置拦截器,可以对RequestMapping进行统一的管理和控制。 例如,可以根据用户的权限,拦截某些RequestMapping的请求,从而实现权限控制。

相关专题

更多
spring框架介绍
spring框架介绍

本专题整合了spring框架相关内容,想了解更多详细内容,请阅读专题下面的文章。

103

2025.08.06

json数据格式
json数据格式

JSON是一种轻量级的数据交换格式。本专题为大家带来json数据格式相关文章,帮助大家解决问题。

412

2023.08.07

json是什么
json是什么

JSON是一种轻量级的数据交换格式,具有简洁、易读、跨平台和语言的特点,JSON数据是通过键值对的方式进行组织,其中键是字符串,值可以是字符串、数值、布尔值、数组、对象或者null,在Web开发、数据交换和配置文件等方面得到广泛应用。本专题为大家提供json相关的文章、下载、课程内容,供大家免费下载体验。

533

2023.08.23

jquery怎么操作json
jquery怎么操作json

操作的方法有:1、“$.parseJSON(jsonString)”2、“$.getJSON(url, data, success)”;3、“$.each(obj, callback)”;4、“$.ajax()”。更多jquery怎么操作json的详细内容,可以访问本专题下面的文章。

309

2023.10.13

go语言处理json数据方法
go语言处理json数据方法

本专题整合了go语言中处理json数据方法,阅读专题下面的文章了解更多详细内容。

74

2025.09.10

string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

318

2023.08.02

pdf怎么转换成xml格式
pdf怎么转换成xml格式

将 pdf 转换为 xml 的方法:1. 使用在线转换器;2. 使用桌面软件(如 adobe acrobat、itext);3. 使用命令行工具(如 pdftoxml)。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1881

2024.04.01

xml怎么变成word
xml怎么变成word

步骤:1. 导入 xml 文件;2. 选择 xml 结构;3. 映射 xml 元素到 word 元素;4. 生成 word 文档。提示:确保 xml 文件结构良好,并预览 word 文档以验证转换是否成功。想了解更多xml的相关内容,可以阅读本专题下面的文章。

2087

2024.08.01

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

43

2026.01.16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 3.6万人学习

Node.js 教程
Node.js 教程

共57课时 | 8.8万人学习

CSS3 教程
CSS3 教程

共18课时 | 4.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号