001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.bind.annotation;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Retention;
022import java.lang.annotation.RetentionPolicy;
023import java.lang.annotation.Target;
024import java.util.Map;
025
026import org.springframework.core.annotation.AliasFor;
027
028/**
029 * Annotation which indicates that a method parameter should be bound to a web
030 * request parameter.
031 *
032 * <p>Supported for annotated handler methods in Spring MVC and Spring WebFlux
033 * as follows:
034 * <ul>
035 * <li>In Spring MVC, "request parameters" map to query parameters, form data,
036 * and parts in multipart requests. This is because the Servlet API combines
037 * query parameters and form data into a single map called "parameters", and
038 * that includes automatic parsing of the request body.
039 * <li>In Spring WebFlux, "request parameters" map to query parameters only.
040 * To work with all 3, query, form data, and multipart data, you can use data
041 * binding to a command object annotated with {@link ModelAttribute}.
042 * </ul>
043 *
044 * <p>If the method parameter type is {@link Map} and a request parameter name
045 * is specified, then the request parameter value is converted to a {@link Map}
046 * assuming an appropriate conversion strategy is available.
047 *
048 * <p>If the method parameter is {@link java.util.Map Map&lt;String, String&gt;} or
049 * {@link org.springframework.util.MultiValueMap MultiValueMap&lt;String, String&gt;}
050 * and a parameter name is not specified, then the map parameter is populated
051 * with all request parameter names and values.
052 *
053 * @author Arjen Poutsma
054 * @author Juergen Hoeller
055 * @author Sam Brannen
056 * @since 2.5
057 * @see RequestMapping
058 * @see RequestHeader
059 * @see CookieValue
060 */
061@Target(ElementType.PARAMETER)
062@Retention(RetentionPolicy.RUNTIME)
063@Documented
064public @interface RequestParam {
065
066        /**
067         * Alias for {@link #name}.
068         */
069        @AliasFor("name")
070        String value() default "";
071
072        /**
073         * The name of the request parameter to bind to.
074         * @since 4.2
075         */
076        @AliasFor("value")
077        String name() default "";
078
079        /**
080         * Whether the parameter is required.
081         * <p>Defaults to {@code true}, leading to an exception being thrown
082         * if the parameter is missing in the request. Switch this to
083         * {@code false} if you prefer a {@code null} value if the parameter is
084         * not present in the request.
085         * <p>Alternatively, provide a {@link #defaultValue}, which implicitly
086         * sets this flag to {@code false}.
087         */
088        boolean required() default true;
089
090        /**
091         * The default value to use as a fallback when the request parameter is
092         * not provided or has an empty value.
093         * <p>Supplying a default value implicitly sets {@link #required} to
094         * {@code false}.
095         */
096        String defaultValue() default ValueConstants.DEFAULT_NONE;
097
098}