下面列出了怎么用org.springframework.web.bind.annotation.ControllerAdvice的API类实例代码及写法,或者点击链接到github查看源代码。
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();
}
}
/**
* 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());
}
/**
* 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));
}
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();
}
}
/**
* 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());
}
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();
}
}
/**
* 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;
}
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;
}
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();
}
}
/**
* 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;
}
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;
}
@Override
protected Set<Class<?>> getValidClasses() {
Set<Class<?>> result = super.getValidClasses();
result.addAll(apiSource.getValidClasses(RestController.class));
result.addAll(apiSource.getValidClasses(ControllerAdvice.class));
return result;
}
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;
}
/**
* 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;
}
如果文章对你有帮助,欢迎点击上方按钮打赏作者