本文整理汇总了Java中org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver类的典型用法代码示例。如果您正苦于以下问题:Java DefaultHandlerExceptionResolver类的具体用法?Java DefaultHandlerExceptionResolver怎么用?Java DefaultHandlerExceptionResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DefaultHandlerExceptionResolver类属于org.springframework.web.servlet.mvc.support包,在下文中一共展示了DefaultHandlerExceptionResolver类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addDefaultHandlerExceptionResolvers
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
/**
* 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());
}
示例2: supportsAllDefaultHandlerExceptionResolverExceptionTypes
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@Test
public void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception {
Class<ResponseEntityExceptionHandler> clazz = ResponseEntityExceptionHandler.class;
Method handleExceptionMethod = clazz.getMethod("handleException", Exception.class, WebRequest.class);
ExceptionHandler annotation = handleExceptionMethod.getAnnotation(ExceptionHandler.class);
List<Class<?>> exceptionTypes = Arrays.asList(annotation.value());
for (Method method : DefaultHandlerExceptionResolver.class.getDeclaredMethods()) {
Class<?>[] paramTypes = method.getParameterTypes();
if (method.getName().startsWith("handle") && (paramTypes.length == 4)) {
String name = paramTypes[0].getSimpleName();
assertTrue("@ExceptionHandler is missing " + name, exceptionTypes.contains(paramTypes[0]));
}
}
}
示例3: handlerExceptionResolver
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@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);
}
示例4: handlerExceptionResolver
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@Test
public void handlerExceptionResolver() throws Exception {
HandlerExceptionResolverComposite compositeResolver =
(HandlerExceptionResolverComposite) mvcConfiguration.handlerExceptionResolver();
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());
}
示例5: configureHandlerExceptionResolvers
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
// NOTE: Do not change the order! Only first resolution is used.
// By default Spring MVC does not log exceptions from @Controller methods.
if (!runtimeEnvironmentUtil.isIntegrationTestEnvironment()) {
exceptionResolvers.add(new LoggingHandlerExceptionResolver());
}
// Process exception using @ExceptionHandler method inside @Controller
exceptionResolvers.add(exceptionHandlerExceptionResolver());
// Return status code specified using @ResponseStatus in exception class
exceptionResolvers.add(new ResponseStatusExceptionResolver());
// Send HTTP status code depending on the exception which is mapped to error page in web.xml
exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
示例6: setup
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@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();
}
示例7: handlerExceptionResolver
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@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();
}
}
示例8: configureExceptionResolvers
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@Test
public void configureExceptionResolvers() throws Exception {
List<WebMvcConfigurer> configurers = new ArrayList<WebMvcConfigurer>();
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());
}
示例9: exceptionResolvers
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
protected HandlerExceptionResolver[] exceptionResolvers() {
HandlerExceptionResolver
defaultHandlerExceptionResolver =
new DefaultHandlerExceptionResolver();
return new HandlerExceptionResolver[]{uncheckedExceptionResolver(),
defaultHandlerExceptionResolver};
}
示例10: getHandlerExceptionResolverComposite
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@Bean
public HandlerExceptionResolverComposite getHandlerExceptionResolverComposite() {
HandlerExceptionResolverComposite result = new HandlerExceptionResolverComposite();
List<HandlerExceptionResolver> resolverList = new ArrayList<>();
resolverList.add(new ExceptionHandlerExceptionResolver());
resolverList.add(new ResponseStatusExceptionResolver());
resolverList.add(new Error500Resolver());
resolverList.add(new DefaultHandlerExceptionResolver());
result.setExceptionResolvers(resolverList);
return result;
}
示例11: setup
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@Before
public void setup() {
this.servletRequest = new MockHttpServletRequest();
this.servletResponse = new MockHttpServletResponse();
this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);
this.exceptionHandlerSupport = new ApplicationExceptionHandler();
this.defaultExceptionResolver = new DefaultHandlerExceptionResolver();
}
示例12: supportsAllDefaultHandlerExceptionResolverExceptionTypes
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
@Test
public void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception {
Method annotMethod = ResponseEntityExceptionHandler.class.getMethod("handleException", Exception.class, WebRequest.class);
ExceptionHandler annot = annotMethod.getAnnotation(ExceptionHandler.class);
List<Class<? extends Throwable>> supportedTypes = Arrays.asList(annot.value());
for (Method method : DefaultHandlerExceptionResolver.class.getDeclaredMethods()) {
Class<?>[] paramTypes = method.getParameterTypes();
if (method.getName().startsWith("handle") && (paramTypes.length == 4)) {
String name = paramTypes[0].getSimpleName();
assertTrue("@ExceptionHandler is missing " + name, supportedTypes.contains(paramTypes[0]));
}
}
}
示例13: initServlet
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
/**
* Initialize a DispatcherServlet instance registering zero or more controller classes
* and also providing additional bean definitions through a callback.
*/
@SuppressWarnings("serial")
protected WebApplicationContext initServlet(
final ApplicationContextInitializer<GenericWebApplicationContext> initializer,
final Class<?>... controllerClasses) throws ServletException {
final GenericWebApplicationContext wac = new GenericWebApplicationContext();
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
for (Class<?> clazz : controllerClasses) {
wac.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
}
Class<?> mappingType = RequestMappingHandlerMapping.class;
RootBeanDefinition beanDef = new RootBeanDefinition(mappingType);
beanDef.getPropertyValues().add("removeSemicolonContent", "false");
wac.registerBeanDefinition("handlerMapping", beanDef);
Class<?> adapterType = RequestMappingHandlerAdapter.class;
wac.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(adapterType));
Class<?> resolverType = ExceptionHandlerExceptionResolver.class;
wac.registerBeanDefinition("requestMappingResolver", new RootBeanDefinition(resolverType));
resolverType = ResponseStatusExceptionResolver.class;
wac.registerBeanDefinition("responseStatusResolver", new RootBeanDefinition(resolverType));
resolverType = DefaultHandlerExceptionResolver.class;
wac.registerBeanDefinition("defaultResolver", new RootBeanDefinition(resolverType));
if (initializer != null) {
initializer.initialize(wac);
}
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
return wac;
}
示例14: addDefaultHandlerExceptionResolvers
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; //导入依赖的package包/类
/**
* 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.setApplicationContext(this.applicationContext);
exceptionHandlerExceptionResolver.setContentNegotiationManager(mvcContentNegotiationManager());
exceptionHandlerExceptionResolver.setMessageConverters(getMessageConverters());
exceptionHandlerExceptionResolver.afterPropertiesSet();
exceptionResolvers.add(exceptionHandlerExceptionResolver);
exceptionResolvers.add(new ResponseStatusExceptionResolver());
exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}