Search in sources :

Example 1 with ExceptionHandlerExceptionResolver

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());
}
Also used : ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)

Example 2 with ExceptionHandlerExceptionResolver

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());
}
Also used : ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Test(org.junit.Test)

Example 3 with ExceptionHandlerExceptionResolver

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();
    }
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) List(java.util.List) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

ExceptionHandlerExceptionResolver (org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver)3 Test (org.junit.Test)2 ApplicationContext (org.springframework.context.ApplicationContext)2 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)2 HandlerExceptionResolverComposite (org.springframework.web.servlet.handler.HandlerExceptionResolverComposite)2 ResponseStatusExceptionResolver (org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver)2 DefaultHandlerExceptionResolver (org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)2 List (java.util.List)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)1 HandlerExceptionResolver (org.springframework.web.servlet.HandlerExceptionResolver)1 JsonViewResponseBodyAdvice (org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice)1 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)1