org.springframework.web.method.ControllerAdviceBean Java Examples

The following examples show how to use org.springframework.web.method.ControllerAdviceBean. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestResponseBodyAdviceChain.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <A> List<A> getMatchingAdvice(MethodParameter parameter, Class<? extends A> adviceType) {
	List<Object> availableAdvice = getAdvice(adviceType);
	if (CollectionUtils.isEmpty(availableAdvice)) {
		return Collections.emptyList();
	}
	List<A> result = new ArrayList<>(availableAdvice.size());
	for (Object advice : availableAdvice) {
		if (advice instanceof ControllerAdviceBean) {
			ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
			if (!adviceBean.isApplicableToBeanType(parameter.getContainingClass())) {
				continue;
			}
			advice = adviceBean.resolveBean();
		}
		if (adviceType.isAssignableFrom(advice.getClass())) {
			result.add((A) advice);
		}
	}
	return result;
}
 
Example #2
Source File: RequestResponseBodyAdviceChain.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <A> List<A> getMatchingAdvice(MethodParameter parameter, Class<? extends A> adviceType) {
	List<Object> availableAdvice = getAdvice(adviceType);
	if (CollectionUtils.isEmpty(availableAdvice)) {
		return Collections.emptyList();
	}
	List<A> result = new ArrayList<>(availableAdvice.size());
	for (Object advice : availableAdvice) {
		if (advice instanceof ControllerAdviceBean) {
			ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
			if (!adviceBean.isApplicableToBeanType(parameter.getContainingClass())) {
				continue;
			}
			advice = adviceBean.resolveBean();
		}
		if (adviceType.isAssignableFrom(advice.getClass())) {
			result.add((A) advice);
		}
	}
	return result;
}
 
Example #3
Source File: ExceptionHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void initExceptionHandlerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for exception mappings: " + getApplicationContext());
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(adviceBean.getBeanType());
		if (resolver.hasExceptionMappings()) {
			this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
			logger.info("Detected @ExceptionHandler methods in " + adviceBean);
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(adviceBean.getBeanType())) {
			this.responseBodyAdvice.add(adviceBean);
			logger.info("Detected ResponseBodyAdvice implementation in " + adviceBean);
		}
	}
}
 
Example #4
Source File: RequestResponseBodyAdviceChain.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <A> List<A> getMatchingAdvice(MethodParameter parameter, Class<? extends A> adviceType) {
	List<Object> availableAdvice = getAdvice(adviceType);
	if (CollectionUtils.isEmpty(availableAdvice)) {
		return Collections.emptyList();
	}
	List<A> result = new ArrayList<A>(availableAdvice.size());
	for (Object advice : availableAdvice) {
		if (advice instanceof ControllerAdviceBean) {
			ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
			if (!adviceBean.isApplicableToBeanType(parameter.getContainingClass())) {
				continue;
			}
			advice = adviceBean.resolveBean();
		}
		if (adviceType.isAssignableFrom(advice.getClass())) {
			result.add((A) advice);
		}
	}
	return result;
}
 
Example #5
Source File: SpringMvcPolyfill.java    From Milkomeda with MIT License 6 votes vote down vote up
/**
 * 动态添加异常切面
 * @param handlerExceptionResolver  HandlerExceptionResolver
 * @param beanFactory               BeanFactory
 * @param beanName                  异常拦截处理器bean名
 */
public static void addDynamicExceptionAdvice(HandlerExceptionResolver handlerExceptionResolver, BeanFactory beanFactory, String beanName) {
    if (handlerExceptionResolver instanceof HandlerExceptionResolverComposite) {
        List<HandlerExceptionResolver> exceptionResolvers = ((HandlerExceptionResolverComposite) handlerExceptionResolver).getExceptionResolvers();
        for (HandlerExceptionResolver exceptionResolver : exceptionResolvers) {
            if (exceptionResolver instanceof ExceptionHandlerExceptionResolver) {
                // TODO <mark> 由于使用底层API, 这个exceptionHandlerAdviceCache属性在未来版本中可能会改
                Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> resolverMap = ReflectUtil.invokeFieldPath(exceptionResolver, "exceptionHandlerAdviceCache");
                if (resolverMap == null) {
                    resolverMap = new HashMap<>(2);
                }
                // 仿Spring MVC源码创建advice
                ControllerAdviceBean adviceBean = new ControllerAdviceBean(beanName, ApplicationContextHolder.get(), null);
                Class<?> beanType = adviceBean.getBeanType();
                if (beanType == null) {
                    throw new IllegalStateException("SpringMvcPolyfill find unresolvable type for ControllerAdviceBean: " + adviceBean);
                }
                ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
                if (resolver.hasExceptionMappings()) {
                    resolverMap.put(adviceBean, resolver);
                }
                break;
            }
        }
    }
}
 
Example #6
Source File: RequestResponseBodyAdviceChain.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <A> List<A> getMatchingAdvice(MethodParameter parameter, Class<? extends A> adviceType) {
	List<Object> availableAdvice = getAdvice(adviceType);
	if (CollectionUtils.isEmpty(availableAdvice)) {
		return Collections.emptyList();
	}
	List<A> result = new ArrayList<A>(availableAdvice.size());
	for (Object advice : availableAdvice) {
		if (advice instanceof ControllerAdviceBean) {
			ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
			if (!adviceBean.isApplicableToBeanType(parameter.getContainingClass())) {
				continue;
			}
			advice = adviceBean.resolveBean();
		}
		if (adviceType.isAssignableFrom(advice.getClass())) {
			result.add((A) advice);
		}
	}
	return result;
}
 
Example #7
Source File: ExceptionHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void initExceptionHandlerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for exception mappings: " + getApplicationContext());
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(adviceBean.getBeanType());
		if (resolver.hasExceptionMappings()) {
			this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
			if (logger.isInfoEnabled()) {
				logger.info("Detected @ExceptionHandler methods in " + adviceBean);
			}
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(adviceBean.getBeanType())) {
			this.responseBodyAdvice.add(adviceBean);
			if (logger.isInfoEnabled()) {
				logger.info("Detected ResponseBodyAdvice implementation in " + adviceBean);
			}
		}
	}
}
 
Example #8
Source File: RequestResponseBodyAdviceChainTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void controllerAdvice() {
	Object adviceBean = new ControllerAdviceBean(new MyControllerAdvice());
	RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Collections.singletonList(adviceBean));

	String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType,
			this.converterType, this.request, this.response);

	assertEquals("body-MyControllerAdvice", actual);
}
 
Example #9
Source File: RequestResponseBodyAdviceChainTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void controllerAdviceNotApplicable() {
	Object adviceBean = new ControllerAdviceBean(new TargetedControllerAdvice());
	RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Collections.singletonList(adviceBean));

	String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType,
			this.converterType, this.request, this.response);

	assertEquals(this.body, actual);
}
 
Example #10
Source File: WebSocketAnnotationMethodMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void initControllerAdviceCache() {
	ApplicationContext context = getApplicationContext();
	if (context == null) {
		return;
	}
	if (logger.isTraceEnabled()) {
		logger.trace("Looking for @MessageExceptionHandler mappings: " + context);
	}
	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(context);
	AnnotationAwareOrderComparator.sort(beans);
	initMessagingAdviceCache(MessagingControllerAdviceBean.createFromList(beans));
}
 
Example #11
Source File: WebSocketAnnotationMethodMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
public static List<MessagingAdviceBean> createFromList(List<ControllerAdviceBean> beans) {
	List<MessagingAdviceBean> result = new ArrayList<>(beans.size());
	for (ControllerAdviceBean bean : beans) {
		result.add(new MessagingControllerAdviceBean(bean));
	}
	return result;
}
 
Example #12
Source File: RequestResponseBodyAdviceChain.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void initAdvice(List<Object> requestResponseBodyAdvice) {
	if (requestResponseBodyAdvice == null) {
		return;
	}
	for (Object advice : requestResponseBodyAdvice) {
		Class<?> beanType = (advice instanceof ControllerAdviceBean ?
				((ControllerAdviceBean) advice).getBeanType() : advice.getClass());
		if (RequestBodyAdvice.class.isAssignableFrom(beanType)) {
			this.requestBodyAdvice.add(advice);
		}
		else if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
			this.responseBodyAdvice.add(advice);
		}
	}
}
 
Example #13
Source File: ControllerMethodResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Find an {@code @ExceptionHandler} method in {@code @ControllerAdvice}
 * components or in the controller of the given {@code @RequestMapping} method.
 */
@Nullable
public InvocableHandlerMethod getExceptionHandlerMethod(Throwable ex, HandlerMethod handlerMethod) {
	Class<?> handlerType = handlerMethod.getBeanType();

	// Controller-local first...
	Object targetBean = handlerMethod.getBean();
	Method targetMethod = this.exceptionHandlerCache
			.computeIfAbsent(handlerType, ExceptionHandlerMethodResolver::new)
			.resolveMethodByThrowable(ex);

	if (targetMethod == null) {
		// Global exception handlers...
		for (ControllerAdviceBean advice : this.exceptionHandlerAdviceCache.keySet()) {
			if (advice.isApplicableToBeanType(handlerType)) {
				targetBean = advice.resolveBean();
				targetMethod = this.exceptionHandlerAdviceCache.get(advice).resolveMethodByThrowable(ex);
				if (targetMethod != null) {
					break;
				}
			}
		}
	}

	if (targetMethod == null) {
		return null;
	}

	InvocableHandlerMethod invocable = new InvocableHandlerMethod(targetBean, targetMethod);
	invocable.setArgumentResolvers(this.exceptionHandlerResolvers);
	return invocable;
}
 
Example #14
Source File: RequestResponseBodyAdviceChain.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void initAdvice(List<Object> requestResponseBodyAdvice) {
	if (requestResponseBodyAdvice == null) {
		return;
	}
	for (Object advice : requestResponseBodyAdvice) {
		Class<?> beanType = (advice instanceof ControllerAdviceBean ?
				((ControllerAdviceBean) advice).getBeanType() : advice.getClass());
		if (RequestBodyAdvice.class.isAssignableFrom(beanType)) {
			this.requestBodyAdvice.add(advice);
		}
		else if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
			this.responseBodyAdvice.add(advice);
		}
	}
}
 
Example #15
Source File: RequestResponseBodyAdviceChainTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void controllerAdvice() {

	Object adviceBean = new ControllerAdviceBean(new MyControllerAdvice());
	RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Arrays.asList(adviceBean));

	String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType,
			this.converterType, this.request, this.response);

	assertEquals("body-MyControllerAdvice", actual);
}
 
Example #16
Source File: RequestResponseBodyAdviceChainTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void controllerAdviceNotApplicable() {

	Object adviceBean = new ControllerAdviceBean(new TargetedControllerAdvice());
	RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Arrays.asList(adviceBean));

	String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType,
			this.converterType, this.request, this.response);

	assertEquals(this.body, actual);
}
 
Example #17
Source File: WebSocketAnnotationMethodMessageHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void initControllerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for @MessageExceptionHandler mappings: " + getApplicationContext());
	}
	List<ControllerAdviceBean> controllerAdvice = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(controllerAdvice);
	initMessagingAdviceCache(MessagingControllerAdviceBean.createFromList(controllerAdvice));
}
 
Example #18
Source File: WebSocketAnnotationMethodMessageHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public static List<MessagingAdviceBean> createFromList(List<ControllerAdviceBean> controllerAdvice) {
	List<MessagingAdviceBean> messagingAdvice = new ArrayList<MessagingAdviceBean>(controllerAdvice.size());
	for (ControllerAdviceBean bean : controllerAdvice) {
		messagingAdvice.add(new MessagingControllerAdviceBean(bean));
	}
	return messagingAdvice;
}
 
Example #19
Source File: WebSocketAnnotationMethodMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void initControllerAdviceCache() {
	ApplicationContext context = getApplicationContext();
	if (context == null) {
		return;
	}
	if (logger.isTraceEnabled()) {
		logger.trace("Looking for @MessageExceptionHandler mappings: " + context);
	}
	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(context);
	AnnotationAwareOrderComparator.sort(beans);
	initMessagingAdviceCache(MessagingControllerAdviceBean.createFromList(beans));
}
 
Example #20
Source File: ExceptionHandlerExceptionResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void initExceptionHandlerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		Class<?> beanType = adviceBean.getBeanType();
		if (beanType == null) {
			throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean);
		}
		ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
		if (resolver.hasExceptionMappings()) {
			this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
			this.responseBodyAdvice.add(adviceBean);
		}
	}

	if (logger.isDebugEnabled()) {
		int handlerSize = this.exceptionHandlerAdviceCache.size();
		int adviceSize = this.responseBodyAdvice.size();
		if (handlerSize == 0 && adviceSize == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " +
					handlerSize + " @ExceptionHandler, " + adviceSize + " ResponseBodyAdvice");
		}
	}
}
 
Example #21
Source File: RequestResponseBodyAdviceChain.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
static <T> List<T> getAdviceByType(@Nullable List<Object> requestResponseBodyAdvice, Class<T> adviceType) {
	if (requestResponseBodyAdvice != null) {
		List<T> result = new ArrayList<>();
		for (Object advice : requestResponseBodyAdvice) {
			Class<?> beanType = (advice instanceof ControllerAdviceBean ?
					((ControllerAdviceBean) advice).getBeanType() : advice.getClass());
			if (beanType != null && adviceType.isAssignableFrom(beanType)) {
				result.add((T) advice);
			}
		}
		return result;
	}
	return Collections.emptyList();
}
 
Example #22
Source File: RequestResponseBodyAdviceChainTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void controllerAdvice() {
	Object adviceBean = new ControllerAdviceBean(new MyControllerAdvice());
	RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Collections.singletonList(adviceBean));

	String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType,
			this.converterType, this.request, this.response);

	assertEquals("body-MyControllerAdvice", actual);
}
 
Example #23
Source File: RequestResponseBodyAdviceChainTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void controllerAdviceNotApplicable() {
	Object adviceBean = new ControllerAdviceBean(new TargetedControllerAdvice());
	RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(Collections.singletonList(adviceBean));

	String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType,
			this.converterType, this.request, this.response);

	assertEquals(this.body, actual);
}
 
Example #24
Source File: ExceptionHandlerExceptionResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void initExceptionHandlerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		Class<?> beanType = adviceBean.getBeanType();
		if (beanType == null) {
			throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean);
		}
		ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
		if (resolver.hasExceptionMappings()) {
			this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
			this.responseBodyAdvice.add(adviceBean);
		}
	}

	if (logger.isDebugEnabled()) {
		int handlerSize = this.exceptionHandlerAdviceCache.size();
		int adviceSize = this.responseBodyAdvice.size();
		if (handlerSize == 0 && adviceSize == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " +
					handlerSize + " @ExceptionHandler, " + adviceSize + " ResponseBodyAdvice");
		}
	}
}
 
Example #25
Source File: WebSocketAnnotationMethodMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public static List<MessagingAdviceBean> createFromList(List<ControllerAdviceBean> beans) {
	List<MessagingAdviceBean> result = new ArrayList<>(beans.size());
	for (ControllerAdviceBean bean : beans) {
		result.add(new MessagingControllerAdviceBean(bean));
	}
	return result;
}
 
Example #26
Source File: GenericResponseBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Gets generic map response.
 *
 * @param beanType the bean type
 * @return the generic map response
 */
private Map<String, ApiResponse> getGenericMapResponse(Class<?> beanType) {
	return controllerAdviceInfos.stream()
			.filter(controllerAdviceInfo -> new ControllerAdviceBean(controllerAdviceInfo.getControllerAdvice()).isApplicableToBeanType(beanType))
			.map(ControllerAdviceInfo::getApiResponseMap)
			.collect(LinkedHashMap::new, Map::putAll, Map::putAll);
}
 
Example #27
Source File: ControllerMethodResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void initControllerAdviceCaches(ApplicationContext applicationContext) {
	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(applicationContext);
	AnnotationAwareOrderComparator.sort(beans);

	for (ControllerAdviceBean bean : beans) {
		Class<?> beanType = bean.getBeanType();
		if (beanType != null) {
			Set<Method> attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS);
			if (!attrMethods.isEmpty()) {
				this.modelAttributeAdviceCache.put(bean, attrMethods);
			}
			Set<Method> binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS);
			if (!binderMethods.isEmpty()) {
				this.initBinderAdviceCache.put(bean, binderMethods);
			}
			ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
			if (resolver.hasExceptionMappings()) {
				this.exceptionHandlerAdviceCache.put(bean, resolver);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		int modelSize = this.modelAttributeAdviceCache.size();
		int binderSize = this.initBinderAdviceCache.size();
		int handlerSize = this.exceptionHandlerAdviceCache.size();
		if (modelSize == 0 && binderSize == 0 && handlerSize == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " + modelSize + " @ModelAttribute, " + binderSize +
					" @InitBinder, " + handlerSize + " @ExceptionHandler");
		}
	}
}
 
Example #28
Source File: ControllerMethodResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Find an {@code @ExceptionHandler} method in {@code @ControllerAdvice}
 * components or in the controller of the given {@code @RequestMapping} method.
 */
@Nullable
public InvocableHandlerMethod getExceptionHandlerMethod(Throwable ex, HandlerMethod handlerMethod) {

	Class<?> handlerType = handlerMethod.getBeanType();

	// Controller-local first...
	Object targetBean = handlerMethod.getBean();
	Method targetMethod = this.exceptionHandlerCache
			.computeIfAbsent(handlerType, ExceptionHandlerMethodResolver::new)
			.resolveMethodByThrowable(ex);

	if (targetMethod == null) {
		// Global exception handlers...
		for (ControllerAdviceBean advice : this.exceptionHandlerAdviceCache.keySet()) {
			if (advice.isApplicableToBeanType(handlerType)) {
				targetBean = advice.resolveBean();
				targetMethod = this.exceptionHandlerAdviceCache.get(advice).resolveMethodByThrowable(ex);
				if (targetMethod != null) {
					break;
				}
			}
		}
	}

	if (targetMethod == null) {
		return null;
	}

	InvocableHandlerMethod invocable = new InvocableHandlerMethod(targetBean, targetMethod);
	invocable.setArgumentResolvers(this.exceptionHandlerResolvers);
	return invocable;
}
 
Example #29
Source File: RequestResponseBodyAdviceChain.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
static <T> List<T> getAdviceByType(@Nullable List<Object> requestResponseBodyAdvice, Class<T> adviceType) {
	if (requestResponseBodyAdvice != null) {
		List<T> result = new ArrayList<>();
		for (Object advice : requestResponseBodyAdvice) {
			Class<?> beanType = (advice instanceof ControllerAdviceBean ?
					((ControllerAdviceBean) advice).getBeanType() : advice.getClass());
			if (beanType != null && adviceType.isAssignableFrom(beanType)) {
				result.add((T) advice);
			}
		}
		return result;
	}
	return Collections.emptyList();
}
 
Example #30
Source File: ControllerMethodResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void initControllerAdviceCaches(ApplicationContext applicationContext) {
	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(applicationContext);
	AnnotationAwareOrderComparator.sort(beans);

	for (ControllerAdviceBean bean : beans) {
		Class<?> beanType = bean.getBeanType();
		if (beanType != null) {
			Set<Method> attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS);
			if (!attrMethods.isEmpty()) {
				this.modelAttributeAdviceCache.put(bean, attrMethods);
			}
			Set<Method> binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS);
			if (!binderMethods.isEmpty()) {
				this.initBinderAdviceCache.put(bean, binderMethods);
			}
			ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
			if (resolver.hasExceptionMappings()) {
				this.exceptionHandlerAdviceCache.put(bean, resolver);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		int modelSize = this.modelAttributeAdviceCache.size();
		int binderSize = this.initBinderAdviceCache.size();
		int handlerSize = this.exceptionHandlerAdviceCache.size();
		if (modelSize == 0 && binderSize == 0 && handlerSize == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " + modelSize + " @ModelAttribute, " + binderSize +
					" @InitBinder, " + handlerSize + " @ExceptionHandler");
		}
	}
}