@Test public void controllerAdviceWithNestedException() { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class); ctx.refresh(); ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); resolver.setApplicationContext(ctx); resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(new ServletRequestBindingException("message")); assertNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)); }
exceptionHandlerResolver.setContentNegotiationManager(mvcContentNegotiationManager()); exceptionHandlerResolver.setMessageConverters(getMessageConverters()); exceptionHandlerResolver.setCustomArgumentResolvers(getArgumentResolvers()); exceptionHandlerResolver.setCustomReturnValueHandlers(getReturnValueHandlers()); if (jackson2Present) { exceptionHandlerResolver.setResponseBodyAdvice( Collections.singletonList(new JsonViewResponseBodyAdvice())); exceptionHandlerResolver.setApplicationContext(this.applicationContext); exceptionHandlerResolver.afterPropertiesSet(); exceptionResolvers.add(exceptionHandlerResolver);
private void initExceptionHandlerAdviceCache() { if (getApplicationContext() == null) { return; List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext()); AnnotationAwareOrderComparator.sort(adviceBeans);
/** * Return the list of return value handlers to use including built-in and * custom handlers provided via {@link #setReturnValueHandlers}. */ protected List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers() { List<HandlerMethodReturnValueHandler> handlers = new ArrayList<>(); // Single-purpose return value types handlers.add(new ModelAndViewMethodReturnValueHandler()); handlers.add(new ModelMethodProcessor()); handlers.add(new ViewMethodReturnValueHandler()); handlers.add(new HttpEntityMethodProcessor( getMessageConverters(), this.contentNegotiationManager, this.responseBodyAdvice)); // Annotation-based return value types handlers.add(new ModelAttributeMethodProcessor(false)); handlers.add(new RequestResponseBodyMethodProcessor( getMessageConverters(), this.contentNegotiationManager, this.responseBodyAdvice)); // Multi-purpose return value types handlers.add(new ViewNameMethodReturnValueHandler()); handlers.add(new MapMethodProcessor()); // Custom return value types if (getCustomReturnValueHandlers() != null) { handlers.addAll(getCustomReturnValueHandlers()); } // Catch-all handlers.add(new ModelAttributeMethodProcessor(true)); return handlers; }
@BeforeClass public static void setupOnce() { ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); resolver.afterPropertiesSet(); RESOLVER_COUNT = resolver.getArgumentResolvers().getResolvers().size(); HANDLER_COUNT = resolver.getReturnValueHandlers().getHandlers().size(); }
/** * @param resolvers exceptionResolvers * @deprecated see: {@link org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport * #addDefaultHandlerExceptionResolvers(List) addDefaultHandlerExceptionResolvers} */ @Deprecated protected final void addDefaultHandlerExceptionResolvers( // final List<HandlerExceptionResolver> resolvers // ) { final List<ResponseBodyAdvice<?>> interceptors = newArrayList(); if (getJackson2Present()) { interceptors.add(new JsonViewResponseBodyAdvice()); } final ExceptionHandlerExceptionResolver handlerExceptionResolver = new ExceptionHandlerExceptionResolver(); handlerExceptionResolver.setMessageConverters(this.defaultHttpEntityMethodProcessor.getMessageConverters()); handlerExceptionResolver.setResponseBodyAdvice(interceptors); handlerExceptionResolver.setApplicationContext(this.applicationContext); handlerExceptionResolver.afterPropertiesSet(); final ResponseStatusExceptionResolver responseStatusExceptionResolver = new ResponseStatusExceptionResolver(); responseStatusExceptionResolver.setMessageSource(this.applicationContext); resolvers.add(handlerExceptionResolver); // resolvers add responseStatusExceptionResolver ? // resolvers add new DefaultHandlerExceptionResolver ? }
@Test public void resolveExceptionControllerAdviceNoHandler() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyControllerAdviceConfig.class); this.resolver.setApplicationContext(ctx); this.resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, null, ex); assertNotNull("Exception was not handled", mav); assertTrue(mav.isEmpty()); assertEquals("DefaultTestExceptionResolver: IllegalStateException", this.response.getContentAsString()); }
@Override public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { ExceptionHandlerExceptionResolver er = new ExceptionHandlerExceptionResolver(); er.setCustomArgumentResolvers(defaultMethodArgumentResolvers()); er.setMessageConverters(defaultMessageConverters()); configurerDelegate.configureExceptionHandlerExceptionResolver(er); er.afterPropertiesSet(); exceptionResolvers.add(0, er); }
@Before public void setup() throws Exception { this.resolver = new ExceptionHandlerExceptionResolver(); this.resolver.setWarnLogCategory(this.resolver.getClass().getName()); this.request = new MockHttpServletRequest("GET", "/"); this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); this.response = new MockHttpServletResponse(); }
@Before public void setUp() throws Exception { final ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver(); //here we need to setup a dummy application context that only registers the GlobalControllerExceptionHandler final StaticApplicationContext applicationContext = new StaticApplicationContext(); applicationContext.registerBeanDefinition("advice", new RootBeanDefinition(GlobalControllerExceptionHandler.class, null, null)); //set the application context of the resolver to the dummy application context we just created exceptionHandlerExceptionResolver.setApplicationContext(applicationContext); //needed in order to force the exception resolver to update it's internal caches exceptionHandlerExceptionResolver.afterPropertiesSet(); mockMvc = MockMvcBuilders.standaloneSetup(controller).setHandlerExceptionResolvers(exceptionHandlerExceptionResolver).build(); }
/** * Protected method for plugging in a custom subclass of * {@link ExceptionHandlerExceptionResolver}. * @since 4.3 */ protected ExceptionHandlerExceptionResolver createExceptionHandlerExceptionResolver() { return new ExceptionHandlerExceptionResolver(); }
@Test public void nullHandler() { Object handler = null; this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handler, null); assertNull("Exception can be resolved only if there is a HandlerMethod", mav); }
@Override public void configureHandlerExceptionResolvers( final List<HandlerExceptionResolver> exceptionResolvers) { ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver(); exceptionHandlerExceptionResolver.setMessageConverters(getMessageConverters()); exceptionHandlerExceptionResolver.afterPropertiesSet(); exceptionResolvers.add(exceptionHandlerExceptionResolver); exceptionResolvers.add(new ResponseStatusExceptionResolver()); exceptionResolvers.add(new LogUncaughtException(applicationContext)); }
private ExceptionHandlerExceptionResolver createExceptionResolver() { ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() { @Override protected ServletInvocableHandlerMethod getExceptionHandlerMethod( HandlerMethod handlerMethod, Exception exception) { Method method = new ExceptionHandlerMethodResolver( MyAppExceptionHandler.class).resolveMethod(exception); return new ServletInvocableHandlerMethod( new MyAppExceptionHandler(), method); } }; exceptionResolver.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); exceptionResolver.afterPropertiesSet(); return exceptionResolver; }
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller) .setHandlerExceptionResolvers(withExceptionControllerAdvice()) .build(); private ExceptionHandlerExceptionResolver withExceptionControllerAdvice() { final ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() { @Override protected ServletInvocableHandlerMethod getExceptionHandlerMethod(final HandlerMethod handlerMethod, final Exception exception) { Method method = new ExceptionHandlerMethodResolver(TestAdvice.class).resolveMethod(exception); if (method != null) { return new ServletInvocableHandlerMethod(new TestAdvice(), method); } return super.getExceptionHandlerMethod(handlerMethod, exception); } }; exceptionResolver.afterPropertiesSet(); return exceptionResolver; }
@Test public void setCustomArgumentResolvers() { HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver(); this.resolver.setCustomArgumentResolvers(Collections.singletonList(resolver)); this.resolver.afterPropertiesSet(); assertTrue(this.resolver.getArgumentResolvers().getResolvers().contains(resolver)); assertMethodProcessorCount(RESOLVER_COUNT + 1, HANDLER_COUNT); }
@Test public void setCustomReturnValueHandlers() { HandlerMethodReturnValueHandler handler = new ViewNameMethodReturnValueHandler(); this.resolver.setCustomReturnValueHandlers(Collections.singletonList(handler)); this.resolver.afterPropertiesSet(); assertTrue(this.resolver.getReturnValueHandlers().getHandlers().contains(handler)); assertMethodProcessorCount(RESOLVER_COUNT, HANDLER_COUNT + 1); }
@Test public void setReturnValueHandlers() { HandlerMethodReturnValueHandler handler = new ModelMethodProcessor(); this.resolver.setReturnValueHandlers(Collections.singletonList(handler)); this.resolver.afterPropertiesSet(); assertMethodProcessorCount(RESOLVER_COUNT, 1); }
@Test public void setArgumentResolvers() { HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver(); this.resolver.setArgumentResolvers(Collections.singletonList(resolver)); this.resolver.afterPropertiesSet(); assertMethodProcessorCount(1, HANDLER_COUNT); }
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; }