> 文章列表 > ssm异常处理

ssm异常处理

ssm异常处理

ssm异常处理

类上和方法上都要有注解:

类上的注解: 异常处理用到的注解,里面包含了其他的一些必须的注解,详解看下图
ssm异常处理
方法上的注解:
ssm异常处理

上面的要懂打配合
现在创建一个处理异常的工具类,加上前面提到的注解,将所有的异常拦截下来,统一处理:注意下面的注释,详细讲述了传进来参数等等都是怎样的,这里的返回值别和控制类的返回值搞混了,两者没有关系

package com.itjh.contorller;import com.itjh.excption.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;//这个注解自动包含了@ResponseBody和@Component
@RestControllerAdvice
public class Throw {//抓住哪种异常类型@ExceptionHandler(Exception.class)//捕获的是传过来的异常,比如serv类中抛出的异常//所以传过来的,和这里收到的是同一个异常的对象public Result excption(Exception e){//打印在控制台上,我自己要看的System.out.println("异常发生了,吃俺老孙一棍");//这里的返回值是返回给前端的return new Result(Code.GET_NO,null,"呔!异常妖精哪里跑!!!");}
}

控制类:下面的应该是算数异常,所以属于Exception异常,刚好和上面代码中的方法的异常参数相对应,也可以说上面方法中的参数就是下面这个算数异常

package com.itjh.contorller;import com.itjh.excption.SystemException;
import com.itjh.pojo.Book;
import com.itjh.service.ServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;@RestController
@RequestMapping("users")
public class serv {@Autowiredprivate ServiceImp serviceImp;@GetMapping()//将返回值自动变成了json格式:集合中为// pojo类的json格式就是  键名:[{a:b],{c:d}]public Result getAll() throws SystemException{int i=1/0;List<Book> books=serviceImp.selectAll();Integer code=books!=null?Code.GET_OK:Code.GET_NO;String mes=books!=null?"小飞棍来喽":"出错啦,宝";//如果没有发生异常就会将数据返回给前端return new Result(code,books,mes);}}

异常所属的类型有很多:

所以自己给他分个类,分为业务层很多种:
ssm异常处理
相对应的解救方法:
ssm异常处理
先摆出下面代码需要的回应数值:

package com.itjh.contorller;public class Code {//未发生异常时,正常操作的成功与否的回应值public static final Integer SAVE_OK=42021;public static final Integer DELETE_OK=42023;public static final Integer GET_OK=42025;public static final Integer GETBYID_OK=42027;public static final Integer UPDATE_OK=42029;public static final Integer SAVE_NO=42020;public static final Integer DELETE_ONO=42022;public static final Integer GET_NO=42024;public static final Integer GETBYID_NO=42026;public static final Integer UPDATE_NO=42028;//自定义的不同异常所需要的回应值public static final Integer bUSINESS_OK=5001;public static final Integer SYSTEM_OK=5003;public static final Integer SYSTEM_NO=5000;public static final Integer bUSINESS_NO=5002;
}

根据上面第二张图大致可以分析出来三种自定义类型的异常,于是创建自定义异常:令其继承Throwable即可:三个自定义异常

业务异常:

package com.itjh.excption;public class BusinessExcption extends Throwable{Integer code;public BusinessExcption(Integer code,String message) {super(message);this.code=code;}public BusinessExcption(Integer code,String message, Throwable cause) {super(message, cause);this.code=code;}
}

系统异常:

package com.itjh.excption;public class SystemException extends RuntimeException{Integer code;public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public SystemException(Integer code , String message) {super(message);this.code=code;}public SystemException(Integer code, String message, Throwable cause) {super(message, cause);this.code=code;}
}

之后,在控制类再抛出异常的时候就可以按照规矩,抛上面这几种异常:

  • 这里的例子,抛异常SystemException里面的参数是codemessagecode是在自定义异常SystemException两个参数的构造函数中传了的,于是在异常处理工具类中的返回值的位置,在里面参数就是直接通过getCode()方法获得参数code了,而message会发现没有getMessage()方法,从而在异常处理工具类中无法获得message值,但是其实和code一样通过构造含数传递的,只是没有定义getMessage()方法,但是异常的父类Throwable中有这个get()方法,我把参数已经通过构造函数传上去了,限制只需要调用其父类的getMessage()方法即可,因为继承的关系,子类可以调用父类独有的东西
  • 下面两个代码就是控制类传参,和,异常处理工具类:
package com.itjh.contorller;import com.itjh.excption.SystemException;
import com.itjh.pojo.Book;
import com.itjh.service.ServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;@RestController
@RequestMapping("users")
public class serv {@Autowiredprivate ServiceImp serviceImp;@GetMapping()//将返回值自动变成了json格式:集合中为// pojo类的json格式就是  键名:[{a:b],{c:d}]public Result getAll() throws SystemException{try{int i=1/0;}//当前异常是哪种异常,这里除以0,自然是算数异常catch (ArithmeticException excption){//知道发生了异常,就做一些补救措施//脑子里判断出她应该属于系统类错误,//所以选择对应的自定义异常throw new SystemException(Code.GET_NO,"出错啦!!!");}List<Book> books=serviceImp.selectAll();Integer code=books!=null?Code.GET_OK:Code.GET_NO;String mes=books!=null?"小飞棍来喽":"出错啦,宝";return new Result(code,books,mes);}@DeleteMapping("/{id}")public Result delect(@PathVariable int id) throws BusinessException {//传进来的如果id为1则抛出异常,//让异常处理工具类抓住他if(id==1){System.out.println("要抛出业务异常喽!!!");throw new BusinessException(Code.bUSINESS_NO,"输入的id有误,请重新输入!!!");}boolean flag=serviceImp.delete(id);String mes=flag==true?"成功删除了了呢!!":"搞错啦";Integer code=flag==true?Code.UPDATE_OK:Code.UPDATE_NO;return new Result(code,flag,mes);}
}

然后再看拦截异常的工具类:

里面方法的参数就是:控制类抛出的异常,被拦截下来后,放进了方法的参数中。根据异常的不同应当对应的方法有三种才对:这里就写了两种看看就行了

package com.itjh.contorller;import com.itjh.excption.BusinessException;
import com.itjh.excption.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;//这个注解自动包含了@ResponseBody和@Component
@RestControllerAdvice
public class Throw {//抓住哪种异常类型@ExceptionHandler(SystemException.class)//捕获的是传过来的异常,比如serv类中抛出的异常//参数就是控制类传过来的异常参数public Result systemExcption(SystemException e){//打印在控制台上,我自己要看的System.out.println("异常发生了,吃俺老孙一棍");//作为json格式返回给前端return new Result(e.getCode(),null, e.getMessage());}//抓住哪种异常类型@ExceptionHandler(BusinessException.class)//捕获的是传过来的异常,比如serv类中抛出的异常//参数就是控制类传过来的异常参数public Result businessExcption(BusinessException e){//打印在控制台上,我自己要看的System.out.println("异常发生了,吃俺老孙一棍");//作为json格式返回给前端return new Result(e.getCode(),null,e.getMessage());}
}