001/*
002 * Copyright 2002-2016 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;
024
025import org.springframework.core.annotation.AliasFor;
026
027/**
028 * Annotation to bind a method parameter to a request attribute.
029 *
030 * <p>The main motivation is to provide convenient access to request attributes
031 * from a controller method with an optional/required check and a cast to the
032 * target method parameter type.
033 *
034 * @author Rossen Stoyanchev
035 * @since 4.3
036 * @see RequestMapping
037 * @see SessionAttribute
038 */
039@Target(ElementType.PARAMETER)
040@Retention(RetentionPolicy.RUNTIME)
041@Documented
042public @interface RequestAttribute {
043
044        /**
045         * Alias for {@link #name}.
046         */
047        @AliasFor("name")
048        String value() default "";
049
050        /**
051         * The name of the request attribute to bind to.
052         * <p>The default name is inferred from the method parameter name.
053         */
054        @AliasFor("value")
055        String name() default "";
056
057        /**
058         * Whether the request attribute is required.
059         * <p>Defaults to {@code true}, leading to an exception being thrown if
060         * the attribute is missing. Switch this to {@code false} if you prefer
061         * a {@code null} or Java 8 {@code java.util.Optional} if the attribute
062         * doesn't exist.
063         */
064        boolean required() default true;
065
066}