本文整理匯總了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;
}
示例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());
}
示例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);
}
}
示例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);
}
示例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());
}
示例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;
}
});
}
示例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);
});
}
示例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;
}
示例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;
}
}
示例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();
}
});
}
示例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();
}
});
}
示例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);
}