org.springframework.web.method.annotation.ExceptionHandlerMethodResolver Java Examples
The following examples show how to use
org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.
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: SpringMvcPolyfill.java From Milkomeda with MIT License | 6 votes |
/** * 动态添加异常切面 * @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 #2
Source File: ExceptionHandlerExceptionResolver.java From spring4-understanding with Apache License 2.0 | 6 votes |
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 #3
Source File: AbstractControllerTest.java From entando-core with GNU Lesser General Public License v3.0 | 6 votes |
protected ExceptionHandlerExceptionResolver createExceptionResolver() { final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("rest/messages"); messageSource.setUseCodeAsDefaultMessage(true); ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() { @Override protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) { Method method = new ExceptionHandlerMethodResolver(RestExceptionHandler.class).resolveMethod(exception); RestExceptionHandler validationHandler = new RestExceptionHandler(); validationHandler.setMessageSource(messageSource); return new ServletInvocableHandlerMethod(validationHandler, method); } }; exceptionResolver.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); exceptionResolver.afterPropertiesSet(); return exceptionResolver; }
Example #4
Source File: ControllerMethodResolver.java From spring-analysis-note with MIT License | 5 votes |
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 #5
Source File: ControllerMethodResolver.java From spring-analysis-note with MIT License | 5 votes |
/** * 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 #6
Source File: ExceptionHandlerExceptionResolver.java From spring-analysis-note with MIT License | 5 votes |
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 #7
Source File: ControllerMethodResolver.java From java-technology-stack with MIT License | 5 votes |
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 #8
Source File: ControllerMethodResolver.java From java-technology-stack with MIT License | 5 votes |
/** * 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 #9
Source File: ExceptionHandlerExceptionResolver.java From java-technology-stack with MIT License | 5 votes |
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 #10
Source File: ExceptionHandlerExceptionResolver.java From lams with GNU General Public License v2.0 | 5 votes |
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 #11
Source File: ExceptionHandlerExceptionResolver.java From spring-analysis-note with MIT License | 2 votes |
/** * Return an unmodifiable Map with the {@link ControllerAdvice @ControllerAdvice} * beans discovered in the ApplicationContext. The returned map will be empty if * the method is invoked before the bean has been initialized via * {@link #afterPropertiesSet()}. */ public Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> getExceptionHandlerAdviceCache() { return Collections.unmodifiableMap(this.exceptionHandlerAdviceCache); }
Example #12
Source File: ExceptionHandlerExceptionResolver.java From java-technology-stack with MIT License | 2 votes |
/** * Return an unmodifiable Map with the {@link ControllerAdvice @ControllerAdvice} * beans discovered in the ApplicationContext. The returned map will be empty if * the method is invoked before the bean has been initialized via * {@link #afterPropertiesSet()}. */ public Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> getExceptionHandlerAdviceCache() { return Collections.unmodifiableMap(this.exceptionHandlerAdviceCache); }
Example #13
Source File: ExceptionHandlerExceptionResolver.java From lams with GNU General Public License v2.0 | 2 votes |
/** * Return an unmodifiable Map with the {@link ControllerAdvice @ControllerAdvice} * beans discovered in the ApplicationContext. The returned map will be empty if * the method is invoked before the bean has been initialized via * {@link #afterPropertiesSet()}. */ public Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> getExceptionHandlerAdviceCache() { return Collections.unmodifiableMap(this.exceptionHandlerAdviceCache); }
Example #14
Source File: ExceptionHandlerExceptionResolver.java From spring4-understanding with Apache License 2.0 | 2 votes |
/** * Return an unmodifiable Map with the {@link ControllerAdvice @ControllerAdvice} * beans discovered in the ApplicationContext. The returned map will be empty if * the method is invoked before the bean has been initialized via * {@link #afterPropertiesSet()}. */ public Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> getExceptionHandlerAdviceCache() { return Collections.unmodifiableMap(this.exceptionHandlerAdviceCache); }
Example #15
Source File: ServiceSpringModuleConfig.java From herd with Apache License 2.0 | 2 votes |
/** * Returns a new "exception handler method resolver" that knows how to resolve exception handler methods based on the "herd error information exception * handler". This provides the ability to return an "exception handling" method for a specific exception. * * @return the exception handler method resolver. */ @Bean public ExceptionHandlerMethodResolver exceptionHandlerMethodResolver() { return new ExceptionHandlerMethodResolver(HerdErrorInformationExceptionHandler.class); }