![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/06a660132f06d93482fd3b0e1e818284.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c28c628a3bfb2b2e1702bf95b7d31fe7.png)
- OEException.java
public class OEException extends RuntimeException{//注:spring 对于 RuntimeException 异常才会进行事务回滚。
private static final long serialVersionUID = 1L;
private Integer code;
private String message;
/**
* 有参构造器(异常信息封装)
* @param resultEnum:异常信息体
*/
public OEException(ResultEnum resultEnum) {
this.message = resultEnum.getMessage();
this.code = resultEnum.getCode();
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
- OEExceptionHandler.java
@ControllerAdvice //注解将作用在 被 @RequestMapping 注解的方法上,捕获方法抛出的自定义异常
public class OEExceptionHandler {
@ExceptionHandler(value = OEException.class)
@ResponseBody
public ResponseMessage handlerSellerException(OEException e){
//返回异常信息到前端
return ResponseMessageUtil.info(e.getCode(),e.getMessage());
//可以自定义错误页面,并返回页面
}
}
- ResponseMessage.java
public class ResponseMessage implements Serializable{
private static final long serialVersionUID = 1L;
private Object data;//数据
private String message;//信息
private Integer code;//代码:如200、400、404
public ResponseMessage(Object data, String message, Integer code) {
super();
this.data = data;
this.message = message;
this.code = code;
}
public ResponseMessage() {
super();
// TODO Auto-generated constructor stub
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
- ResponseMessageUtil.java
public class ResponseMessageUtil {
/**
* 发送应答信息
* @param code
* @param msg
* @return
*/
public static ResponseMessage info(Integer code,String msg) {
ResponseMessage responseMessage = new ResponseMessage();
responseMessage.setCode(code);//设置返回的代码
responseMessage.setMessage(msg);//设置返回的信息
return responseMessage;
}
}
- ResultEnum.java
public enum ResultEnum {
SUCCESS(0,"成功"),
PARAM_ERROR(1,"参数不正确"),
PARAM_IS_EMPTY(2,"参数为空"),
ADD_ERROR(11,"新增失败"),
DELETE_ERROR(12,"删除失败"),
UPDATE_ERROR(13,"更新失败"),
DATA_NOT_EXIST(14,"数据不存在"),
PAPER_FORM_ERROR(21,"试卷生成失败"),
PAPER_PUBLISH_ERROR(22,"试卷发布失败"),
PAPER_ALREAD_SUBMIT(31,"试卷已提交"),
PAPER_TIMEOUT(32,"试卷已过期"),
PAPER_DISTRIBUTE_ERROR(33,"未分配试卷"),
;
private Integer code;
private String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
-
抛出自定义异常
throw new OEException(ResultEnum.DATA_NOT_EXIST);