|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
1 | 17 | package org.springframework.web.bind.annotation;
|
2 | 18 |
|
3 | 19 | import java.lang.annotation.Documented;
|
4 | 20 | import java.lang.annotation.ElementType;
|
5 | 21 | import java.lang.annotation.Retention;
|
6 | 22 | import java.lang.annotation.RetentionPolicy;
|
7 | 23 | import java.lang.annotation.Target;
|
| 24 | + |
8 | 25 | import org.springframework.core.annotation.AliasFor;
|
9 | 26 |
|
10 |
| -@Target(value={ElementType.METHOD,ElementType.TYPE}) |
11 |
| -@Retention(value=RetentionPolicy.RUNTIME) |
| 27 | +/** |
| 28 | + * Annotation for mapping web requests onto methods in request-handling classes |
| 29 | + * with flexible method signatures. |
| 30 | + * |
| 31 | + * <p>Both Spring MVC and Spring WebFlux support this annotation through a |
| 32 | + * {@code RequestMappingHandlerMapping} and {@code RequestMappingHandlerAdapter} |
| 33 | + * in their respective modules and package structure. For the exact list of |
| 34 | + * supported handler method arguments and return types in each, please use the |
| 35 | + * reference documentation links below: |
| 36 | + * <ul> |
| 37 | + * <li>Spring MVC |
| 38 | + * <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-arguments">Method Arguments</a> |
| 39 | + * and |
| 40 | + * <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-return-types">Return Values</a> |
| 41 | + * </li> |
| 42 | + * <li>Spring WebFlux |
| 43 | + * <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-ann-arguments">Method Arguments</a> |
| 44 | + * and |
| 45 | + * <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-ann-return-types">Return Values</a> |
| 46 | + * </li> |
| 47 | + * </ul> |
| 48 | + * |
| 49 | + * <p><strong>Note:</strong> This annotation can be used both at the class and |
| 50 | + * at the method level. In most cases, at the method level applications will |
| 51 | + * prefer to use one of the HTTP method specific variants |
| 52 | + * {@link GetMapping @GetMapping}, {@link PostMapping @PostMapping}, |
| 53 | + * {@link PutMapping @PutMapping}, {@link DeleteMapping @DeleteMapping}, or |
| 54 | + * {@link PatchMapping @PatchMapping}.</p> |
| 55 | + * |
| 56 | + * <p><b>NOTE:</b> When using controller interfaces (e.g. for AOP proxying), |
| 57 | + * make sure to consistently put <i>all</i> your mapping annotations - such as |
| 58 | + * {@code @RequestMapping} and {@code @SessionAttributes} - on |
| 59 | + * the controller <i>interface</i> rather than on the implementation class. |
| 60 | + * |
| 61 | + * @author Juergen Hoeller |
| 62 | + * @author Arjen Poutsma |
| 63 | + * @author Sam Brannen |
| 64 | + * @since 2.5 |
| 65 | + * @see GetMapping |
| 66 | + * @see PostMapping |
| 67 | + * @see PutMapping |
| 68 | + * @see DeleteMapping |
| 69 | + * @see PatchMapping |
| 70 | + */ |
| 71 | +@Target({ElementType.TYPE, ElementType.METHOD}) |
| 72 | +@Retention(RetentionPolicy.RUNTIME) |
12 | 73 | @Documented
|
13 | 74 | @Mapping
|
14 | 75 | public @interface RequestMapping {
|
15 |
| - String name() default ""; |
16 | 76 |
|
17 |
| - @AliasFor("path") |
18 |
| - String[] value() default {}; |
| 77 | + /** |
| 78 | + * Assign a name to this mapping. |
| 79 | + * <p><b>Supported at the type level as well as at the method level!</b> |
| 80 | + * When used on both levels, a combined name is derived by concatenation |
| 81 | + * with "#" as separator. |
| 82 | + * @see org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder |
| 83 | + * @see org.springframework.web.servlet.handler.HandlerMethodMappingNamingStrategy |
| 84 | + */ |
| 85 | + String name() default ""; |
| 86 | + |
| 87 | + /** |
| 88 | + * The primary mapping expressed by this annotation. |
| 89 | + * <p>This is an alias for {@link #path}. For example, |
| 90 | + * {@code @RequestMapping("/foo")} is equivalent to |
| 91 | + * {@code @RequestMapping(path="/foo")}. |
| 92 | + * <p><b>Supported at the type level as well as at the method level!</b> |
| 93 | + * When used at the type level, all method-level mappings inherit |
| 94 | + * this primary mapping, narrowing it for a specific handler method. |
| 95 | + * <p><strong>NOTE</strong>: A handler method that is not mapped to any path |
| 96 | + * explicitly is effectively mapped to an empty path. |
| 97 | + */ |
| 98 | + @AliasFor("path") |
| 99 | + String[] value() default {}; |
| 100 | + |
| 101 | + /** |
| 102 | + * The path mapping URIs (e.g. {@code "/profile"}). |
| 103 | + * <p>Ant-style path patterns are also supported (e.g. {@code "/profile/**"}). |
| 104 | + * At the method level, relative paths (e.g. {@code "edit"}) are supported |
| 105 | + * within the primary mapping expressed at the type level. |
| 106 | + * Path mapping URIs may contain placeholders (e.g. <code>"/${profile_path}"</code>). |
| 107 | + * <p><b>Supported at the type level as well as at the method level!</b> |
| 108 | + * When used at the type level, all method-level mappings inherit |
| 109 | + * this primary mapping, narrowing it for a specific handler method. |
| 110 | + * <p><strong>NOTE</strong>: A handler method that is not mapped to any path |
| 111 | + * explicitly is effectively mapped to an empty path. |
| 112 | + * @since 4.2 |
| 113 | + */ |
| 114 | + @AliasFor("value") |
| 115 | + String[] path() default {}; |
19 | 116 |
|
20 |
| - @AliasFor("value") |
21 |
| - String[] path() default {}; |
| 117 | + /** |
| 118 | + * The HTTP request methods to map to, narrowing the primary mapping: |
| 119 | + * GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE. |
| 120 | + * <p><b>Supported at the type level as well as at the method level!</b> |
| 121 | + * When used at the type level, all method-level mappings inherit this |
| 122 | + * HTTP method restriction. |
| 123 | + */ |
| 124 | + RequestMethod[] method() default {}; |
22 | 125 |
|
23 |
| - RequestMethod[] method() default {}; |
| 126 | + /** |
| 127 | + * The parameters of the mapped request, narrowing the primary mapping. |
| 128 | + * <p>Same format for any environment: a sequence of "myParam=myValue" style |
| 129 | + * expressions, with a request only mapped if each such parameter is found |
| 130 | + * to have the given value. Expressions can be negated by using the "!=" operator, |
| 131 | + * as in "myParam!=myValue". "myParam" style expressions are also supported, |
| 132 | + * with such parameters having to be present in the request (allowed to have |
| 133 | + * any value). Finally, "!myParam" style expressions indicate that the |
| 134 | + * specified parameter is <i>not</i> supposed to be present in the request. |
| 135 | + * <p><b>Supported at the type level as well as at the method level!</b> |
| 136 | + * When used at the type level, all method-level mappings inherit this |
| 137 | + * parameter restriction. |
| 138 | + */ |
| 139 | + String[] params() default {}; |
24 | 140 |
|
25 |
| - String[] params() default {}; |
| 141 | + /** |
| 142 | + * The headers of the mapped request, narrowing the primary mapping. |
| 143 | + * <p>Same format for any environment: a sequence of "My-Header=myValue" style |
| 144 | + * expressions, with a request only mapped if each such header is found |
| 145 | + * to have the given value. Expressions can be negated by using the "!=" operator, |
| 146 | + * as in "My-Header!=myValue". "My-Header" style expressions are also supported, |
| 147 | + * with such headers having to be present in the request (allowed to have |
| 148 | + * any value). Finally, "!My-Header" style expressions indicate that the |
| 149 | + * specified header is <i>not</i> supposed to be present in the request. |
| 150 | + * <p>Also supports media type wildcards (*), for headers such as Accept |
| 151 | + * and Content-Type. For instance, |
| 152 | + * <pre class="code"> |
| 153 | + * @RequestMapping(value = "/something", headers = "content-type=text/*") |
| 154 | + * </pre> |
| 155 | + * will match requests with a Content-Type of "text/html", "text/plain", etc. |
| 156 | + * <p><b>Supported at the type level as well as at the method level!</b> |
| 157 | + * When used at the type level, all method-level mappings inherit this |
| 158 | + * header restriction. |
| 159 | + * @see org.springframework.http.MediaType |
| 160 | + */ |
| 161 | + String[] headers() default {}; |
26 | 162 |
|
27 |
| - String[] headers() default {}; |
| 163 | + /** |
| 164 | + * Narrows the primary mapping by media types that can be consumed by the |
| 165 | + * mapped handler. Consists of one or more media types one of which must |
| 166 | + * match to the request {@code Content-Type} header. Examples: |
| 167 | + * <pre class="code"> |
| 168 | + * consumes = "text/plain" |
| 169 | + * consumes = {"text/plain", "application/*"} |
| 170 | + * consumes = MediaType.TEXT_PLAIN_VALUE |
| 171 | + * </pre> |
| 172 | + * Expressions can be negated by using the "!" operator, as in |
| 173 | + * "!text/plain", which matches all requests with a {@code Content-Type} |
| 174 | + * other than "text/plain". |
| 175 | + * <p><b>Supported at the type level as well as at the method level!</b> |
| 176 | + * If specified at both levels, the method level consumes condition overrides |
| 177 | + * the type level condition. |
| 178 | + * @see org.springframework.http.MediaType |
| 179 | + * @see javax.servlet.http.HttpServletRequest#getContentType() |
| 180 | + */ |
| 181 | + String[] consumes() default {}; |
28 | 182 |
|
29 |
| - String[] consumes() default {}; |
| 183 | + /** |
| 184 | + * Narrows the primary mapping by media types that can be produced by the |
| 185 | + * mapped handler. Consists of one or more media types one of which must |
| 186 | + * be chosen via content negotiation against the "acceptable" media types |
| 187 | + * of the request. Typically those are extracted from the {@code "Accept"} |
| 188 | + * header but may be derived from query parameters, or other. Examples: |
| 189 | + * <pre class="code"> |
| 190 | + * produces = "text/plain" |
| 191 | + * produces = {"text/plain", "application/*"} |
| 192 | + * produces = MediaType.TEXT_PLAIN_VALUE |
| 193 | + * produces = "text/plain;charset=UTF-8" |
| 194 | + * </pre> |
| 195 | + * <p>If a declared media type contains a parameter (e.g. "charset=UTF-8", |
| 196 | + * "type=feed", "type=entry") and if a compatible media type from the request |
| 197 | + * has that parameter too, then the parameter values must match. Otherwise |
| 198 | + * if the media type from the request does not contain the parameter, it is |
| 199 | + * assumed the client accepts any value. |
| 200 | + * <p>Expressions can be negated by using the "!" operator, as in "!text/plain", |
| 201 | + * which matches all requests with a {@code Accept} other than "text/plain". |
| 202 | + * <p><b>Supported at the type level as well as at the method level!</b> |
| 203 | + * If specified at both levels, the method level produces condition overrides |
| 204 | + * the type level condition. |
| 205 | + * @see org.springframework.http.MediaType |
| 206 | + */ |
| 207 | + String[] produces() default {}; |
30 | 208 |
|
31 |
| - String[] produces() default {}; |
32 | 209 | }
|
0 commit comments