当前位置: 首页>>代码示例>>Java>>正文


Java ControllerAdvice类代码示例

本文整理汇总了Java中org.springframework.web.bind.annotation.ControllerAdvice的典型用法代码示例。如果您正苦于以下问题:Java ControllerAdvice类的具体用法?Java ControllerAdvice怎么用?Java ControllerAdvice使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ControllerAdvice类属于org.springframework.web.bind.annotation包,在下文中一共展示了ControllerAdvice类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ControllerAdviceBean

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
/**
 * 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()));
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:25,代码来源:ControllerAdviceBean.java

示例2: mvc

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
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();
}
 
开发者ID:NetPenguin,项目名称:spring-boot-controller-test-example,代码行数:18,代码来源:TestHelper.java

示例3: init

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
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()));
}
 
开发者ID:damianwajser,项目名称:spring-rest-commons-options,代码行数:13,代码来源:ResourcesBuilder.java

示例4: findAnnotatedBeans

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:15,代码来源:ControllerAdviceBean.java

示例5: initBasePackagesFromBeanType

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
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;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:29,代码来源:ControllerAdviceBean.java

示例6: ControllerAdviceBean

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
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();
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:35,代码来源:ControllerAdviceBean.java

示例7: initBasePackages

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
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;
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:13,代码来源:ControllerAdviceBean.java

示例8: findAnnotatedBeans

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:15,代码来源:ControllerAdviceBean.java

示例9: getValidClasses

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
@Override
protected Set<Class<?>> getValidClasses() {
    Set result = super.getValidClasses();
    result.addAll(apiSource.getValidClasses(RestController.class));
    result.addAll(apiSource.getValidClasses(ControllerAdvice.class));
    return result;
}
 
开发者ID:kongchen,项目名称:swagger-maven-plugin,代码行数:8,代码来源:SpringMavenDocumentSource.java

示例10: generateExceptionMapping

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
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;
}
 
开发者ID:kongchen,项目名称:swagger-maven-plugin,代码行数:37,代码来源:SpringExceptionHandlerReader.java

示例11: ControllerAdviceBuilder

import org.springframework.web.bind.annotation.ControllerAdvice; //导入依赖的package包/类
public ControllerAdviceBuilder() {
	super(ControllerAdvice.class.getName());
}
 
开发者ID:mcraken,项目名称:spring-scaffy,代码行数:4,代码来源:ControllerAdviceBuilder.java


注:本文中的org.springframework.web.bind.annotation.ControllerAdvice类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。