1.在业务成层中写一个ex包,后再写ServiceException的类去继承 RuntimeException
package com.cy.service.ex;
public class ServiceException extends RuntimeException{
public ServiceException() {
super();
}
public ServiceException(String message) {
super(message);
}
public ServiceException(String message, Throwable cause) {
super(message, cause);
}
public ServiceException(Throwable cause) {
super(cause);
}
protected ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
而后可以在写多个异常类去继承ServiceException
2.写一个util工具包进行前端数据的响应
package com.cy.util;
import java.io.Serializable;
/**
* 响应结果类
* @param <E> 响应数据的类型
*/
public class ResponseResult<E> implements Serializable {
/** 操作成功的状态码 */
public static final int OK = 200;
/** 状态码 */
private Integer state;
/** 状态描述信息 */
private String message;
/** 数据 */
private E data;
public ResponseResult() {
}
public ResponseResult(Integer state, String message) {
this.state = state;
this.message = message;
}
public ResponseResult(Integer state, String message, E data) {
this.state = state;
this.message = message;
this.data = data;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public E getData() {
return data;
}
public void setData(E data) {
this.data = data;
}
public static <E> ResponseResult<E> getResponseResult() {
return new ResponseResult<>(OK, null, null);
}
public static <E> ResponseResult<E> getResponseResult(Integer state) {
return new ResponseResult<>(state, null, null);
}
public static <E> ResponseResult<E> getResponseResult(String message) {
return new ResponseResult<>(OK, message, null);
}
public static <E> ResponseResult<E> getResponseResult(E data) {
return new ResponseResult<>(OK, null, data);
}
public static <E> ResponseResult<E> getResponseResult(Integer state, String message) {
return new ResponseResult<>(state, message, null);
}
public static <E> ResponseResult<E> getResponseResult(String message, E data) {
return new ResponseResult<>(OK, message, data);
}
}
3.写一个异常处理类
package com.cy.controller;
import com.cy.service.ex.InsertException;
import com.cy.service.ex.ServiceException;
import com.cy.service.ex.UsernameDuplicateException;
import com.cy.util.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/*
* 控制层的基类
* */
@ControllerAdvice
public class BaseController extends zhuang{
/* 统一异常处理 */
@ExceptionHandler(ServiceException.class)//监控当前类是否产生异常
public ResponseResult<Void> handleException(Throwable e){
ResponseResult<Void> responseResult=null;
if (e instanceof UsernameDuplicateException){
responseResult = ResponseResult.getResponseResult(zhuan1, e.getMessage());
}
if (e instanceof InsertException){
responseResult = ResponseResult.getResponseResult(5000, e.getMessage());
}
return responseResult;
}
}