Search in sources :

Example 1 with ResponseStatusExceptionResolver

use of org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver 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 ResponseStatusExceptionResolver

use of org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver 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

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