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.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 * In particular {@code @RestControllerAdvice} is not supported with the 042 * {@code DefaultAnnotationHandlerMapping}-{@code AnnotationMethodHandlerAdapter} 043 * pair both of which are also deprecated. 044 * 045 * @author Rossen Stoyanchev 046 * @since 4.3 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 e.g.: 058 * {@code @ControllerAdvice("org.my.pkg")} is equivalent to 059 * {@code @ControllerAdvice(basePackages="org.my.pkg")}. 060 * @see #basePackages() 061 */ 062 @AliasFor("basePackages") 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, e.g.: {@code @ControllerAdvice(basePackages="org.my.pkg")} 069 * or {@code @ControllerAdvice(basePackages={"org.my.pkg", "org.my.other.pkg"})}. 070 * <p>{@link #value} is an alias for this attribute, simply allowing for 071 * more concise use of the annotation. 072 * <p>Also consider using {@link #basePackageClasses()} as a type-safe 073 * alternative to String-based package names. 074 */ 075 @AliasFor("value") 076 String[] basePackages() default {}; 077 078 /** 079 * Type-safe alternative to {@link #value()} for specifying the packages 080 * to select Controllers to be assisted by the {@code @ControllerAdvice} 081 * annotated class. 082 * <p>Consider creating a special no-op marker class or interface in each package 083 * that serves no purpose other than being referenced by this attribute. 084 */ 085 Class<?>[] basePackageClasses() default {}; 086 087 /** 088 * Array of classes. 089 * <p>Controllers that are assignable to at least one of the given types 090 * will be assisted by the {@code @ControllerAdvice} annotated class. 091 */ 092 Class<?>[] assignableTypes() default {}; 093 094 /** 095 * Array of annotations. 096 * <p>Controllers that are annotated with this/one of those annotation(s) 097 * will be assisted by the {@code @ControllerAdvice} annotated class. 098 * <p>Consider creating a special annotation or use a predefined one, 099 * like {@link RestController @RestController}. 100 */ 101 Class<? extends Annotation>[] annotations() default {}; 102 103}