马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
1. 先容
@RequestMapping 是 Spring 框架中用于映射 HTTP 哀求到控制器方法的注解。在类上标注了@Controller注解大概@RestController注解之后,同时方法上假如标注了@RestMapping注解,则方法会和某个链接自动建立映射关系,通过链接地址就能够访问到对应的方法
@RequestMapping 可以指定哀求的类型、路径以及其他参数,从而将哀求路由到相应的处理器方法。
2. 利用场景
@RequestMapping注解能够使哀求的URL链接与方法之间建立映射关系,以是在利用SpringMVC大概SpringBoot开发Web应用步伐时,假如必要建立哀求的URL与方法之间的映射关系,都可以利用@RequestMapping注解实现。
假如已经明确哀求的method类型,也可以利用RequestMapping注解的衍生注解
3. 源码
- @Target({ElementType.TYPE, ElementType.METHOD})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Mapping
- @Reflective({ControllerMappingReflectiveProcessor.class})
- public @interface RequestMapping {
- // 为请求的URL指定一个名字
- String name() default "";
- // 指定请求的URL
- @AliasFor("path")
- String[] value() default {};
- // 从Spring4.2版本开始提供的String数组类型的属性,作用与value属性相同
- @AliasFor("value")
- String[] path() default {};
-
- // RequestMethod枚举数组类型的属性,用于指定请求的方法,
- // 取值可以为RequestMethod枚举类型中的GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE
- // 既可以指定单个RequestMethod枚举类型,也可以同时指定多个RequestMethod枚举类型,
- // 不指定时,表示处理所有的RequestMethod枚举类型
- RequestMethod[] method() default {};
- // String数组类型的属性,用于指定请求参数,支持简单的表达式。
- // 使用params属性时,要求请求的参数key和Value必须与params属性中配置的Key和Value相同。
- // 可以使用params属性实现同一个URL映射到不同的方法上,请求时根据不同的参数映射到不同的方法上
- String[] params() default {};
-
- // String数组类型的属性,用于指定请求头信息,主要是限制请求头的信息,
- // 当请求头中必须包含某些指定的头信息时,才能让方法处理请求。
- String[] headers() default {};
- // String数组类型的属性,用于指定要接收的请求体(消息体)的类型,只有满足只写类型的请求才会被处理
- // 不指定时,表示处理所有类型。
- // 支持类似逻辑非操作,
- // 请求体类型,参见org.springframework.http.MediaType
- String[] consumes() default {};
- // String 数组类型的属性,用于指定要响应的消息体类型
- // 指定的类型必须是请求头(Accept)中所包含的类型
- // 当请求头(Accept)中包含指定类型时才会响应结果。
- String[] produces() default {};
- }
复制代码 留意:当利用@RequestMapping注解时,只要出现两个或以上的属性时,多个属性之间的关系是与关系,表现必须同时满足条件才会处理
4. Demo
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class ExampleController {
- // /greeting 路径只响应带有 name 参数的 GET 请求。
- @RequestMapping(value = "/greeting", method = RequestMethod.GET, params = "name")
- public String greeting(@RequestParam String name) {
- return "Hello, " + name + "!";
- }
- }
复制代码- import org.springframework.http.MediaType;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class ExampleController {
- // 定义了一个处理 POST 请求的方法,该方法接受 JSON 格式的数据(通过 consumes 属性指定),并且返回的数据也是 JSON 格式(通过 produces 属性指定)。
- @RequestMapping(value = "/postJson", method = RequestMethod.POST,
- consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
- public User postJson(@RequestBody User user) {
- // 处理接收到的用户数据
- return user;
- }
- static class User {
- private String name;
- private int age;
- // getters and setters
- }
- }
复制代码- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class ExampleController {
- // /secure 路径仅对携带了特定头部信息 X-Auth-Token=secret 的 GET 请求作出响应。
- @RequestMapping(value = "/secure", method = RequestMethod.GET, headers = "X-Auth-Token=secret")
- public String secure() {
- return "This is a secure area.";
- }
- }
复制代码- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class ExampleController {
-
- /**
- * /multiCondition 路径只响应 PUT 请求,这些请求需要满足以下条件:
- * 必须包含 id 参数,但不能包含 name 参数。
- * 必须具有 Content-Type=application/xml 的头部信息。
- * 请求体必须是 XML 格式。
- * 响应将被设置为 JSON 格式。
- */
- @RequestMapping(value = "/multiCondition", method = RequestMethod.PUT,
- params = {"id", "!name"}, headers = "Content-Type=application/xml",
- consumes = "application/xml", produces = "application/json")
- public String multiCondition(@RequestParam String id) {
- return "Updated resource with ID: " + id;
- }
- }
复制代码- import org.springframework.http.MediaType;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class ExampleController {
-
- /**
- * 限制条件:
- * 请求方法为POST
- * 请求参数中包含param1=value1和param2(param2的值可以是任意值)
- * 请求头中包含X-Custom-Header=customValue
- * 请求的内容类型为application/xml
- * 响应的内容类型为application/json
- */
- @RequestMapping(value = "/complex", method = RequestMethod.POST,
- params = {"param1=value1", "param2"},
- headers = "X-Custom-Header=customValue",
- consumes = MediaType.APPLICATION_XML_VALUE,
- produces = MediaType.APPLICATION_JSON_VALUE)
- public String handleComplexRequest() {
- return "{"message": "Complex request handled successfully"}";
- }
- }
复制代码 |