List of usage examples for org.springframework.web.method ControllerAdviceBean resolveBean
public Object resolveBean()
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. jav a2 s.co 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) {//from w w w. ja v a 2s . 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); }/* ww w. j a v a 2 s . c om*/ 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; }