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);
}
});
}
}
}
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;
}
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();
}
}
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());
}
Aggregations