Search in sources :

Example 6 with ControllerAdviceBean

use of org.springframework.web.method.ControllerAdviceBean in project spring-framework by spring-projects.

the class RequestResponseBodyAdviceChain method getMatchingAdvice.

@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;
}
Also used : ArrayList(java.util.ArrayList) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean)

Example 7 with ControllerAdviceBean

use of org.springframework.web.method.ControllerAdviceBean in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method getDataBinderFactory.

private WebDataBinderFactory getDataBinderFactory(HandlerMethod handlerMethod) throws Exception {
    Class<?> handlerType = handlerMethod.getBeanType();
    Set<Method> methods = this.initBinderCache.get(handlerType);
    if (methods == null) {
        methods = MethodIntrospector.selectMethods(handlerType, INIT_BINDER_METHODS);
        this.initBinderCache.put(handlerType, methods);
    }
    List<InvocableHandlerMethod> initBinderMethods = new ArrayList<>();
    // Global methods first
    for (Entry<ControllerAdviceBean, Set<Method>> entry : this.initBinderAdviceCache.entrySet()) {
        if (entry.getKey().isApplicableToBeanType(handlerType)) {
            Object bean = entry.getKey().resolveBean();
            for (Method method : entry.getValue()) {
                initBinderMethods.add(createInitBinderMethod(bean, method));
            }
        }
    }
    for (Method method : methods) {
        Object bean = handlerMethod.getBean();
        initBinderMethods.add(createInitBinderMethod(bean, method));
    }
    return createDataBinderFactory(initBinderMethods);
}
Also used : Set(java.util.Set) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) ArrayList(java.util.ArrayList) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean)

Aggregations

ControllerAdviceBean (org.springframework.web.method.ControllerAdviceBean)7 Method (java.lang.reflect.Method)5 HandlerMethod (org.springframework.web.method.HandlerMethod)5 ArrayList (java.util.ArrayList)4 ExceptionHandlerMethodResolver (org.springframework.web.method.annotation.ExceptionHandlerMethodResolver)3 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)3 Set (java.util.Set)2 ModelFactory (org.springframework.web.method.annotation.ModelFactory)1 SessionAttributesHandler (org.springframework.web.method.annotation.SessionAttributesHandler)1 InvocableHandlerMethod (org.springframework.web.reactive.result.method.InvocableHandlerMethod)1 SyncInvocableHandlerMethod (org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)1