• @RequestMapping注解标识的位置


    目录

    @RequestMapping的源码

    一、@RequestMapping注解的功能

    二、@RequestMapping注解的位置

    三、@RequestMapping注解的value属性

    四、@RequestMapping注解的method属性

    ⚪RequestMethod 的源码 

    1.GET方法

    2.POST方法 

    ​编辑​编辑​编辑3.在@RequestMapping注解的基础上,结合请求方式的派生注解 ​编辑

    五、@RequestMapping注解的params属性(了解)

    ⭐页面报错400 

    六、@RequestMapping注解的headers属性(了解)

    七、SpringMVC支持ant风格的路径

    八、SpringMVC支持路径中的占位符(重点)


    @RequestMapping的源码

    1. //
    2. // Source code recreated from a .class file by IntelliJ IDEA
    3. // (powered by FernFlower decompiler)
    4. //
    5. package org.springframework.web.bind.annotation;
    6. import java.lang.annotation.Documented;
    7. import java.lang.annotation.ElementType;
    8. import java.lang.annotation.Retention;
    9. import java.lang.annotation.RetentionPolicy;
    10. import java.lang.annotation.Target;
    11. import org.springframework.core.annotation.AliasFor;
    12. @Target({ElementType.TYPE, ElementType.METHOD})
    13. @Retention(RetentionPolicy.RUNTIME)
    14. @Documented
    15. @Mapping
    16. public @interface RequestMapping {
    17. String name() default "";
    18. @AliasFor("path") //value属性,别名:path,通过请求的请求路径匹配
    19. String[] value() default {};
    20. @AliasFor("value")
    21. String[] path() default {};
    22. RequestMethod[] method() default {}; //通过请求方式匹配
    23. String[] params() default {}; //通过请求参数匹配
    24. String[] headers() default {}; //通过请求头信息匹配
    25. String[] consumes() default {};
    26. String[] produces() default {};
    27. }

    一、@RequestMapping注解的功能

    将请求和处理请求的控制器方法关联起来,建立映射关系。

    SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。

    二、@RequestMapping注解的位置

    • 标识类:设置映射请求的请求路径的初始信息
    • 标识方法:设置映射请求请求路径的具体信息
    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. @Controller
    5. @RequestMapping("/test")
    6. public class TestRequestMappingController {
    7. //此时控制器方法所匹配的请求路径为/test/hello
    8. @RequestMapping("/hello")
    9. public String hello(){
    10. return "success";
    11. }
    12. }

    三、@RequestMapping注解的value属性

    value属性通过请求的请求地址匹配请求映射

    @RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值,则当前请求就会被注解所标识的方法进行处理

    @RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射

    1. import org.springframework.stereotype.Controller;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. @Controller
    4. //@RequestMapping("/test")
    5. public class TestRequestMappingController {
    6. //此时控制器方法所匹配的请求路径为/test/hello
    7. @RequestMapping({"/hello","/abc"})
    8. public String hello(){
    9. return "success";
    10. }
    11. }
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a>
    10. <a th:href="@{/hello}">测试@RequestMapping注解的value属性a>
    11. body>
    12. html>

    四、@RequestMapping注解的method属性

    method属性通过请求的请求方式(get或post)匹配请求映射

    @RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求,则当前请求就会被注解所标识的方法进行处理

    若浏览器所发送的请求的请求路径和@RequestMapping注解的value属性匹配,但是请求方式不满足method属性,则浏览器报错 405:Request method 'XXXX' not supported

    ⚪RequestMethod 的源码 

    1. package org.springframework.web.bind.annotation;
    2. public enum RequestMethod {
    3. GET,
    4. HEAD,
    5. POST,
    6. PUT,
    7. PATCH,
    8. DELETE,
    9. OPTIONS,
    10. TRACE;
    11. private RequestMethod() {
    12. }
    13. }

    示例:

    1.GET方法

    1. @Controller
    2. //@RequestMapping("/test")
    3. public class TestRequestMappingController {
    4. //此时控制器方法所匹配的请求路径为/test/hello
    5. @RequestMapping(
    6. value = {"/hello","/abc"},
    7. method = RequestMethod.GET
    8. )
    9. public String hello(){
    10. return "success";
    11. }
    12. }

    2.POST方法 

    1. @Controller
    2. //@RequestMapping("/test")
    3. public class TestRequestMappingController {
    4. //此时控制器方法所匹配的请求路径为/test/hello
    5. @RequestMapping(
    6. value = {"/hello","/abc"},
    7. method = RequestMethod.POST
    8. )
    9. public String hello(){
    10. return "success";
    11. }
    12. }
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/hello}">测试@RequestMapping注解的value属性a><br>
    11. <form th:action="@{/hello}">
    12. <input type="submit" value="测试@RequestMapping注解的method属性">
    13. form>
    14. body>
    15. html>

    此时点击,报405错误 

    3.在@RequestMapping注解的基础上,结合请求方式的派生注解 

      

    五、@RequestMapping注解的params属性(了解)

    @RequestMapping注解的params属性通过请求的请求参数匹配请求映射 即浏览器发送的请求的请求参数必须满足params属性的设置,是一个字符串类型的数组

    可以通过四种表达式设置请求参数和请求映射的匹配关系

    • "param":表示当前所匹配请求的请求参数中必须携带param请求参数
    • "!param":表示当前所匹配请求的请求参数中必须不能携带param请求参数
    • "param=value":表示当前所匹配请求的请求参数中必须携带param请求参数且param=value
    • "param!=value":表示当前所匹配请求的请求参数中可以不携带param,但是若携带,值一定不能时value

    ⭐页面报错400 

    1. @Controller
    2. //@RequestMapping("/test")
    3. public class TestRequestMappingController {
    4. //此时控制器方法所匹配的请求路径为/test/hello
    5. @RequestMapping(
    6. value = {"/hello","/abc"},
    7. method = {RequestMethod.POST,RequestMethod.GET},
    8. //必须携带username,不能携带password,age=20,gender不为女
    9. params = {"username","!password","age=20","gender!=女"}
    10. )
    11. public String hello(){
    12. return "success";
    13. }
    14. }
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/hello}">测试@RequestMapping注解的value属性a><br>
    11. <form th:action="@{/hello}" method="post">
    12. <input type="submit" value="测试@RequestMapping注解的method属性">
    13. form>
    14. <a th:href="@{/hello?username=admin}">测试@RequestMapping注解的params属性a><br>
    15. <a th:href="@{/hello(username='admin')}">测试@RequestMapping注解的params属性a><br>
    16. body>
    17. html>

    六、@RequestMapping注解的headers属性(了解)

    @RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射 即浏览器发送的请求的请求参数必须满足headers属性的设置,是一个字符串类型的数组

    可以通过四种表达式设置请求头信息和请求映射的匹配关系

    • "header":要求请求映射所匹配的请求必须携带header请求头信息
    • "!header":要求请求映射所匹配的请求必须不能携带header请求头信息
    • "header=value":要求请求映射所匹配的请求必须携带header请求头信息且header=value
    • "header!=value":要求请求映射所匹配的请求必须携带header请求头信息且header!=value

    若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,此时页面 显示404错误,即资源未找到

     

    七、SpringMVC支持ant风格的路径

    在@RequestMapping注解的value属性值中设置一些特殊字符

    • ?:表示任意的单个字符(不包括 ?)
    1. @RequestMapping("/a?a/test/ant")
    2. public String testAnt(){
    3. return "success";
    4. }
    • *  :表示任意的0个或多个字符(不包括  ?和  /  )
    • ** :表示任意层数的任意目录

    注意:在使用**时,只能使用/**/xxx的方式,**之间不允许出现其它的字符,只能将**写在双斜线中

    八、SpringMVC支持路径中的占位符(重点)

    原始方式:/deleteUser?id=1

    rest方式:/user/delete/1

    SpringMVC路径中的占位符常用于RESTful风格中

    当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的@RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据,再通过@PathVariable注解,将占位符所表示的数据赋值给控制器方法的形参

    1. @RequestMapping("/test/rest/{id}")
    2. public String testRest(@PathVariable("id") Integer id){
    3. System.out.println("id:" + id);
    4. return "success";
    5. }

    html: 

    <a th:href="@{/test/rest/1}">测试@RequestMapping注解的value属性中的占位符a><br>

  • 相关阅读:
    产品第一次兼项目经理积累了经验也踩过坑
    Serializable 和Parcelable的区别
    AI准研究生应该掌握的Linux知识
    Memcached 未授权访问漏洞验证
    基于多种设计模式重构代码(工厂、模板、策略)
    第六章 网络学习相关技巧5(超参数验证)
    while循环语句
    在WINDOWS中硬件管理中即插即用的含义是什么
    C++ | Leetcode C++题解之第42题接雨水
    穿越周期的强者:打造“第二招牌”是战略共性
  • 原文地址:https://blog.csdn.net/m0_52982868/article/details/126119126