org.springframework.web.bind.annotation.ControllerAdvice Java Examples
The following examples show how to use
org.springframework.web.bind.annotation.ControllerAdvice.
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: ControllerAdviceBean.java From spring-analysis-note with MIT License | 5 votes |
private ControllerAdviceBean(Object bean, @Nullable 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 = (beanType != null ? AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class) : null); if (annotation != null) { this.beanTypePredicate = HandlerTypePredicate.builder() .basePackage(annotation.basePackages()) .basePackageClass(annotation.basePackageClasses()) .assignableType(annotation.assignableTypes()) .annotation(annotation.annotations()) .build(); } else { this.beanTypePredicate = HandlerTypePredicate.forAnyHandlerType(); } }
Example #2
Source File: ControllerAdviceBean.java From spring-analysis-note with MIT License | 5 votes |
/** * Find beans annotated with {@link ControllerAdvice @ControllerAdvice} in the * given {@link ApplicationContext} and wrap them as {@code ControllerAdviceBean} * instances. */ public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) { return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class)) .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null) .map(name -> new ControllerAdviceBean(name, context)) .collect(Collectors.toList()); }
Example #3
Source File: OpenAPIBuilder.java From springdoc-openapi with Apache License 2.0 | 5 votes |
/** * Gets controller advice map. * * @return the controller advice map */ public Map<String, Object> getControllerAdviceMap() { Map<String, Object> controllerAdviceMap = context.getBeansWithAnnotation(ControllerAdvice.class); return Stream.of(controllerAdviceMap).flatMap(mapEl -> mapEl.entrySet().stream()).filter( controller -> (AnnotationUtils.findAnnotation(controller.getValue().getClass(), Hidden.class) == null)) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1)); }
Example #4
Source File: ControllerAdviceBean.java From java-technology-stack with MIT License | 5 votes |
private ControllerAdviceBean(Object bean, @Nullable 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 = (beanType != null ? AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class) : null); if (annotation != null) { this.beanTypePredicate = HandlerTypePredicate.builder() .basePackage(annotation.basePackages()) .basePackageClass(annotation.basePackageClasses()) .assignableType(annotation.assignableTypes()) .annotation(annotation.annotations()) .build(); } else { this.beanTypePredicate = HandlerTypePredicate.forAnyHandlerType(); } }
Example #5
Source File: ControllerAdviceBean.java From java-technology-stack with MIT License | 5 votes |
/** * 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 context) { return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class)) .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null) .map(name -> new ControllerAdviceBean(name, context)) .collect(Collectors.toList()); }
Example #6
Source File: ControllerAdviceBean.java From lams with GNU General Public License v2.0 | 5 votes |
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 = AnnotatedElementUtils.findMergedAnnotation(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(); } }
Example #7
Source File: ControllerAdviceBean.java From lams with GNU General Public License v2.0 | 5 votes |
/** * 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; }
Example #8
Source File: ControllerAdviceBean.java From lams with GNU General Public License v2.0 | 5 votes |
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; }
Example #9
Source File: ControllerAdviceBean.java From spring4-understanding with Apache License 2.0 | 5 votes |
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(); } }
Example #10
Source File: ControllerAdviceBean.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * 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; }
Example #11
Source File: ControllerAdviceBean.java From spring4-understanding with Apache License 2.0 | 5 votes |
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; }
Example #12
Source File: SpringMavenDocumentSource.java From swagger-maven-plugin with Apache License 2.0 | 5 votes |
@Override protected Set<Class<?>> getValidClasses() { Set<Class<?>> result = super.getValidClasses(); result.addAll(apiSource.getValidClasses(RestController.class)); result.addAll(apiSource.getValidClasses(ControllerAdvice.class)); return result; }
Example #13
Source File: SpringExceptionHandlerReader.java From swagger-maven-plugin with Apache License 2.0 | 5 votes |
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; }
Example #14
Source File: WebMvcUtils.java From spring-webmvc-support with GNU General Public License v3.0 | 2 votes |
/** * Determine whether the Bean Type is present annotated by {@link ControllerAdvice} * * @param beanType Bean Type * @return If {@link ControllerAdvice} bean type is present , return <code>true</code> , or <code>false</code>. */ public static boolean isControllerAdviceBeanType(Class<?> beanType) { return AnnotationUtils.findAnnotation(beanType, ControllerAdvice.class) != null; }