Python 官方文档:入门教程 => 点击学习
目录1.springMVC默认三个异常处理类2.@ExceptionHandler注解异常3.@ResponseStatus注解异常4.DefaultHandlerException
如果以上3个异常解析器都无法处理,会上抛给Tomcat,处理异常内部的默认工作流程:所有异常解析器依次尝试解析,解析完成进行后续操作,解析失败,下一个解析器继续尝试解析。
@ExceptionHandler标注在方法上
@ExceptionHandler(value = { ArithmeticException.class, NullPointerException.class })
// 告诉SpringMVC,这个方法专门处理这个类发送的所有异常
public ModelAndView handleException01(Exception exception) {
System.out.println("handleException01..." + exception);
ModelAndView view = new ModelAndView("myerror");
view.addObject("ex", exception);
return view;
}
@ExceptionHandler统一异常处理
@ControllerAdvice
public class MyException {
// 处理空指针异常
@ExceptionHandler(value = { NullPointerException.class })
public ModelAndView handleException01(Exception exception) {
System.out.println("全局的handleException01..." + exception);
ModelAndView view = new ModelAndView("myerror");
view.addObject("ex", exception);
return view;
}
// 处理算数异常
@ExceptionHandler(value = { ArithmeticException.class })
public ModelAndView handleException02(Exception exception) {
System.out.println("全局的handleException02..." + exception);
ModelAndView view = new ModelAndView("myerror");
view.addObject("ex", exception);
return view;
}
}
@ResponseStatus注解标注在自定义异常上,用于设置自定义异常信息
@ResponseStatus(reason = "用户被拒绝登录", value = httpstatus.NOT_ACCEPTABLE)
public class UsernameNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
DefaultHandlerExceptionResolver包含以下默认异常
源码:
public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
...
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
try {
if (ex instanceof NoSuchRequestHandlingMethodException) {
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, request, response,
handler);
}
else if (ex instanceof HttpRequestMethodNotSupportedException) {
return handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException) ex, request,
response, handler);
}
else if (ex instanceof HttpMediaTypeNotSupportedException) {
return handleHttpMediaTypeNotSupported((HttpMediaTypeNotSupportedException) ex, request, response,
handler);
}
else if (ex instanceof HttpMediaTypeNotAcceptableException) {
return handleHttpMediaTypeNotAcceptable((HttpMediaTypeNotAcceptableException) ex, request, response,
handler);
}
else if (ex instanceof MissingPathVariableException) {
return handleMissingPathVariable((MissingPathVariableException) ex, request,
response, handler);
}
else if (ex instanceof MissingServletRequestParameterException) {
return handleMissingServletRequestParameter((MissingServletRequestParameterException) ex, request,
response, handler);
}
else if (ex instanceof ServletRequestBindingException) {
return handleServletRequestBindingException((ServletRequestBindingException) ex, request, response,
handler);
}
else if (ex instanceof ConversionNotSupportedException) {
return handleConversionNotSupported((ConversionNotSupportedException) ex, request, response, handler);
}
else if (ex instanceof TypeMismatchException) {
return handleTypeMismatch((TypeMismatchException) ex, request, response, handler);
}
else if (ex instanceof HttpMessageNotReadableException) {
return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, request, response, handler);
}
else if (ex instanceof HttpMessageNotWritableException) {
return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler);
}
else if (ex instanceof MethodArgumentNotValidException) {
return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response,
handler);
}
else if (ex instanceof MissingServletRequestPartException) {
return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request,
response, handler);
}
else if (ex instanceof BindException) {
return handleBindException((BindException) ex, request, response, handler);
}
else if (ex instanceof NoHandlerFoundException) {
return handleNoHandlerFoundException((NoHandlerFoundException) ex, request, response, handler);
}
}
catch (Exception handlerException) {
if (logger.isWarnEnabled()) {
logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
}
}
return null;
}
...
}
如下HttpRequestMethodNotSupportedException请求方式不对。使用GET对POST方法进行访问
@RequestMapping(value = "/handle03", method = RequestMethod.POST)
public String postMethod() {
return "success";
}
若没有对应异常处理类,会进入对应服务器错误页面
以上就是详解SpringMVC中的异常处理的详细内容,更多关于SpringMVC 异常处理的资料请关注编程网其它相关文章!
--结束END--
本文标题: 详解SpringMVC中的异常处理
本文链接: https://lsjlt.com/news/122222.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0