Search in sources :

Example 1 with HandlerExceptionResolver

use of org.springframework.web.servlet.HandlerExceptionResolver in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method testLogResolvedExceptionCustomization.

private void testLogResolvedExceptionCustomization(final boolean expected) {
    HandlerExceptionResolver exceptionResolver = this.context.getBean(HandlerExceptionResolver.class);
    assertThat(exceptionResolver).isInstanceOf(HandlerExceptionResolverComposite.class);
    List<HandlerExceptionResolver> delegates = ((HandlerExceptionResolverComposite) exceptionResolver).getExceptionResolvers();
    for (HandlerExceptionResolver delegate : delegates) {
        if (delegate instanceof AbstractHandlerMethodAdapter) {
            assertThat(new DirectFieldAccessor(delegate).getPropertyValue("warnLogger")).is(new Condition<Object>() {

                @Override
                public boolean matches(Object value) {
                    return (expected ? value != null : value == null);
                }
            });
        }
    }
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) AbstractHandlerMethodAdapter(org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter)

Example 2 with HandlerExceptionResolver

use of org.springframework.web.servlet.HandlerExceptionResolver in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method handlerExceptionResolver.

/**
	 * Returns a {@link HandlerExceptionResolverComposite} containing a list
	 * of exception resolvers obtained either through
	 * {@link #configureHandlerExceptionResolvers(List)} or through
	 * {@link #addDefaultHandlerExceptionResolvers(List)}.
	 * <p><strong>Note:</strong> This method cannot be made final due to CGLib
	 * constraints. Rather than overriding it, consider overriding
	 * {@link #configureHandlerExceptionResolvers(List)}, which allows
	 * providing a list of resolvers.
	 */
@Bean
public HandlerExceptionResolver handlerExceptionResolver() {
    List<HandlerExceptionResolver> exceptionResolvers = new ArrayList<>();
    configureHandlerExceptionResolvers(exceptionResolvers);
    if (exceptionResolvers.isEmpty()) {
        addDefaultHandlerExceptionResolvers(exceptionResolvers);
    }
    extendHandlerExceptionResolvers(exceptionResolvers);
    HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite();
    composite.setOrder(0);
    composite.setExceptionResolvers(exceptionResolvers);
    return composite;
}
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) ArrayList(java.util.ArrayList) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) Bean(org.springframework.context.annotation.Bean)

Example 3 with HandlerExceptionResolver

use of org.springframework.web.servlet.HandlerExceptionResolver 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)

Example 4 with HandlerExceptionResolver

use of org.springframework.web.servlet.HandlerExceptionResolver in project spring-framework by spring-projects.

the class DelegatingWebMvcConfigurationTests method configureExceptionResolvers.

@Test
public void configureExceptionResolvers() throws Exception {
    List<WebMvcConfigurer> configurers = new ArrayList<>();
    configurers.add(new WebMvcConfigurerAdapter() {

        @Override
        public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
            exceptionResolvers.add(new DefaultHandlerExceptionResolver());
        }
    });
    delegatingConfig.setConfigurers(configurers);
    HandlerExceptionResolverComposite composite = (HandlerExceptionResolverComposite) delegatingConfig.handlerExceptionResolver();
    assertEquals("Only one custom converter is expected", 1, composite.getExceptionResolvers().size());
}
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) ArrayList(java.util.ArrayList) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) Test(org.junit.Test)

Aggregations

HandlerExceptionResolver (org.springframework.web.servlet.HandlerExceptionResolver)4 HandlerExceptionResolverComposite (org.springframework.web.servlet.handler.HandlerExceptionResolverComposite)4 ExceptionHandlerExceptionResolver (org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver)3 DefaultHandlerExceptionResolver (org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 List (java.util.List)1 ApplicationContext (org.springframework.context.ApplicationContext)1 Bean (org.springframework.context.annotation.Bean)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 ResponseStatusExceptionResolver (org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver)1 AbstractHandlerMethodAdapter (org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter)1