001/*
002 * Copyright 2002-2019 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.Annotation;
020import java.lang.annotation.Documented;
021import java.lang.annotation.ElementType;
022import java.lang.annotation.Retention;
023import java.lang.annotation.RetentionPolicy;
024import java.lang.annotation.Target;
025
026import org.springframework.core.annotation.AliasFor;
027
028/**
029 * A convenience annotation that is itself annotated with
030 * {@link ControllerAdvice @ControllerAdvice}
031 * and {@link ResponseBody @ResponseBody}.
032 *
033 * <p>Types that carry this annotation are treated as controller advice where
034 * {@link ExceptionHandler @ExceptionHandler} methods assume
035 * {@link ResponseBody @ResponseBody} semantics by default.
036 *
037 * <p><b>NOTE:</b> {@code @RestControllerAdvice} is processed if an appropriate
038 * {@code HandlerMapping}-{@code HandlerAdapter} pair is configured such as the
039 * {@code RequestMappingHandlerMapping}-{@code RequestMappingHandlerAdapter} pair
040 * which are the default in the MVC Java config and the MVC namespace.
041 *
042 * @author Rossen Stoyanchev
043 * @author Sam Brannen
044 * @since 4.3
045 * @see RestController
046 * @see ControllerAdvice
047 */
048@Target(ElementType.TYPE)
049@Retention(RetentionPolicy.RUNTIME)
050@Documented
051@ControllerAdvice
052@ResponseBody
053public @interface RestControllerAdvice {
054
055        /**
056         * Alias for the {@link #basePackages} attribute.
057         * <p>Allows for more concise annotation declarations &mdash; for example,
058         * {@code @RestControllerAdvice("org.my.pkg")} is equivalent to
059         * {@code @RestControllerAdvice(basePackages = "org.my.pkg")}.
060         * @see #basePackages
061         */
062        @AliasFor(annotation = ControllerAdvice.class)
063        String[] value() default {};
064
065        /**
066         * Array of base packages.
067         * <p>Controllers that belong to those base packages or sub-packages thereof
068         * will be included &mdash; for example,
069         * {@code @RestControllerAdvice(basePackages = "org.my.pkg")} or
070         * {@code @RestControllerAdvice(basePackages = {"org.my.pkg", "org.my.other.pkg"})}.
071         * <p>{@link #value} is an alias for this attribute, simply allowing for
072         * more concise use of the annotation.
073         * <p>Also consider using {@link #basePackageClasses} as a type-safe
074         * alternative to String-based package names.
075         */
076        @AliasFor(annotation = ControllerAdvice.class)
077        String[] basePackages() default {};
078
079        /**
080         * Type-safe alternative to {@link #basePackages} for specifying the packages
081         * in which to select controllers to be advised by the {@code @RestControllerAdvice}
082         * annotated class.
083         * <p>Consider creating a special no-op marker class or interface in each package
084         * that serves no purpose other than being referenced by this attribute.
085         */
086        @AliasFor(annotation = ControllerAdvice.class)
087        Class<?>[] basePackageClasses() default {};
088
089        /**
090         * Array of classes.
091         * <p>Controllers that are assignable to at least one of the given types
092         * will be advised by the {@code @RestControllerAdvice} annotated class.
093         */
094        @AliasFor(annotation = ControllerAdvice.class)
095        Class<?>[] assignableTypes() default {};
096
097        /**
098         * Array of annotations.
099         * <p>Controllers that are annotated with at least one of the supplied annotation
100         * types will be advised by the {@code @RestControllerAdvice} annotated class.
101         * <p>Consider creating a custom composed annotation or use a predefined one,
102         * like {@link RestController @RestController}.
103         */
104        @AliasFor(annotation = ControllerAdvice.class)
105        Class<? extends Annotation>[] annotations() default {};
106
107}