当前位置: 首页>>代码示例>>Java>>正文


Java HandlerExceptionResolver类代码示例

本文整理汇总了Java中org.springframework.web.servlet.HandlerExceptionResolver的典型用法代码示例。如果您正苦于以下问题:Java HandlerExceptionResolver类的具体用法?Java HandlerExceptionResolver怎么用?Java HandlerExceptionResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


HandlerExceptionResolver类属于org.springframework.web.servlet包,在下文中一共展示了HandlerExceptionResolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: resolveException

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
/**
 * Resolve the exception by iterating over the list of configured exception resolvers.
 * The first one to return a ModelAndView instance wins. Otherwise {@code null} is returned.
 */
@Override
public ModelAndView resolveException(HttpServletRequest request,
									 HttpServletResponse response,
									 Object handler,
									 Exception ex) {
	if (resolvers != null) {
		for (HandlerExceptionResolver handlerExceptionResolver : resolvers) {
			ModelAndView mav = handlerExceptionResolver.resolveException(request, response, handler, ex);
			if (mav != null) {
				return mav;
			}
		}
	}
	return null;
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:20,代码来源:HandlerExceptionResolverComposite.java

示例2: addDefaultHandlerExceptionResolvers

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的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());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:32,代码来源:WebMvcConfigurationSupport.java

示例3: configureHandlerExceptionResolvers

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Override
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	if (handlerExceptionResolvers == null) {
		return;
	}
	for (HandlerExceptionResolver resolver : handlerExceptionResolvers) {
		if (resolver instanceof ApplicationContextAware) {
			((ApplicationContextAware) resolver).setApplicationContext(getApplicationContext());
		}
		if (resolver instanceof InitializingBean) {
			try {
				((InitializingBean) resolver).afterPropertiesSet();
			}
			catch (Exception ex) {
				throw new IllegalStateException("Failure from afterPropertiesSet", ex);
			}
		}
		exceptionResolvers.add(resolver);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:21,代码来源:StandaloneMockMvcBuilder.java

示例4: configureHandlerExceptionResolvers

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	RestExceptionHandler handler = new RestExceptionHandler();
	handler.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);

	RestErrorDefinition<Exception> authErrorDefinition = new RestErrorDefinition<>(HttpStatus.BAD_REQUEST,
			ErrorType.ACCESS_DENIED,
			new DefaultExceptionMessageBuilder()
	);
	Map<Class<? extends Throwable>, RestErrorDefinition> errorMappings = ImmutableMap.<Class<? extends Throwable>, RestErrorDefinition>builder()
			.put(OAuth2Exception.class, authErrorDefinition)
			.put(AuthenticationException.class, authErrorDefinition)
			.putAll(ExceptionMappings.DEFAULT_MAPPING)

			.build();

	DefaultErrorResolver defaultErrorResolver = new DefaultErrorResolver(errorMappings);
	handler.setErrorResolver(new ReportPortalExceptionResolver(defaultErrorResolver));
	handler.setMessageConverters(messageConverters.getConverters());
	exceptionResolvers.add(handler);
}
 
开发者ID:reportportal,项目名称:service-authorization,代码行数:22,代码来源:AuthServerApplication.java

示例5: configureHandlerExceptionResolvers

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Override
protected void configureHandlerExceptionResolvers(
		List<HandlerExceptionResolver> exceptionResolvers) {
	super.configureHandlerExceptionResolvers(exceptionResolvers);
	if (exceptionResolvers.isEmpty()) {
		addDefaultHandlerExceptionResolvers(exceptionResolvers);
	}
	if (this.mvcProperties.isLogResolvedException()) {
		for (HandlerExceptionResolver resolver : exceptionResolvers) {
			if (resolver instanceof AbstractHandlerExceptionResolver) {
				((AbstractHandlerExceptionResolver) resolver)
						.setWarnLogCategory(resolver.getClass().getName());
			}
		}
	}
}
 
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:17,代码来源:WebMvcAutoConfiguration.java

示例6: testLogResolvedExceptionCustomization

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
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);
								}
							});
		}
	}
}
 
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:21,代码来源:WebMvcAutoConfigurationTests.java

示例7: testForwardingToError

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Test
public void testForwardingToError() throws Exception {

	EvernoteException evernoteException = new EvernoteException("MESSAGE", new EDAMUserException());

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();

	// mimic how exception info is registered in logic
	((HandlerExceptionResolver) errorAttributes).resolveException(request, response, null, evernoteException);
	request.setAttribute(WebUtils.ERROR_STATUS_CODE_ATTRIBUTE, HttpStatus.BAD_REQUEST.value()); // return error 400

	MockHttpServletRequestBuilder requestBuilder = post("/error");
	copyRequestAttributes(requestBuilder, request);

	ResultActions result = mockMvc.perform(requestBuilder.contentType(MediaType.APPLICATION_JSON));
	result.andDo(print());
	result.andExpect(status().isBadRequest());
	result.andExpect(jsonPath("$.timestamp").exists());
	result.andExpect(jsonPath("$.status").value(400));
	result.andExpect(jsonPath("$.error").exists());
	result.andExpect(jsonPath("$.exception").exists());
	result.andExpect(jsonPath("$.message").value("MESSAGE"));

}
 
开发者ID:ttddyy,项目名称:evernote-rest-webapp,代码行数:26,代码来源:StoreOperationControllerExceptionIntegrationTest.java

示例8: handlerExceptionResolver

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的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());
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:17,代码来源:WebMvcConfigurationSupportTests.java

示例9: configureHandlerExceptionResolvers

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {
        @Override
        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
            try {
                log.error("An error has occured: {}", ex.getMessage());

                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return new ModelAndView();
            } catch (Exception handlerException) {
                log.warn("Handling of [{}] resulted in Exception", ex.getClass().getName(), handlerException);
            }
            return null;
        }
    });
}
 
开发者ID:przodownikR1,项目名称:thymeleaf_kata,代码行数:18,代码来源:WebConfig.java

示例10: extendHandlerExceptionResolvers

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Override
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	exceptionResolvers.forEach(resolver -> {
		if (resolver instanceof ResponseStatusExceptionResolver)
			((ResponseStatusExceptionResolver) resolver).setMessageSource(null);
	});
}
 
开发者ID:yushijinhun,项目名称:akir,代码行数:8,代码来源:MvcConfig.java

示例11: customExceptionHandlerExceptionResolver

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Bean
public HandlerExceptionResolver customExceptionHandlerExceptionResolver() {
    CustomExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new CustomExceptionHandlerExceptionResolver(
            true);
    exceptionHandlerExceptionResolver.getErrorResolver()
                                     .add(new ErrorMappingResolver())
                                     .setDefaultErrorResolver(new DefaultErrorResolver());
    return exceptionHandlerExceptionResolver;
}
 
开发者ID:melthaw,项目名称:spring-backend-boilerplate,代码行数:10,代码来源:OpenApiWebMvcConfigurer.java

示例12: sentryExceptionResolver

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
/**
 * Configure a bean to record all exceptions thrown by the controllers
 * 
 * @return the handler exception resolver
 */
@Bean
public HandlerExceptionResolver sentryExceptionResolver() {
	if (this.isSentryEnabled) {
		return new io.sentry.spring.SentryExceptionResolver();
	} else {
		// allow definitions of other handler
		return null;
	}
}
 
开发者ID:lucapompei,项目名称:WebTemplate,代码行数:15,代码来源:SentryConfig.java

示例13: configureHandlerExceptionResolvers

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {
        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
            Result result = new Result();
            logger.warn(e.getMessage());
            if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误”
                result.setCode(ResultCode.FAIL).setMessage(e.getMessage());
            } else if (e instanceof IllegalArgumentException) {  //参数检查异常
                result.setCode(ResultCode.FAIL).setMessage(e.getMessage());
            }else if (e instanceof NoHandlerFoundException) {
                result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在");
            } else if (e instanceof ServletException) {
                result.setCode(ResultCode.FAIL).setMessage(e.getMessage());
            } else  if (e instanceof AccessDeniedException){
                result.setCode(ResultCode.PERMIT).setMessage(e.getMessage());
            } else {
                result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员");
                String message;
                if (handler instanceof HandlerMethod) {
                    HandlerMethod handlerMethod = (HandlerMethod) handler;
                    message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s",
                            request.getRequestURI(),
                            handlerMethod.getBean().getClass().getName(),
                            handlerMethod.getMethod().getName(),
                            e.getMessage());
                } else {
                    message = e.getMessage();
                }
                logger.error(message, e);
            }
            responseResult(response, result);
            return new ModelAndView();
        }

    });
}
 
开发者ID:pandboy,项目名称:pingguopai,代码行数:38,代码来源:WebMvcConfigurer.java

示例14: configureHandlerExceptionResolvers

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {
        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
            Result result = new Result();
            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;

                if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误”
                    result.setCode(ResultCode.FAIL).setMessage(e.getMessage());
                    logger.info(e.getMessage());
                } else {
                    result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员");
                    String message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s",
                            request.getRequestURI(),
                            handlerMethod.getBean().getClass().getName(),
                            handlerMethod.getMethod().getName(),
                            e.getMessage());
                    logger.error(message, e);
                }
            } else {
                if (e instanceof NoHandlerFoundException) {
                    result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在");
                } else {
                    result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(e.getMessage());
                    logger.error(e.getMessage(), e);
                }
            }
            responseResult(response, result);
            return new ModelAndView();
        }

    });
}
 
开发者ID:jeikerxiao,项目名称:SpringBootStudy,代码行数:35,代码来源:WebMvcConfig.java

示例15: configureHandlerExceptionResolvers

import org.springframework.web.servlet.HandlerExceptionResolver; //导入依赖的package包/类
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
	RestExceptionHandler handler = new RestExceptionHandler();
	handler.setOrder(Ordered.HIGHEST_PRECEDENCE);

	DefaultErrorResolver defaultErrorResolver = new DefaultErrorResolver(ExceptionMappings.DEFAULT_MAPPING);
	handler.setErrorResolver(new ReportPortalExceptionResolver(defaultErrorResolver));
	handler.setMessageConverters(Collections.singletonList(new MappingJackson2HttpMessageConverter()));
	exceptionResolvers.add(handler);
}
 
开发者ID:reportportal,项目名称:commons-bugtracking,代码行数:11,代码来源:BugTrackingApp.java


注:本文中的org.springframework.web.servlet.HandlerExceptionResolver类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。