Spring Boot基础功能:控制器

@Controller标识一个类是控制器,控制器是 Spring MVC 框架中的组件,用于处理客户端请求并返回视图或数据给客户端。Spring Boot 的 @Controller 注解使得创建 Web 控制器变得简单,它可以处理不同类型的请求,执行相应的业务逻辑,并返回适当的响应。

1 关联注解

控制器的功能大部分都是通过注解完成,于其去学习使用,不如直接看注解的定义。

1.1 @Controller

用于将一个类标识为控制器,处理客户端的请求并返回视图或数据。

注解定义:

package org.springframework.stereotype;

......

/**
 * Indicates that an annotated class is a "Controller" (e.g. a web controller).
 *
 * <p>This annotation serves as a specialization of {@link Component @Component},
 * allowing for implementation classes to be autodetected through classpath scanning.
 * It is typically used in combination with annotated handler methods based on the
 * {@link org.springframework.web.bind.annotation.RequestMapping} annotation.
 *
 * @author Arjen Poutsma
 * @author Juergen Hoeller
 * @since 2.5
 * @see Component
 * @see org.springframework.web.bind.annotation.RequestMapping
 * @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {

    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * @return the suggested component name, if any (or empty String otherwise)
     */
    @AliasFor(annotation = Component.class)
    String value() default "";

}

继承自@Component注解, 接受一个参数作为组件名称。

1.2 @RestController

@Controller相似,但专用于restful风格的控制器,自动将方法返回的对象序列化为JSON响应;

注解定义:
···
package org.springframework.web.bind.annotation;

......

/**

  • A convenience annotation that is itself annotated with
  • {@link Controller @Controller} and {@link ResponseBody @ResponseBody}.
  • Types that carry this annotation are treated as controllers where
  • {@link RequestMapping @RequestMapping} methods assume
  • {@link ResponseBody @ResponseBody} semantics by default.
  • OTE: {@code @RestController} is processed if an appropriate
  • {@code HandlerMapping}-{@code HandlerAdapter} pair is configured such as the
  • {@code RequestMappingHandlerMapping}-{@code RequestMappingHandlerAdapter}
  • pair which are the default in the MVC Java config and the MVC namespace.
  • @author Rossen Stoyanchev
  • @author Sam Brannen
  • @since 4.0
    */
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Controller
    @ResponseBody
    public @interface RestController {

    /**

    • The value may indicate a suggestion for a logical component name,
    • to be turned into a Spring bean in case of an autodetected component.
    • @return the suggested component name, if any (or empty String otherwise)
    • @since 4.0.1
      */
      @AliasFor(annotation = Controller.class)
      String value() default "";

}

···

继承自@Controller注解,接受一个参数作为主键名称。

1.3 @RequestMapping

用于映射请求路径和HTTP方法到控制器方法,可用于类别和方法级别。

注解定义:
package org.springframework.web.bind.annotation;

/**
* 用于将web请求映射到具有灵活方法签名的请求处理类中的方法的注释。
*
* <p>Spring MVC和Spring WebFlux都通过各自模块和包结构中的
* {@code RequestMappingHandlerMapping}和{@code Request MappingHandlerAdapter}来支持此注释。
* 有关每个中支持的处理程序方法参数和返回类型的确切列表,请使用下面的参考文档链接:
* <ul>
* <li>Spring MVC
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-arguments">Method Arguments</a>
* and
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-return-types">Return Values</a>
* </li>
* <li>Spring WebFlux
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-ann-arguments">Method Arguments</a>
* and
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-ann-return-types">Return Values</a>
* </li>
* </ul>
*
* <p><strong>注意:</strong> This annotation can be used both at the class and
* at the method level. In most cases, at the method level applications will
* prefer to use one of the HTTP method specific variants
* {@link GetMapping @GetMapping}, {@link PostMapping @PostMapping},
* {@link PutMapping @PutMapping}, {@link DeleteMapping @DeleteMapping}, or
* {@link PatchMapping @PatchMapping}.</p>
*
* <p><b>NOTE:</b> When using controller interfaces (e.g. for AOP proxying),
* make sure to consistently put <i>all</i> your mapping annotations - such as
* {@code @RequestMapping} and {@code @SessionAttributes} - on
* the controller <i>interface</i> rather than on the implementation class.
*
* @author Juergen Hoeller
* @author Arjen Poutsma
* @author Sam Brannen
* @since 2.5
* @see GetMapping
* @see PostMapping
* @see PutMapping
* @see DeleteMapping
* @see PatchMapping
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
@Reflective(ControllerMappingReflectiveProcessor.class)
public @interface RequestMapping {
/**
* 为此Mapping指定名称.
* <p><b>类和方法级别都支持这个属性!</b>
* 当在两个级别上使用时,组合名称是通过将“#”作为分隔符串联而派生的
* @see org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder
* @see org.springframework.web.servlet.handler.HandlerMethodMappingNamingStrategy
*/
String name() default "";
/**
* 主要映射由此注解表示。
* <p>这是{@link #path}的别名。例如,
* {@code @RequestMapping("/foo")} 等同于
* {@code @RequestMapping(path="/foo")}。
* <p><b>不仅支持在方法级别,还支持在类型级别!</b>
* 当在类型级别使用时,所有方法级别的映射都会继承
* 此主要映射,可以缩小特定处理程序方法的范围。
* <p><strong>注意</strong>:一个没有被显式映射到任何路径的处理程序方法
* 实际上被映射到一个空路径。
*/
@AliasFor("path")
String[] value() default {};
/**
* 路径映射的URI(例如 {@code "/profile"})。
* <p>还支持Ant风格的路径模式(例如 {@code "/profile/**"})。
* 在方法级别,相对路径(例如 {@code "edit"})在类型级别的主要映射中得到支持。
* 路径映射的URI可能包含占位符(例如 <code>"/${profile_path}"</code>)。
* <p><b>不仅支持在方法级别,还支持在类型级别!</b>
* 在类型级别使用时,所有方法级别的映射都会继承此主要映射,
* 可以缩小特定处理程序方法的范围。
* <p><strong>注意</strong>:一个没有明确映射到任何路径的处理程序方法
* 实际上被映射到一个空路径。
* @since 4.2
*/
@AliasFor("value")
String[] path() default {};
/**
* 映射到的HTTP请求方法,用于缩小主要映射范围:
* GET、POST、HEAD、OPTIONS、PUT、PATCH、DELETE、TRACE。
* <p><b>不仅支持在方法级别,还支持在类型级别!</b>
* 在类型级别使用时,所有方法级别的映射都会继承这个HTTP方法限制。
*/
RequestMethod[] method() default {};
/**
* 映射请求的参数,用于缩小主要映射范围。
* <p>在任何环境中都使用相同的格式:一系列“myParam=myValue”样式的表达式,
* 只有当每个这样的参数被发现具有给定值时,请求才会被映射。
* 可以通过使用“!=”操作符来否定表达式,例如“myParam!=myValue”。
* 还支持“myParam”样式的表达式,这些参数必须在请求中存在(允许具有任何值)。
* 最后,“!myParam”样式的表达式表示请求中不应该出现指定的参数。
* <p><b>不仅支持在方法级别,还支持在类型级别!</b>
* 在类型级别使用时,所有方法级别的映射都会继承这个参数限制。
*/
String[] params() default {};
/**
* 映射请求的头部,用于缩小主要映射范围。
* <p>在任何环境中都使用相同的格式:一系列“My-Header=myValue”样式的表达式,
* 只有当每个这样的头部被发现具有给定值时,请求才会被映射。
* 可以通过使用“!=”操作符来否定表达式,例如“My-Header!=myValue”。
* 还支持“My-Header”样式的表达式,这些头部必须在请求中存在(允许具有任何值)。
* 最后,“!My-Header”样式的表达式表示请求中不应该出现指定的头部。
* <p>还支持媒体类型通配符(*),适用于Accept和Content-Type等头部。例如,
* <pre class="code">
* @RequestMapping(value = "/something", headers = "content-type=text/*")
* </pre>
* 将匹配Content-Type为“text/html”、“text/plain”等的请求。
* <p><b>不仅支持在方法级别,还支持在类型级别!</b>
* 在类型级别使用时,所有方法级别的映射都会继承这个头部限制。
* @see org.springframework.http.MediaType
*/
String[] headers() default {};
/**
* 通过可以被映射处理程序消耗的媒体类型来缩小主要映射。
* 包括一个或多个媒体类型,其中之一必须与请求的 {@code Content-Type} 头部匹配。示例:
* <pre class="code">
* consumes = "text/plain"
* consumes = {"text/plain", "application/*"}
* consumes = MediaType.TEXT_PLAIN_VALUE
* </pre>
* <p>如果声明的媒体类型包含参数,并且请求的 {@code "content-type"} 也具有该参数,
* 则参数值必须匹配。否则,如果请求的媒体类型 {@code "content-type"} 不包含该参数,
* 则参数在匹配目的上会被忽略。
* <p>可以通过使用 "!" 操作符来否定表达式,例如 "!text/plain",
* 它匹配除了 "text/plain" 之外的所有带有 {@code Content-Type} 的请求。
* <p><b>不仅支持在方法级别,还支持在类型级别!</b>
* 如果在两个级别都指定了,方法级别的消耗条件将覆盖类型级别的条件。
* @see org.springframework.http.MediaType
* @see jakarta.servlet.http.HttpServletRequest#getContentType()
*/
String[] consumes() default {};
/**
* 通过可以被映射处理程序生成的媒体类型来缩小主要映射。
* 包括一个或多个媒体类型,其中之一必须通过与请求的“可接受”媒体类型进行内容协商来选择。
* 通常,这些媒体类型是从{@code "Accept"}头部中提取的,但也可以从查询参数或其他地方派生。
* 示例:
* <pre class="code">
* produces = "text/plain"
* produces = {"text/plain", "application/*"}
* produces = MediaType.TEXT_PLAIN_VALUE
* produces = "text/plain;charset=UTF-8"
* </pre>
* <p>如果声明的媒体类型包含参数(例如“charset=UTF-8”,“type=feed”,“type=entry”),
* 并且如果请求中的兼容媒体类型也具有该参数,则参数值必须匹配。
* 否则,如果请求的媒体类型不包含该参数,则假定客户端接受任何值。
* <p>可以通过使用 "!" 操作符来否定表达式,例如 "!text/plain",
* 它匹配所有带有 {@code Accept} 除了 "text/plain" 之外的请求。
* <p><b>不仅支持在方法级别,还支持在类型级别!</b>
* 如果在两个级别都指定了,方法级别的生成条件将覆盖类型级别的条件。
* @see org.springframework.http.MediaType
*/
String[] produces() default {};
}

valuepath没啥区别,互为注解。

1.4 @GetMapping

快捷方式注解,用于映射 GET 请求到控制器方法。

package org.springframework.web.bind.annotation;
/**
* Annotation for mapping HTTP {@code GET} requests onto specific handler
* methods.
*
* 

Specifically, {@code @GetMapping} is a composed annotation that * acts as a shortcut for {@code @RequestMapping(method = RequestMethod.GET)}. * * @author Sam Brannen * @since 4.3 * @see PostMapping * @see PutMapping * @see DeleteMapping * @see PatchMapping * @see RequestMapping */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @RequestMapping(method = RequestMethod.GET) public @interface GetMapping { /** * Alias for {@link RequestMapping#name}. */ @AliasFor(annotation = RequestMapping.class) String name() default ""; /** * Alias for {@link RequestMapping#value}. */ @AliasFor(annotation = RequestMapping.class) String[] value() default {}; /** * Alias for {@link RequestMapping#path}. */ @AliasFor(annotation = RequestMapping.class) String[] path() default {}; /** * Alias for {@link RequestMapping#params}. */ @AliasFor(annotation = RequestMapping.class) String[] params() default {}; /** * Alias for {@link RequestMapping#headers}. */ @AliasFor(annotation = RequestMapping.class) String[] headers() default {}; /** * Alias for {@link RequestMapping#consumes}. * @since 4.3.5 */ @AliasFor(annotation = RequestMapping.class) String[] consumes() default {}; /** * Alias for {@link RequestMapping#produces}. */ @AliasFor(annotation = RequestMapping.class) String[] produces() default {}; }

继承自注解`@RequestMappping`, 固定method属性为 `RequestMethod.GET`。

1.5 @PostMapping

快捷方式注解,用于映射 POST 请求到控制器方法。

1.6 @PutMapping

快捷方式注解,用于映射 PUT 请求到控制器方法。

1.7 @DeleteMapping

快捷方式注解,用于映射 DELETE 请求到控制器方法。

1.8 @PatchMapping

快捷方式注解,用于映射 PATCH 请求到控制器方法。

1.9 @RequestParam

用于将请求参数映射到方法参数,可设置默认值、是否必需等选项。

注解定义:

package org.springframework.web.bind.annotation;
......
/**
* Annotation which indicates that a method parameter should be bound to a web
* request parameter.
*
* 

Supported for annotated handler methods in Spring MVC and Spring WebFlux * as follows: *

    *
  • In Spring MVC, "request parameters" map to query parameters, form data, * and parts in multipart requests. This is because the Servlet API combines * query parameters and form data into a single map called "parameters", and * that includes automatic parsing of the request body. *
  • In Spring WebFlux, "request parameters" map to query parameters only. * To work with all 3, query, form data, and multipart data, you can use data * binding to a command object annotated with {@link ModelAttribute}. *
* *

If the method parameter type is {@link Map} and a request parameter name * is specified, then the request parameter value is converted to a {@link Map} * assuming an appropriate conversion strategy is available. * *

If the method parameter is {@link java.util.Map Map<String, String>} or * {@link org.springframework.util.MultiValueMap MultiValueMap<String, String>} * and a parameter name is not specified, then the map parameter is populated * with all request parameter names and values. * * @author Arjen Poutsma * @author Juergen Hoeller * @author Sam Brannen * @since 2.5 * @see RequestMapping * @see RequestHeader * @see CookieValue */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestParam { /** * Alias for {@link #name}. */ @AliasFor("name") String value() default ""; /** * The name of the request parameter to bind to. * @since 4.2 */ @AliasFor("value") String name() default ""; /** * Whether the parameter is required. *

Defaults to {@code true}, leading to an exception being thrown * if the parameter is missing in the request. Switch this to * {@code false} if you prefer a {@code null} value if the parameter is * not present in the request. *

Alternatively, provide a {@link #defaultValue}, which implicitly * sets this flag to {@code false}. */ boolean required() default true; /** * The default value to use as a fallback when the request parameter is * not provided or has an empty value. *

Supplying a default value implicitly sets {@link #required} to * {@code false}. */ String defaultValue() default ValueConstants.DEFAULT_NONE; }

1.10 @PathVariable

用于将路径变量映射到方法参数。

注解定义:

package org.springframework.web.bind.annotation;
/**
* Annotation which indicates that a method parameter should be bound to a URI template
* variable. Supported for {@link RequestMapping} annotated handler methods.
*
* 

If the method parameter is {@link java.util.Map Map<String, String>} * then the map is populated with all path variable names and values. * * @author Arjen Poutsma * @author Juergen Hoeller * @since 3.0 * @see RequestMapping * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface PathVariable { /** * Alias for {@link #name}. */ @AliasFor("name") String value() default ""; /** * The name of the path variable to bind to. * @since 4.3.3 */ @AliasFor("value") String name() default ""; /** * Whether the path variable is required. *

Defaults to {@code true}, leading to an exception being thrown if the path * variable is missing in the incoming request. Switch this to {@code false} if * you prefer a {@code null} or Java 8 {@code java.util.Optional} in this case. * e.g. on a {@code ModelAttribute} method which serves for different requests. * @since 4.3.3 */ boolean required() default true; }

1.11 @RequestHeader

用于将请求头部信息映射到方法参数。

注解定义:

package org.springframework.web.bind.annotation;
......
/**
* Annotation which indicates that a method parameter should be bound to a web request header.
*
* 

Supported for annotated handler methods in Spring MVC and Spring WebFlux. * *

If the method parameter is {@link java.util.Map Map<String, String>}, * {@link org.springframework.util.MultiValueMap MultiValueMap<String, String>}, * or {@link org.springframework.http.HttpHeaders HttpHeaders} then the map is * populated with all header names and values. * * @author Juergen Hoeller * @author Sam Brannen * @since 3.0 * @see RequestMapping * @see RequestParam * @see CookieValue */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestHeader { /** * Alias for {@link #name}. */ @AliasFor("name") String value() default ""; /** * The name of the request header to bind to. * @since 4.2 */ @AliasFor("value") String name() default ""; /** * Whether the header is required. *

Defaults to {@code true}, leading to an exception being thrown * if the header is missing in the request. Switch this to * {@code false} if you prefer a {@code null} value if the header is * not present in the request. *

Alternatively, provide a {@link #defaultValue}, which implicitly * sets this flag to {@code false}. */ boolean required() default true; /** * The default value to use as a fallback. *

Supplying a default value implicitly sets {@link #required} to * {@code false}. */ String defaultValue() default ValueConstants.DEFAULT_NONE; }

1.12 @RequestBody

用于将请求体映射到方法参数,通常用于处理 POST 请求的数据。

注解定义:

package org.springframework.web.bind.annotation;
...
/**
* Annotation indicating a method parameter should be bound to the body of the web request.
* The body of the request is passed through an {@link HttpMessageConverter} to resolve the
* method argument depending on the content type of the request. Optionally, automatic
* validation can be applied by annotating the argument with {@code @Valid}.
*
* 

Supported for annotated handler methods. * * @author Arjen Poutsma * @since 3.0 * @see RequestHeader * @see ResponseBody * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestBody { /** * Whether body content is required. *

Default is {@code true}, leading to an exception thrown in case * there is no body content. Switch this to {@code false} if you prefer * {@code null} to be passed when the body content is {@code null}. * @since 3.2 */ boolean required() default true; }

1.13 @ResponseBody

用于将方法的返回值作为响应体返回给客户端,通常用于Restful接口。

注解定义

package org.springframework.web.bind.annotation;
...
/**
* Annotation that indicates a method return value should be bound to the web
* response body. Supported for annotated handler methods.
*
* 

As of version 4.0 this annotation can also be added on the type level in * which case it is inherited and does not need to be added on the method level. * * @author Arjen Poutsma * @since 3.0 * @see RequestBody * @see RestController */ @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ResponseBody { }

1.14 @ModelAttribute

将请求参数绑定到模型对象,通常用于表单提交。

注解定义:

package org.springframework.web.bind.annotation;
......
/**
* Annotation that binds a method parameter or method return value
* to a named model attribute, exposed to a web view. Supported
* for controller classes with {@link RequestMapping @RequestMapping}
* methods.
*
* 

WARNING: Data binding can lead to security issues by exposing * parts of the object graph that are not meant to be accessed or modified by * external clients. Therefore the design and use of data binding should be considered * carefully with regard to security. For more details, please refer to the dedicated * sections on data binding for * Spring Web MVC and * Spring WebFlux * in the reference manual. * *

{@code @ModelAttribute} can be used to expose command objects to a web view, * using specific attribute names, by annotating corresponding parameters of an * {@link RequestMapping @RequestMapping} method. * *

{@code @ModelAttribute} can also be used to expose reference data to a web * view by annotating accessor methods in a controller class with * {@link RequestMapping @RequestMapping} methods. Such accessor * methods are allowed to have any arguments that * {@link RequestMapping @RequestMapping} methods support, returning * the model attribute value to expose. * *

Note however that reference data and all other model content are * not available to web views when request processing results in an * {@code Exception} since the exception could be raised at any time * making the content of the model unreliable. For this reason * {@link ExceptionHandler @ExceptionHandler} methods do not provide * access to a {@link Model} argument. * * @author Juergen Hoeller * @author Rossen Stoyanchev * @author Sebastien Deleuze * @since 2.5 * @see ControllerAdvice */ @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Reflective public @interface ModelAttribute { /** * Alias for {@link #name}. */ @AliasFor("name") String value() default ""; /** * The name of the model attribute to bind to. *

The default model attribute name is inferred from the declared * attribute type (i.e. the method parameter type or method return type), * based on the non-qualified class name: * e.g. "orderAddress" for class "mypackage.OrderAddress", * or "orderAddressList" for "List<mypackage.OrderAddress>". * @since 4.3 */ @AliasFor("value") String name() default ""; /** * Allows data binding to be disabled directly on an {@code @ModelAttribute} * method parameter or on the attribute returned from an {@code @ModelAttribute} * method, both of which would prevent data binding for that attribute. *

By default this is set to {@code true} in which case data binding applies. * Set this to {@code false} to disable data binding. * @since 4.3 */ boolean binding() default true; }

1.15 SessionAttributes

用于将模型属性存储到回话中,以便多个请求之间共享。

注解定义:

package org.springframework.web.bind.annotation;
....
/**
* Annotation that indicates the session attributes that a specific handler uses.
*
* 

This will typically list the names of model attributes which should be * transparently stored in the session or some conversational storage, * serving as form-backing beans. Declared at the type level, applying * to the model attributes that the annotated handler class operates on. * *

NOTE: Session attributes as indicated using this annotation * correspond to a specific handler's model attributes, getting transparently * stored in a conversational session. Those attributes will be removed once * the handler indicates completion of its conversational session. Therefore, * use this facility for such conversational attributes which are supposed * to be stored in the session temporarily during the course of a * specific handler's conversation. * *

For permanent session attributes, e.g. a user authentication object, * use the traditional {@code session.setAttribute} method instead. * Alternatively, consider using the attribute management capabilities of the * generic {@link org.springframework.web.context.request.WebRequest} interface. * *

NOTE: When using controller interfaces (e.g. for AOP proxying), * make sure to consistently put all your mapping annotations — * such as {@code @RequestMapping} and {@code @SessionAttributes} — on * the controller interface rather than on the implementation class. * * @author Juergen Hoeller * @author Sam Brannen * @since 2.5 */ @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface SessionAttributes { /** * Alias for {@link #names}. */ @AliasFor("names") String[] value() default {}; /** * The names of session attributes in the model that should be stored in the * session or some conversational storage. *

Note: This indicates the model attribute names. * The session attribute names may or may not match the model attribute * names. Applications should therefore not rely on the session attribute * names but rather operate on the model only. * @since 4.2 */ @AliasFor("value") String[] names() default {}; /** * The types of session attributes in the model that should be stored in the * session or some conversational storage. *

All model attributes of these types will be stored in the session, * regardless of attribute name. */ Class[] types() default {}; }

1.16 @CookieValue

用于将请求中的Cookie值映射到方法参数。

注解定义:

package org.springframework.web.bind.annotation;
.... 
/**
* Annotation to indicate that a method parameter is bound to an HTTP cookie.
*
* 

The method parameter may be declared as type {@link jakarta.servlet.http.Cookie} * or as cookie value type (String, int, etc.). * *

Note that with spring-webmvc 5.3.x and earlier, the cookie value is URL * decoded. This will be changed in 6.0 but in the meantime, applications can * also declare parameters of type {@link jakarta.servlet.http.Cookie} to access * the raw value. * * @author Juergen Hoeller * @author Sam Brannen * @since 3.0 * @see RequestMapping * @see RequestParam * @see RequestHeader * @see org.springframework.web.bind.annotation.RequestMapping */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface CookieValue { /** * Alias for {@link #name}. */ @AliasFor("name") String value() default ""; /** * The name of the cookie to bind to. * @since 4.2 */ @AliasFor("value") String name() default ""; /** * Whether the cookie is required. *

Defaults to {@code true}, leading to an exception being thrown * if the cookie is missing in the request. Switch this to * {@code false} if you prefer a {@code null} value if the cookie is * not present in the request. *

Alternatively, provide a {@link #defaultValue}, which implicitly * sets this flag to {@code false}. */ boolean required() default true; /** * The default value to use as a fallback. *

Supplying a default value implicitly sets {@link #required} to * {@code false}. */ String defaultValue() default ValueConstants.DEFAULT_NONE; }

发表回复

您的电子邮箱地址不会被公开。