Springboot: exception处理
MVC:
package us.transcode.thymeleaf.exception; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class GlobalHandlerExceptionResolver implements HandlerExceptionResolver{ /** * ModelAndView 模型(数据)和视图, resolveException 当controller任意一个方法出现异常, 该controller没有自己处理异常, 进入该方法 * * @param request 当前请求对象 * @param response 当前请求响应对象 * @param handler 出现错误的方法对象 * @param ex 异常对 * @return ModelAndView */ @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){ System.out.println("Global HandlerExceptionResolver"); System.out.println("Exception: " + ex.getMessage()); ModelAndView modelAndView = new ModelAndView(); if(ex instanceof UsernameNotFoundException){ modelAndView.setViewName("login"); return modelAndView; } modelAndView.setViewName("500"); return modelAndView; } }
package us.transcode.thymeleaf.exception; public class UsernameNotFoundException extends RuntimeException{ public UsernameNotFoundException(String message){ super(message); } }
RESTFUL:
ExceptionController:
package us.vellum.representational.controller; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import us.vellum.representational.exception.IllegalNumberException; @RestController @RequestMapping(value = {"exception"}) public class ExceptionController{ @GetMapping public ResponseEntity<String> demo(){ int n = 1 / 0; return new ResponseEntity<>("demo ok", HttpStatus.ALREADY_REPORTED); } @GetMapping(value = "/{id}") public ResponseEntity<String> id(@PathVariable Integer id){ System.out.println("id = " + id); System.out.println("(id<0) = " + (id < 0)); if(id < 0) throw new IllegalNumberException(String.format("%05d invalid", id)); return new ResponseEntity<>(String.format("id: %08d", id), HttpStatus.GONE); } }
异常处理类:
package us.vellum.representational.exception; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class GlobalExceptionResolver{ @ExceptionHandler(value = Exception.class) // 用在方法, 处理指定异常, value指定异常类型 @ResponseBody public ResponseEntity<String> generalExceptionHandler(Exception ex){ System.out.println("ExceptionHandler"); if(ex instanceof RuntimeException){ System.out.println("ex instanceof RuntimeException"); return new ResponseEntity<>(ex.getLocalizedMessage(), HttpStatus.BAD_REQUEST); } return new ResponseEntity<>(ex.getMessage(), HttpStatus.GATEWAY_TIMEOUT); } @ExceptionHandler(value = IllegalNumberException.class) @ResponseBody public ResponseEntity<String> illegalNumberExceptionHandler(Exception ex){ System.out.println("IllegalNumberException"); return new ResponseEntity<>(ex.getLocalizedMessage(), HttpStatus.CONFLICT); } }
自定义异常类
package us.vellum.representational.exception; public class IllegalNumberException extends RuntimeException{ public IllegalNumberException(String message){ super(message); } }
package us.vellum.representational.exception; public class UsernameNotFoundException extends RuntimeException{ public UsernameNotFoundException(String message){ super(message); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律