Java 类org.springframework.web.bind.annotation.ControllerAdvice 实例源码

项目:lams    文件:ControllerAdviceBean.java   
/**
 * Create an instance using the given bean name.
 * @param beanName the name of the bean
 * @param beanFactory a BeanFactory that can be used later to resolve the bean
 */
public ControllerAdviceBean(String beanName, BeanFactory beanFactory) {
    Assert.hasText(beanName, "Bean name must not be null");
    Assert.notNull(beanFactory, "BeanFactory must not be null");
    Assert.isTrue(beanFactory.containsBean(beanName),
            "BeanFactory [" + beanFactory + "] does not contain bean with name '" + beanName + "'");

    this.bean = beanName;
    this.beanFactory = beanFactory;

    Class<?> beanType = this.beanFactory.getType(beanName);
    this.order = initOrderFromBeanType(beanType);

    ControllerAdvice annotation = AnnotationUtils.findAnnotation(beanType,ControllerAdvice.class);
    Assert.notNull(annotation, "BeanType [" + beanType.getName() + "] is not annotated @ControllerAdvice");

    this.basePackages.addAll(initBasePackagesFromBeanType(beanType, annotation));
    this.annotations.addAll(Arrays.asList(annotation.annotations()));
    this.assignableTypes.addAll(Arrays.asList(annotation.assignableTypes()));
}
项目:spring-boot-controller-test-example    文件:TestHelper.java   
public MockMvc mvc(Object controller) {
    StandaloneMockMvcBuilder builder = new StandaloneMockMvcBuilder(controller) {
        @Override
        protected WebApplicationContext initWebAppContext() {
            WebApplicationContext context = super.initWebAppContext();
            StaticListableBeanFactory beanFactory = (StaticListableBeanFactory)context.getAutowireCapableBeanFactory();

            Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class))
                .filter(name -> applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null)
                .forEach(name -> beanFactory.addBean(name, applicationContext.getBean(name)));

            context.getBean(RequestMappingHandlerAdapter.class).afterPropertiesSet();
            return context;
        }
    };
    return builder.setHandlerExceptionResolvers(handlerExceptionResolver).build();
}
项目:spring-rest-commons-options    文件:ResourcesBuilder.java   
private void init(ApplicationContext context) {
    init();
    LOGGER.debug("Get All ExceptionHandlers");
    List<Object> exceptionsHandlers = ReflectionUtils
            .proxyToObject(context.getBeansWithAnnotation(ControllerAdvice.class).values());
    LOGGER.debug("Get All RestController");
    exceptionsHandlers.forEach(this::buildHttpCodes);
    controllers
            .addAll(ReflectionUtils.proxyToObject(context.getBeansWithAnnotation(RestController.class).values()));
    LOGGER.debug("Get All Controller");
    controllers.addAll(ReflectionUtils.proxyToObject(context.getBeansWithAnnotation(Controller.class).values()));
}
项目:lams    文件:ControllerAdviceBean.java   
/**
 * Create an instance using the given bean instance.
 * @param bean the bean
 */
public ControllerAdviceBean(Object bean) {
    Assert.notNull(bean, "Bean must not be null");
    this.bean = bean;
    this.order = initOrderFromBean(bean);

    Class<?> beanType = bean.getClass();
    ControllerAdvice annotation = AnnotationUtils.findAnnotation(beanType,ControllerAdvice.class);
    Assert.notNull(annotation, "Bean type [" + beanType.getName() + "] is not annotated @ControllerAdvice");

    this.basePackages.addAll(initBasePackagesFromBeanType(beanType, annotation));
    this.annotations.addAll(Arrays.asList(annotation.annotations()));
    this.assignableTypes.addAll(Arrays.asList(annotation.assignableTypes()));
    this.beanFactory = null;
}
项目:lams    文件:ControllerAdviceBean.java   
/**
 * Find the names of beans annotated with
 * {@linkplain ControllerAdvice @ControllerAdvice} in the given
 * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
 */
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext applicationContext) {
    List<ControllerAdviceBean> beans = new ArrayList<ControllerAdviceBean>();
    for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class)) {
        if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) {
            beans.add(new ControllerAdviceBean(name, applicationContext));
        }
    }
    return beans;
}
项目:lams    文件:ControllerAdviceBean.java   
private static List<Package> initBasePackagesFromBeanType(Class<?> beanType, ControllerAdvice annotation) {
    List<Package> basePackages = new ArrayList<Package>();
    List<String> basePackageNames = new ArrayList<String>();
    basePackageNames.addAll(Arrays.asList(annotation.value()));
    basePackageNames.addAll(Arrays.asList(annotation.basePackages()));
    for (String pkgName : basePackageNames) {
        if (StringUtils.hasText(pkgName)) {
            Package pkg = Package.getPackage(pkgName);
            if(pkg != null) {
                basePackages.add(pkg);
            }
            else {
                logger.warn("Package [" + pkgName + "] was not found, see [" + beanType.getName() + "]");
            }
        }
    }
    for (Class<?> markerClass : annotation.basePackageClasses()) {
        Package pack = markerClass.getPackage();
        if (pack != null) {
            basePackages.add(pack);
        }
        else {
            logger.warn("Package was not found for class [" + markerClass.getName() +
                    "], see [" + beanType.getName() + "]");
        }
    }
    return basePackages;
}
项目:spring4-understanding    文件:ControllerAdviceBean.java   
private ControllerAdviceBean(Object bean, BeanFactory beanFactory) {
    this.bean = bean;
    this.beanFactory = beanFactory;
    Class<?> beanType;

    if (bean instanceof String) {
        String beanName = (String) bean;
        Assert.hasText(beanName, "Bean name must not be null");
        Assert.notNull(beanFactory, "BeanFactory must not be null");
        if (!beanFactory.containsBean(beanName)) {
            throw new IllegalArgumentException("BeanFactory [" + beanFactory +
                    "] does not contain specified controller advice bean '" + beanName + "'");
        }
        beanType = this.beanFactory.getType(beanName);
        this.order = initOrderFromBeanType(beanType);
    }
    else {
        Assert.notNull(bean, "Bean must not be null");
        beanType = bean.getClass();
        this.order = initOrderFromBean(bean);
    }

    ControllerAdvice annotation = AnnotationUtils.findAnnotation(beanType, ControllerAdvice.class);
    if (annotation != null) {
        this.basePackages = initBasePackages(annotation);
        this.assignableTypes = Arrays.asList(annotation.assignableTypes());
        this.annotations = Arrays.asList(annotation.annotations());
    }
    else {
        this.basePackages = Collections.emptySet();
        this.assignableTypes = Collections.emptyList();
        this.annotations = Collections.emptyList();
    }
}
项目:spring4-understanding    文件:ControllerAdviceBean.java   
/**
 * Find the names of beans annotated with
 * {@linkplain ControllerAdvice @ControllerAdvice} in the given
 * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
 */
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext applicationContext) {
    List<ControllerAdviceBean> beans = new ArrayList<ControllerAdviceBean>();
    for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class)) {
        if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) {
            beans.add(new ControllerAdviceBean(name, applicationContext));
        }
    }
    return beans;
}
项目:spring4-understanding    文件:ControllerAdviceBean.java   
private static Set<String> initBasePackages(ControllerAdvice annotation) {
    Set<String> basePackages = new LinkedHashSet<String>();
    for (String basePackage : annotation.basePackages()) {
        if (StringUtils.hasText(basePackage)) {
            basePackages.add(adaptBasePackage(basePackage));
        }
    }
    for (Class<?> markerClass : annotation.basePackageClasses()) {
        basePackages.add(adaptBasePackage(ClassUtils.getPackageName(markerClass)));
    }
    return basePackages;
}
项目:class-guard    文件:ControllerAdviceBean.java   
/**
 * Find the names of beans annotated with
 * {@linkplain ControllerAdvice @ControllerAdvice} in the given
 * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
 */
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext applicationContext) {
    List<ControllerAdviceBean> beans = new ArrayList<ControllerAdviceBean>();
    for (String name : applicationContext.getBeanDefinitionNames()) {
        if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) {
            beans.add(new ControllerAdviceBean(name, applicationContext));
        }
    }
    return beans;
}
项目:swagger-maven-plugin    文件:SpringMavenDocumentSource.java   
@Override
protected Set<Class<?>> getValidClasses() {
    Set result = super.getValidClasses();
    result.addAll(apiSource.getValidClasses(RestController.class));
    result.addAll(apiSource.getValidClasses(ControllerAdvice.class));
    return result;
}
项目:swagger-maven-plugin    文件:SpringExceptionHandlerReader.java   
protected Map<Class<? extends Throwable>, ResponseStatus> generateExceptionMapping(Set<Class<?>> classes) {
    Map<Class<? extends Throwable>, ResponseStatus> result =
            new HashMap<Class<? extends Throwable>, ResponseStatus>();

    log.debug(String.format("Looking for classes with @ControllerAdvice annotation"));
    for (Class clazz: classes) {
        ControllerAdvice advice = findAnnotation(clazz, ControllerAdvice.class);
        if (advice == null) {
            continue;
        }

        log.debug(String.format("%s is annotated as @ControllerAdvice", clazz.getName()));

        for (Method method: clazz.getMethods()) {
            ExceptionHandler handler = findAnnotation(method, ExceptionHandler.class);
            if (handler == null) {
                log.debug(String.format("@ExceptionHandler is missing on %s method, skipping", method));
                continue;
            }

            ResponseStatus responseStatus = findAnnotation(method, ResponseStatus.class);
            if (responseStatus == null) {
                log.debug(String.format("@ResponseStatus is missing on %s method, skipping", method));
                continue;
            }

            Class[] exceptionClasses = handler.value();
            for (Class exceptionClass: exceptionClasses) {
                log.debug(String.format("%s will be mapped to %s", exceptionClass, responseStatus));
                result.put(exceptionClass, responseStatus);
            }
        }

    }
    return result;
}
项目:spring-scaffy    文件:ControllerAdviceBuilder.java   
public ControllerAdviceBuilder() {
    super(ControllerAdvice.class.getName());
}