Search in sources :

Example 6 with ExceptionHandlerMethodResolver

use of org.springframework.web.method.annotation.ExceptionHandlerMethodResolver in project spring-mvc-31-demo by rstoyanchev.

the class ExtendedExceptionHandlerExceptionResolver method setExceptionHandler.

/**
 * Provide a handler with @{@link ExceptionHandler} methods.
 */
public void setExceptionHandler(Object handler) {
    this.handler = handler;
    this.methodResolver = new ExceptionHandlerMethodResolver(handler.getClass());
}
Also used : ExceptionHandlerMethodResolver(org.springframework.web.method.annotation.ExceptionHandlerMethodResolver)

Example 7 with ExceptionHandlerMethodResolver

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

the class ControllerMethodResolver method initControllerAdviceCaches.

private void initControllerAdviceCaches(ApplicationContext applicationContext) {
    List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(applicationContext);
    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");
        }
    }
}
Also used : ExceptionHandlerMethodResolver(org.springframework.web.method.annotation.ExceptionHandlerMethodResolver) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean)

Example 8 with ExceptionHandlerMethodResolver

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

the class ControllerMethodResolver method getExceptionHandlerMethod.

/**
 * 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 (Map.Entry<ControllerAdviceBean, ExceptionHandlerMethodResolver> entry : this.exceptionHandlerAdviceCache.entrySet()) {
            ControllerAdviceBean advice = entry.getKey();
            if (advice.isApplicableToBeanType(handlerType)) {
                targetBean = advice.resolveBean();
                targetMethod = entry.getValue().resolveMethodByThrowable(ex);
                if (targetMethod != null) {
                    break;
                }
            }
        }
    }
    if (targetMethod == null) {
        return null;
    }
    InvocableHandlerMethod invocable = new InvocableHandlerMethod(targetBean, targetMethod);
    invocable.setArgumentResolvers(this.exceptionHandlerResolvers);
    return invocable;
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) ExceptionHandlerMethodResolver(org.springframework.web.method.annotation.ExceptionHandlerMethodResolver) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean) Nullable(org.springframework.lang.Nullable)

Aggregations

ExceptionHandlerMethodResolver (org.springframework.web.method.annotation.ExceptionHandlerMethodResolver)8 Method (java.lang.reflect.Method)6 ControllerAdviceBean (org.springframework.web.method.ControllerAdviceBean)6 HandlerMethod (org.springframework.web.method.HandlerMethod)6 InvocableHandlerMethod (org.springframework.web.reactive.result.method.InvocableHandlerMethod)4 SyncInvocableHandlerMethod (org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Nullable (org.springframework.lang.Nullable)2 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Function (java.util.function.Function)1