Search in sources :

Example 1 with DefaultHandlerExceptionResolver

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

use of org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method addDefaultHandlerExceptionResolvers.

/**
 * A method available to subclasses for adding default
 * {@link HandlerExceptionResolver HandlerExceptionResolvers}.
 * <p>Adds the following exception resolvers:
 * <ul>
 * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions through
 * {@link org.springframework.web.bind.annotation.ExceptionHandler} methods.
 * <li>{@link ResponseStatusExceptionResolver} for exceptions annotated with
 * {@link org.springframework.web.bind.annotation.ResponseStatus}.
 * <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring exception types
 * </ul>
 */
protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers, ContentNegotiationManager mvcContentNegotiationManager) {
    ExceptionHandlerExceptionResolver exceptionHandlerResolver = createExceptionHandlerExceptionResolver();
    exceptionHandlerResolver.setContentNegotiationManager(mvcContentNegotiationManager);
    exceptionHandlerResolver.setMessageConverters(getMessageConverters());
    exceptionHandlerResolver.setCustomArgumentResolvers(getArgumentResolvers());
    exceptionHandlerResolver.setCustomReturnValueHandlers(getReturnValueHandlers());
    if (jackson2Present) {
        exceptionHandlerResolver.setResponseBodyAdvice(Collections.singletonList(new JsonViewResponseBodyAdvice()));
    }
    if (this.applicationContext != null) {
        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 3 with DefaultHandlerExceptionResolver

use of org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver in project spring-framework by spring-projects.

the class ResponseEntityExceptionHandlerTests method setup.

@Before
public void setup() {
    this.servletRequest = new MockHttpServletRequest("GET", "/");
    this.servletResponse = new MockHttpServletResponse();
    this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);
    this.exceptionHandlerSupport = new ApplicationExceptionHandler();
    this.defaultExceptionResolver = new DefaultHandlerExceptionResolver();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 4 with DefaultHandlerExceptionResolver

use of org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver in project spring-framework by spring-projects.

the class DelegatingWebMvcConfigurationTests method configureExceptionResolvers.

@Test
public void configureExceptionResolvers() {
    WebMvcConfigurer configurer = new WebMvcConfigurer() {

        @Override
        public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
            resolvers.add(new DefaultHandlerExceptionResolver());
        }
    };
    webMvcConfig.setConfigurers(Collections.singletonList(configurer));
    HandlerExceptionResolverComposite composite = (HandlerExceptionResolverComposite) webMvcConfig.handlerExceptionResolver(webMvcConfig.mvcContentNegotiationManager());
    assertThat(composite.getExceptionResolvers().size()).as("Only one custom converter is expected").isEqualTo(1);
}
Also used : HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) List(java.util.List) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultHandlerExceptionResolver

use of org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver in project spring-boot by spring-projects.

the class CompositeHandlerExceptionResolver method extractResolvers.

private List<HandlerExceptionResolver> extractResolvers() {
    List<HandlerExceptionResolver> list = new ArrayList<>(this.beanFactory.getBeansOfType(HandlerExceptionResolver.class).values());
    list.remove(this);
    AnnotationAwareOrderComparator.sort(list);
    if (list.isEmpty()) {
        list.add(new DefaultErrorAttributes());
        list.add(new DefaultHandlerExceptionResolver());
    }
    return list;
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) ArrayList(java.util.ArrayList) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)

Aggregations

DefaultHandlerExceptionResolver (org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)5 ResponseStatusExceptionResolver (org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver)2 ExceptionHandlerExceptionResolver (org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver)2 JsonViewResponseBodyAdvice (org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Before (org.junit.Before)1 Test (org.junit.jupiter.api.Test)1 DefaultErrorAttributes (org.springframework.boot.web.servlet.error.DefaultErrorAttributes)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1 HandlerExceptionResolver (org.springframework.web.servlet.HandlerExceptionResolver)1 HandlerExceptionResolverComposite (org.springframework.web.servlet.handler.HandlerExceptionResolverComposite)1