Example usage for org.springframework.web.method ControllerAdviceBean isApplicableToBeanType

List of usage examples for org.springframework.web.method ControllerAdviceBean isApplicableToBeanType

Introduction

In this page you can find the example usage for org.springframework.web.method ControllerAdviceBean isApplicableToBeanType.

Prototype

public boolean isApplicableToBeanType(@Nullable Class<?> beanType) 

Source Link

Document

Check whether the given bean type should be advised by this ControllerAdviceBean .

Usage

From source file:org.springframework.web.reactive.result.method.annotation.ControllerMethodResolver.java

/**
 * Find an {@code @ExceptionHandler} method in {@code @ControllerAdvice}
 * components or in the controller of the given {@code @RequestMapping} method.
 *//*w  w w  .j ava2s .  c  o m*/
@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;
}

From source file:org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain.java

@SuppressWarnings("unchecked")
public <T> T invoke(T body, MethodParameter returnType, MediaType selectedContentType,
        Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
        ServerHttpResponse response) {/*w  w w .jav a 2  s .  c o  m*/

    if (this.advice != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Invoking ResponseBodyAdvice chain for body=" + body);
        }
        for (Object advice : this.advice) {
            if (advice instanceof ControllerAdviceBean) {
                ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
                if (!adviceBean.isApplicableToBeanType(returnType.getContainingClass())) {
                    continue;
                }
                advice = adviceBean.resolveBean();
            }
            if (advice instanceof ResponseBodyAdvice) {
                ResponseBodyAdvice<T> typedAdvice = (ResponseBodyAdvice<T>) advice;
                if (typedAdvice.supports(returnType, selectedConverterType)) {
                    body = typedAdvice.beforeBodyWrite(body, returnType, selectedContentType,
                            selectedConverterType, request, response);
                }
            } else {
                throw new IllegalStateException("Expected ResponseBodyAdvice: " + advice);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("After ResponseBodyAdvice chain body=" + body);
        }
    }
    return body;
}

From source file:org.springframework.web.servlet.mvc.method.annotation.ResponseBodyInterceptorChain.java

public <T> T invoke(T body, MediaType contentType, Class<? extends HttpMessageConverter<T>> converterType,
        MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {

    if (this.interceptors != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Invoking ResponseBody interceptor chain for body=" + body);
        }/*  www.  ja v  a  2 s  .  co  m*/
        for (Object interceptor : this.interceptors) {
            if (interceptor instanceof ControllerAdviceBean) {
                ControllerAdviceBean adviceBean = (ControllerAdviceBean) interceptor;
                if (!adviceBean.isApplicableToBeanType(returnType.getContainingClass())) {
                    continue;
                }
                interceptor = adviceBean.resolveBean();
            }
            Assert.state(interceptor instanceof ResponseBodyInterceptor);
            body = ((ResponseBodyInterceptor) interceptor).beforeBodyWrite(body, contentType, converterType,
                    returnType, request, response);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("After interceptor chain body=" + body);
        }
    }
    return body;
}