Spring学习笔记_36——@RequestMapping

[复制链接]
发表于 2024-11-15 13:58:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1. 先容

@RequestMapping 是 Spring 框架中用于映射 HTTP 哀求到控制器方法的注解。在类上标注了@Controller注解大概@RestController注解之后,同时方法上假如标注了@RestMapping注解,则方法会和某个链接自动建立映射关系,通过链接地址就能够访问到对应的方法
@RequestMapping 可以指定哀求的类型、路径以及其他参数,从而将哀求路由到相应的处理器方法。
2. 利用场景

@RequestMapping注解能够使哀求的URL链接与方法之间建立映射关系,以是在利用SpringMVC大概SpringBoot开发Web应用步伐时,假如必要建立哀求的URL与方法之间的映射关系,都可以利用@RequestMapping注解实现。
假如已经明确哀求的method类型,也可以利用RequestMapping注解的衍生注解
3. 源码

  1. @Target({ElementType.TYPE, ElementType.METHOD})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Mapping
  5. @Reflective({ControllerMappingReflectiveProcessor.class})
  6. public @interface RequestMapping {
  7.                 // 为请求的URL指定一个名字
  8.     String name() default "";
  9.           // 指定请求的URL
  10.     @AliasFor("path")
  11.     String[] value() default {};
  12.           // 从Spring4.2版本开始提供的String数组类型的属性,作用与value属性相同
  13.     @AliasFor("value")
  14.     String[] path() default {};
  15.   
  16.                 // RequestMethod枚举数组类型的属性,用于指定请求的方法,
  17.           // 取值可以为RequestMethod枚举类型中的GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE
  18.           // 既可以指定单个RequestMethod枚举类型,也可以同时指定多个RequestMethod枚举类型,
  19.           // 不指定时,表示处理所有的RequestMethod枚举类型
  20.     RequestMethod[] method() default {};
  21.           // String数组类型的属性,用于指定请求参数,支持简单的表达式。
  22.           // 使用params属性时,要求请求的参数key和Value必须与params属性中配置的Key和Value相同。
  23.           // 可以使用params属性实现同一个URL映射到不同的方法上,请求时根据不同的参数映射到不同的方法上
  24.     String[] params() default {};
  25.   
  26.                 // String数组类型的属性,用于指定请求头信息,主要是限制请求头的信息,
  27.           // 当请求头中必须包含某些指定的头信息时,才能让方法处理请求。
  28.     String[] headers() default {};
  29.           // String数组类型的属性,用于指定要接收的请求体(消息体)的类型,只有满足只写类型的请求才会被处理
  30.           // 不指定时,表示处理所有类型。
  31.           // 支持类似逻辑非操作,
  32.     // 请求体类型,参见org.springframework.http.MediaType
  33.     String[] consumes() default {};
  34.           // String 数组类型的属性,用于指定要响应的消息体类型
  35.           // 指定的类型必须是请求头(Accept)中所包含的类型
  36.           // 当请求头(Accept)中包含指定类型时才会响应结果。
  37.     String[] produces() default {};
  38. }
复制代码
留意:当利用@RequestMapping注解时,只要出现两个或以上的属性时,多个属性之间的关系是与关系,表现必须同时满足条件才会处理
4. Demo

  1. import org.springframework.web.bind.annotation.RequestMapping;
  2. import org.springframework.web.bind.annotation.RequestMethod;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class ExampleController {
  6.           // /greeting 路径只响应带有 name 参数的 GET 请求。
  7.     @RequestMapping(value = "/greeting", method = RequestMethod.GET, params = "name")
  8.     public String greeting(@RequestParam String name) {
  9.         return "Hello, " + name + "!";
  10.     }
  11. }
复制代码
  1. import org.springframework.http.MediaType;
  2. import org.springframework.web.bind.annotation.RequestBody;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class ExampleController {
  8.                 // 定义了一个处理 POST 请求的方法,该方法接受 JSON 格式的数据(通过 consumes 属性指定),并且返回的数据也是 JSON 格式(通过 produces 属性指定)。
  9.     @RequestMapping(value = "/postJson", method = RequestMethod.POST,
  10.                      consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  11.     public User postJson(@RequestBody User user) {
  12.         // 处理接收到的用户数据
  13.         return user;
  14.     }
  15.     static class User {
  16.         private String name;
  17.         private int age;
  18.         // getters and setters
  19.     }
  20. }
复制代码
  1. import org.springframework.web.bind.annotation.RequestMapping;
  2. import org.springframework.web.bind.annotation.RequestMethod;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class ExampleController {
  6.           // /secure 路径仅对携带了特定头部信息 X-Auth-Token=secret 的 GET 请求作出响应。
  7.     @RequestMapping(value = "/secure", method = RequestMethod.GET, headers = "X-Auth-Token=secret")
  8.     public String secure() {
  9.         return "This is a secure area.";
  10.     }
  11. }
复制代码
  1. import org.springframework.web.bind.annotation.RequestMapping;
  2. import org.springframework.web.bind.annotation.RequestMethod;
  3. import org.springframework.web.bind.annotation.RequestParam;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. public class ExampleController {
  7.   
  8.           /**
  9.                   * /multiCondition 路径只响应 PUT 请求,这些请求需要满足以下条件:
  10.                         * 必须包含 id 参数,但不能包含 name 参数。
  11.                         * 必须具有 Content-Type=application/xml 的头部信息。
  12.                         * 请求体必须是 XML 格式。
  13.                         * 响应将被设置为 JSON 格式。
  14.                   */
  15.     @RequestMapping(value = "/multiCondition", method = RequestMethod.PUT,
  16.                      params = {"id", "!name"}, headers = "Content-Type=application/xml",
  17.                      consumes = "application/xml", produces = "application/json")
  18.     public String multiCondition(@RequestParam String id) {
  19.         return "Updated resource with ID: " + id;
  20.     }
  21. }
复制代码
  1. import org.springframework.http.MediaType;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestMethod;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. public class ExampleController {
  7.   
  8.           /**
  9.                   * 限制条件:       
  10.                   * 请求方法为POST
  11.                         * 请求参数中包含param1=value1和param2(param2的值可以是任意值)
  12.                         * 请求头中包含X-Custom-Header=customValue
  13.                         * 请求的内容类型为application/xml
  14.                         * 响应的内容类型为application/json
  15.                         */
  16.     @RequestMapping(value = "/complex", method = RequestMethod.POST,
  17.             params = {"param1=value1", "param2"},
  18.             headers = "X-Custom-Header=customValue",
  19.             consumes = MediaType.APPLICATION_XML_VALUE,
  20.             produces = MediaType.APPLICATION_JSON_VALUE)
  21.     public String handleComplexRequest() {
  22.         return "{"message": "Complex request handled successfully"}";
  23.     }
  24. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表