spring异常+自定义以及使用
1、首先自定义异常
DataException:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wbg.maven1128.exception; public class DataException extends Exception { public DataException(){ } public DataException(String msg){ super(msg); } }
DbException:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wbg.maven1128.exception; public class DbException extends Exception { public DbException(){ } public DbException(String msg){ super(msg); } }
2、在dao层把异常抛了
在服务层做好
不使用注解方法
在控制层使用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Controller @RequestMapping("/order") public class OrdersController { @Autowired private OrdersService ordersService; @PostMapping(produces = "application/json;charset=utf-8") @ResponseBody public String add(Orders orders ){ System.out.println(orders); try { ordersService.insert(orders); }catch(DbException e){ return e.getMessage(); } catch (DataException e) { return e.getMessage(); } return "成功"; } @GetMapping public String index(){ return "order_add"; }
1、自定义AbstractHandlerExceptionResolver
自定义就是当遇到AbstractHandlerExceptionResolver异常的时候直接进入自定义的异常,不再进入AbstractHandlerExceptionResolver
第一步:创建一个类来进行继承AbstractHandlerExceptionResolver类
MyExceptionResolver :
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wbg.maven1128.exception; import org.springframework.beans.TypeMismatchException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyExceptionResolver extends AbstractHandlerExceptionResolver { @Override protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { if(ex instanceof DbException){ return new ModelAndView("数据库错误"); }else if(ex instanceof TypeMismatchException){ return new ModelAndView("TypeMismatchException"); } return null; } }
第二步:进行在xml注册
<bean class="com.wbg.maven1128.exception.MyExceptionResolver"/>
最后直接在方法体上抛出异常即可throws Exception
2、使用ExceptionHandler注解方式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Controller @RequestMapping("/order") public class OrdersController { @Autowired private OrdersService ordersService; @PostMapping(produces = "application/json;charset=utf-8") @ResponseBody public String add(Orders orders ) throws Exception { ordersService.insert(orders); return "成功"; } @GetMapping public String index(){ return "order_add"; } @ExceptionHandler(DbException.class) public String handlerDbException(Exception ex, Model model){ model.addAttribute("err",ex.getMessage()); return "err1"; } @ExceptionHandler(DataException.class) public ModelAndView handlerDbException2(Exception ex, Model model){ model.addAttribute("err",ex.getMessage()); return new ModelAndView("err2"); }
3、使用@ControllerAdvice注解(推荐)
1、首先创建一个类,在这个类上加注解
2、在该类中写方法,方法上使用@ExceptionHandler注解进行捕获异常
3、然后在Controller层直接抛出异常、会自动捕获
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wbg.maven1128.controller; import com.wbg.maven1128.exception.DbException; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; @Component @ControllerAdvice public class AllControllerAdvice { // 这个处理,对所有的 Controller 都会有效 @ExceptionHandler(Exception.class) @ResponseStatus(value = HttpStatus.URI_TOO_LONG) @ResponseBody public String handlerDbException3 () { return "ksjfksd"; } @ExceptionHandler(DbException.class) @ResponseStatus(value = HttpStatus.URI_TOO_LONG) @ResponseBody public String DbException3 () { return "ksjfksd"; } @ExceptionHandler(RuntimeException.class) public String handlerDbException4 () { return "runtime"; } }
测试: