use of org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver in project spring-framework by spring-projects.
the class WebMvcConfigurationSupport method addDefaultHandlerExceptionResolvers.
/**
* A method available to subclasses for adding default {@link HandlerExceptionResolver}s.
* <p>Adds the following exception resolvers:
* <ul>
* <li>{@link ExceptionHandlerExceptionResolver}
* for handling exceptions through @{@link ExceptionHandler} methods.
* <li>{@link ResponseStatusExceptionResolver}
* for exceptions annotated with @{@link ResponseStatus}.
* <li>{@link DefaultHandlerExceptionResolver}
* for resolving known Spring exception types
* </ul>
*/
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
ExceptionHandlerExceptionResolver exceptionHandlerResolver = createExceptionHandlerExceptionResolver();
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);
ResponseStatusExceptionResolver responseStatusResolver = new ResponseStatusExceptionResolver();
responseStatusResolver.setMessageSource(this.applicationContext);
exceptionResolvers.add(responseStatusResolver);
exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
use of org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportTests method customArgumentResolvers.
@Test
public void customArgumentResolvers() {
ApplicationContext context = initContext(CustomArgumentResolverConfig.class);
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
HandlerExceptionResolverComposite composite = context.getBean(HandlerExceptionResolverComposite.class);
assertNotNull(adapter);
assertEquals(1, adapter.getCustomArgumentResolvers().size());
assertEquals(TestArgumentResolver.class, adapter.getCustomArgumentResolvers().get(0).getClass());
assertEquals(1, adapter.getCustomReturnValueHandlers().size());
assertEquals(TestReturnValueHandler.class, adapter.getCustomReturnValueHandlers().get(0).getClass());
assertNotNull(composite);
assertEquals(3, composite.getExceptionResolvers().size());
assertEquals(ExceptionHandlerExceptionResolver.class, composite.getExceptionResolvers().get(0).getClass());
ExceptionHandlerExceptionResolver resolver = (ExceptionHandlerExceptionResolver) composite.getExceptionResolvers().get(0);
assertEquals(1, resolver.getCustomArgumentResolvers().size());
assertEquals(TestArgumentResolver.class, resolver.getCustomArgumentResolvers().get(0).getClass());
assertEquals(1, resolver.getCustomReturnValueHandlers().size());
assertEquals(TestReturnValueHandler.class, resolver.getCustomReturnValueHandlers().get(0).getClass());
}
use of org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportTests method handlerExceptionResolver.
@Test
@SuppressWarnings("unchecked")
public void handlerExceptionResolver() throws Exception {
ApplicationContext context = initContext(WebConfig.class);
HandlerExceptionResolverComposite compositeResolver = context.getBean("handlerExceptionResolver", HandlerExceptionResolverComposite.class);
assertEquals(0, compositeResolver.getOrder());
List<HandlerExceptionResolver> expectedResolvers = compositeResolver.getExceptionResolvers();
assertEquals(ExceptionHandlerExceptionResolver.class, expectedResolvers.get(0).getClass());
assertEquals(ResponseStatusExceptionResolver.class, expectedResolvers.get(1).getClass());
assertEquals(DefaultHandlerExceptionResolver.class, expectedResolvers.get(2).getClass());
ExceptionHandlerExceptionResolver eher = (ExceptionHandlerExceptionResolver) expectedResolvers.get(0);
assertNotNull(eher.getApplicationContext());
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(eher);
List<Object> interceptors = (List<Object>) fieldAccessor.getPropertyValue("responseBodyAdvice");
assertEquals(1, interceptors.size());
assertEquals(JsonViewResponseBodyAdvice.class, interceptors.get(0).getClass());
LocaleContextHolder.setLocale(Locale.ENGLISH);
try {
ResponseStatusExceptionResolver rser = (ResponseStatusExceptionResolver) expectedResolvers.get(1);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
MockHttpServletResponse response = new MockHttpServletResponse();
rser.resolveException(request, response, context.getBean(TestController.class), new UserAlreadyExistsException());
assertEquals("User already exists!", response.getErrorMessage());
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
Aggregations