org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver Java Examples

The following examples show how to use org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractControllerTest.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected ExceptionHandlerExceptionResolver createExceptionResolver() {

        final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("rest/messages");
        messageSource.setUseCodeAsDefaultMessage(true);

        ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {

            @Override
            protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
                Method method = new ExceptionHandlerMethodResolver(RestExceptionHandler.class).resolveMethod(exception);
                RestExceptionHandler validationHandler = new RestExceptionHandler();
                validationHandler.setMessageSource(messageSource);
                return new ServletInvocableHandlerMethod(validationHandler, method);
            }
        };

        exceptionResolver.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        exceptionResolver.afterPropertiesSet();
        return exceptionResolver;
    }
 
Example #2
Source File: WebMvcConfigurationSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.<ResponseBodyAdvice<?>>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());
}
 
Example #3
Source File: WebMvcConfigurationSupport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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 exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver();
	exceptionHandlerExceptionResolver.setContentNegotiationManager(mvcContentNegotiationManager());
	exceptionHandlerExceptionResolver.setMessageConverters(getMessageConverters());
	if (jackson2Present) {
		List<ResponseBodyAdvice<?>> interceptors = new ArrayList<ResponseBodyAdvice<?>>();
		interceptors.add(new JsonViewResponseBodyAdvice());
		exceptionHandlerExceptionResolver.setResponseBodyAdvice(interceptors);
	}
	exceptionHandlerExceptionResolver.setApplicationContext(this.applicationContext);
	exceptionHandlerExceptionResolver.afterPropertiesSet();
	exceptionResolvers.add(exceptionHandlerExceptionResolver);

	ResponseStatusExceptionResolver responseStatusExceptionResolver = new ResponseStatusExceptionResolver();
	responseStatusExceptionResolver.setMessageSource(this.applicationContext);
	exceptionResolvers.add(responseStatusExceptionResolver);

	exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
 
Example #4
Source File: WebMvcConfigurationSupport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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());
}
 
Example #5
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
}
 
Example #6
Source File: DelegatingWebMvcConfigurationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void handlerExceptionResolver() throws Exception {

	delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
	delegatingConfig.handlerExceptionResolver();

	verify(webMvcConfigurer).configureMessageConverters(converters.capture());
	verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
	verify(webMvcConfigurer).configureHandlerExceptionResolvers(exceptionResolvers.capture());

	assertEquals(3, exceptionResolvers.getValue().size());
	assertTrue(exceptionResolvers.getValue().get(0) instanceof ExceptionHandlerExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(1) instanceof ResponseStatusExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(2) instanceof DefaultHandlerExceptionResolver);
	assertTrue(converters.getValue().size() > 0);
}
 
Example #7
Source File: WebMvcConfigurationSupport.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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) {
	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());
}
 
Example #8
Source File: SbpMvcPatchAutoConfiguration.java    From sbp with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(WebMvcRegistrations.class)
public WebMvcRegistrations mvcRegistrations() {
	return new WebMvcRegistrations() {
		@Override
		public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
			return new PluginRequestMappingHandlerMapping();
		}

		@Override
		public RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() {
			return null;
		}

		@Override
		public ExceptionHandlerExceptionResolver getExceptionHandlerExceptionResolver() {
			return null;
		}
	};
}
 
Example #9
Source File: SpringMvcPolyfill.java    From Milkomeda with MIT License 6 votes vote down vote up
/**
 * 动态添加异常切面
 * @param handlerExceptionResolver  HandlerExceptionResolver
 * @param beanFactory               BeanFactory
 * @param beanName                  异常拦截处理器bean名
 */
public static void addDynamicExceptionAdvice(HandlerExceptionResolver handlerExceptionResolver, BeanFactory beanFactory, String beanName) {
    if (handlerExceptionResolver instanceof HandlerExceptionResolverComposite) {
        List<HandlerExceptionResolver> exceptionResolvers = ((HandlerExceptionResolverComposite) handlerExceptionResolver).getExceptionResolvers();
        for (HandlerExceptionResolver exceptionResolver : exceptionResolvers) {
            if (exceptionResolver instanceof ExceptionHandlerExceptionResolver) {
                // TODO <mark> 由于使用底层API, 这个exceptionHandlerAdviceCache属性在未来版本中可能会改
                Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> resolverMap = ReflectUtil.invokeFieldPath(exceptionResolver, "exceptionHandlerAdviceCache");
                if (resolverMap == null) {
                    resolverMap = new HashMap<>(2);
                }
                // 仿Spring MVC源码创建advice
                ControllerAdviceBean adviceBean = new ControllerAdviceBean(beanName, ApplicationContextHolder.get(), null);
                Class<?> beanType = adviceBean.getBeanType();
                if (beanType == null) {
                    throw new IllegalStateException("SpringMvcPolyfill find unresolvable type for ControllerAdviceBean: " + adviceBean);
                }
                ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
                if (resolver.hasExceptionMappings()) {
                    resolverMap.put(adviceBean, resolver);
                }
                break;
            }
        }
    }
}
 
Example #10
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
}
 
Example #11
Source File: SpringMvcPolyfill.java    From Milkomeda with MIT License 5 votes vote down vote up
/**
 * 动态添加消息体响应切面
 * @param returnValueHandlers       响应处理器列表(需要注入 {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#getReturnValueHandlers()})
 * @param handlerExceptionResolver  异常处理解析器(需要注入 {@link org.springframework.web.servlet.HandlerExceptionResolver})
 * @param responseBodyAdvice        ResponseBodyAdvice
 */
public static void addDynamicResponseBodyAdvice(List<HandlerMethodReturnValueHandler> returnValueHandlers, HandlerExceptionResolver handlerExceptionResolver, ResponseBodyAdvice<?> responseBodyAdvice) {
    if (CollectionUtils.isEmpty(returnValueHandlers)) {
        return;
    }
    // 下面这行添加不起作用,由于内部构建已经完成
    // adapter.setResponseBodyAdvice(Collections.singletonList(responseBodyAdvice));
    for (HandlerMethodReturnValueHandler returnValueHandler : returnValueHandlers) {
        // 只有AbstractMessageConverterMethodArgumentResolver继承类型(主要是HttpEntityMethodProcessor、RequestResponseBodyMethodProcessor)有Advice Chain
        if (returnValueHandler instanceof AbstractMessageConverterMethodArgumentResolver) {
            // TODO <mark> 由于使用底层API, 这个advice.responseBodyAdvice属性在未来版本中可能会改
            List<Object> advices = ReflectUtil.invokeFieldPath(returnValueHandler, "advice.responseBodyAdvice");
            if (CollectionUtils.isEmpty(advices)) {
                continue;
            }
            advices.add(responseBodyAdvice);
        }
    }

    // 动态添加到异常处理(因为源码流程中的异常处理是新加载的HandlerExceptionResolver,与正常响应处理不是同个处理集)
    if (handlerExceptionResolver instanceof HandlerExceptionResolverComposite) {
        // SpringMVC默认为注册HandlerExceptionResolverComposite的Bean
        List<HandlerExceptionResolver> exceptionResolvers = ((HandlerExceptionResolverComposite) handlerExceptionResolver).getExceptionResolvers();
        if (CollectionUtils.isEmpty(exceptionResolvers)) {
            return;
        }
        for (HandlerExceptionResolver exceptionResolver : exceptionResolvers) {
            if (exceptionResolver instanceof ExceptionHandlerExceptionResolver) {
                HandlerMethodReturnValueHandlerComposite returnValueHandlerComposite = ((ExceptionHandlerExceptionResolver) exceptionResolver).getReturnValueHandlers();
                if (returnValueHandlerComposite == null) {
                    return;
                }
                SpringMvcPolyfill.addDynamicResponseBodyAdvice(returnValueHandlerComposite.getHandlers(), null, responseBodyAdvice);
            }
        }
    }
}
 
Example #12
Source File: LegacyDetectorControllerTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    this.controllerUnderTest = new LegacyDetectorController();
    mockMvc = MockMvcBuilders.standaloneSetup(controllerUnderTest).setHandlerExceptionResolvers(new ExceptionHandlerExceptionResolver()).build();

    MockitoAnnotations.initMocks(this);
    initTestObjects();
    initDependencies();
}
 
Example #13
Source File: DetectorControllerTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    this.controllerUnderTest = new DetectorController();
    mockMvc = MockMvcBuilders.standaloneSetup(controllerUnderTest).setHandlerExceptionResolvers(new ExceptionHandlerExceptionResolver()).build();

    MockitoAnnotations.initMocks(this);
    initTestObjects();
    initDependencies();
}
 
Example #14
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
	}
}
 
Example #15
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
	}
}
 
Example #16
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testMessageConverters() {
	loadBeanDefinitions("mvc-config-message-converters.xml");
	verifyMessageConverters(this.appContext.getBean(RequestMappingHandlerAdapter.class), true);
	verifyMessageConverters(this.appContext.getBean(ExceptionHandlerExceptionResolver.class), true);
	verifyRequestResponseBodyAdvice(this.appContext.getBean(RequestMappingHandlerAdapter.class));
	verifyResponseBodyAdvice(this.appContext.getBean(ExceptionHandlerExceptionResolver.class));
}
 
Example #17
Source File: DelegatingWebMvcConfigurationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void handlerExceptionResolver() throws Exception {
	delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
	delegatingConfig.handlerExceptionResolver();

	verify(webMvcConfigurer).configureMessageConverters(converters.capture());
	verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
	verify(webMvcConfigurer).configureHandlerExceptionResolvers(exceptionResolvers.capture());

	assertEquals(3, exceptionResolvers.getValue().size());
	assertTrue(exceptionResolvers.getValue().get(0) instanceof ExceptionHandlerExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(1) instanceof ResponseStatusExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(2) instanceof DefaultHandlerExceptionResolver);
	assertTrue(converters.getValue().size() > 0);
}
 
Example #18
Source File: DelegatingWebMvcConfigurationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void handlerExceptionResolver() throws Exception {
	delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
	delegatingConfig.handlerExceptionResolver(delegatingConfig.mvcContentNegotiationManager());

	verify(webMvcConfigurer).configureMessageConverters(converters.capture());
	verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
	verify(webMvcConfigurer).configureHandlerExceptionResolvers(exceptionResolvers.capture());

	assertEquals(3, exceptionResolvers.getValue().size());
	assertTrue(exceptionResolvers.getValue().get(0) instanceof ExceptionHandlerExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(1) instanceof ResponseStatusExceptionResolver);
	assertTrue(exceptionResolvers.getValue().get(2) instanceof DefaultHandlerExceptionResolver);
	assertTrue(converters.getValue().size() > 0);
}
 
Example #19
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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();
	}
}
 
Example #20
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testMessageConverters() {
	loadBeanDefinitions("mvc-config-message-converters.xml");
	verifyMessageConverters(this.appContext.getBean(RequestMappingHandlerAdapter.class), true);
	verifyMessageConverters(this.appContext.getBean(ExceptionHandlerExceptionResolver.class), true);
	verifyRequestResponseBodyAdvice(this.appContext.getBean(RequestMappingHandlerAdapter.class));
	verifyResponseBodyAdvice(this.appContext.getBean(ExceptionHandlerExceptionResolver.class));
}
 
Example #21
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageConverters() {
	loadBeanDefinitions("mvc-config-message-converters.xml");
	verifyMessageConverters(appContext.getBean(RequestMappingHandlerAdapter.class), true);
	verifyMessageConverters(appContext.getBean(ExceptionHandlerExceptionResolver.class), true);
	verifyRequestResponseBodyAdvice(appContext.getBean(RequestMappingHandlerAdapter.class));
	verifyResponseBodyAdvice(appContext.getBean(ExceptionHandlerExceptionResolver.class));
}
 
Example #22
Source File: RestConfiguration.java    From moserp with Apache License 2.0 4 votes vote down vote up
@Override
public void configureExceptionHandlerExceptionResolver(ExceptionHandlerExceptionResolver exceptionResolver) {
    exceptionResolver.setMessageConverters(HttpMessageConverterUtils.getDefaultHttpMessageConverters());
}
 
Example #23
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageConvertersWithoutDefaultRegistrations() {
	loadBeanDefinitions("mvc-config-message-converters-defaults-off.xml");
	verifyMessageConverters(appContext.getBean(RequestMappingHandlerAdapter.class), false);
	verifyMessageConverters(appContext.getBean(ExceptionHandlerExceptionResolver.class), false);
}
 
Example #24
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testArgumentResolvers() {
	loadBeanDefinitions("mvc-config-argument-resolvers.xml");
	testArgumentResolvers(this.appContext.getBean(RequestMappingHandlerAdapter.class));
	testArgumentResolvers(this.appContext.getBean(ExceptionHandlerExceptionResolver.class));
}
 
Example #25
Source File: SecKillAdminApplicationTest.java    From seckill with Apache License 2.0 4 votes vote down vote up
private ExceptionHandlerExceptionResolver withExceptionControllerAdvice() {
  final ExceptionHandlerExceptionResolver exceptionResolver = new InvocationExceptionHandlerExceptionResolver();
  exceptionResolver.afterPropertiesSet();
  return exceptionResolver;
}
 
Example #26
Source File: SecKillStartedPromotionTest.java    From seckill with Apache License 2.0 4 votes vote down vote up
private ExceptionHandlerExceptionResolver withExceptionControllerAdvice() {
  final ExceptionHandlerExceptionResolver exceptionResolver = new InvocationExceptionHandlerExceptionResolver();
  exceptionResolver.afterPropertiesSet();
  return exceptionResolver;
}
 
Example #27
Source File: SecKillPromotionBootstrapTest.java    From seckill with Apache License 2.0 4 votes vote down vote up
private ExceptionHandlerExceptionResolver withExceptionControllerAdvice() {
  final ExceptionHandlerExceptionResolver exceptionResolver = new InvocationExceptionHandlerExceptionResolver();
  exceptionResolver.afterPropertiesSet();
  return exceptionResolver;
}
 
Example #28
Source File: SecKillRecoveryApplicationTest.java    From seckill with Apache License 2.0 4 votes vote down vote up
private ExceptionHandlerExceptionResolver withExceptionControllerAdvice() {
  final ExceptionHandlerExceptionResolver exceptionResolver = new InvocationExceptionHandlerExceptionResolver();
  exceptionResolver.afterPropertiesSet();
  return exceptionResolver;
}
 
Example #29
Source File: SecKillCommandApplicationTest.java    From seckill with Apache License 2.0 4 votes vote down vote up
private ExceptionHandlerExceptionResolver withExceptionControllerAdvice() {
  final ExceptionHandlerExceptionResolver exceptionResolver = new InvocationExceptionHandlerExceptionResolver();
  exceptionResolver.afterPropertiesSet();
  return exceptionResolver;
}
 
Example #30
Source File: BladeWebMvcRegistrations.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ExceptionHandlerExceptionResolver getExceptionHandlerExceptionResolver() {
	return null;
}