1.定义基础异常接口类
public interface BaseErrorInfoInterface {
2.定义错误处理枚举类
public enum ExceptionEnum implements BaseErrorInfoInterface{
BODY_NOT_MATCH("4000","请求的数据格式不符!"),
SIGNATURE_NOT_MATCH("4001","请求的数字签名不匹配!"),
NOT_FOUND("4004", "未找到该资源!"),
INTERNAL_SERVER_ERROR("5000", "服务器内部错误!"),
SERVER_BUSY("5003","服务器正忙,请稍后再试!");
private final String resultCode;
private final String resultMsg;
ExceptionEnum(String resultCode, String resultMsg) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
public String getResultCode() {
public String getResultMsg() {
3.自定义异常类
public class BizException extends RuntimeException{
private static final long serialVersionUID = 1L;
protected String errorCode;
protected String errorMsg;
public BizException(BaseErrorInfoInterface errorInfoInterface) {
super(errorInfoInterface.getResultCode());
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
super(errorInfoInterface.getResultCode(), cause);
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
public BizException(String errorMsg) {
this.errorMsg = errorMsg;
public BizException(String errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
public BizException(String errorCode, String errorMsg, Throwable cause) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
public String getErrorCode() {
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
public String getErrorMsg() {
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
public Throwable fillInStackTrace() {

4.自定义响应体类
public class ResultResponse {
public ResultResponse() {
public ResultResponse(BaseErrorInfoInterface errorInfo) {
this.code = errorInfo.getResultCode();
this.message = errorInfo.getResultMsg();
public String getCode() {
public void setCode(String code) {
public String getMessage() {
public void setMessage(String message) {
public Object getResult() {
public void setResult(Object result) {
public static ResultResponse success() {
public static ResultResponse success(Object data) {
ResultResponse rb = new ResultResponse();
rb.setCode(ExceptionEnum.SUCCESS.getResultCode());
rb.setMessage(ExceptionEnum.SUCCESS.getResultMsg());
public static ResultResponse error(BaseErrorInfoInterface errorInfo) {
ResultResponse rb = new ResultResponse();
rb.setCode(errorInfo.getResultCode());
rb.setMessage(errorInfo.getResultMsg());
public static ResultResponse error(String code, String message) {
ResultResponse rb = new ResultResponse();
public static ResultResponse error( String message) {
ResultResponse rb = new ResultResponse();
public String toString() {
return JSONObject.toJSONString(this);

5.自定义全局异常处理
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = BizException.class)
public ResultResponse bizExceptionHandler(HttpServletRequest req, BizException e){
logger.error("发生业务异常!原因是:{}",e.getErrorMsg());
return ResultResponse.error(e.getErrorCode(),e.getErrorMsg());
@ExceptionHandler(value =NullPointerException.class)
public ResultResponse exceptionHandler(HttpServletRequest req, NullPointerException e){
logger.error("发生空指针异常!原因是:",e);
return ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH);
@ExceptionHandler(value =Exception.class)
public ResultResponse exceptionHandler(HttpServletRequest req, Exception e){
logger.error("未知异常!原因是:",e);
return ResultResponse.error(ExceptionEnum.INTERNAL_SERVER_ERROR);
6.测试代码
public boolean add(@RequestBody User user) {
if(user.getName()==null){
throw new BizException("-1","用户姓名不能为空!");
public boolean update(@RequestBody User user) {
@DeleteMapping("/delete")
public boolean delete(@RequestBody User user) {
Integer.parseInt("abc123");
@ExceptionHandler(value = NumberFormatException.class)
public ResultResponse exceptionHandler(HttpServletRequest req, NumberFormatException e){
logger.error("发生类型转换异常!原因是:",e);
return ResultResponse.error(ExceptionEnum.PARAMS_NOT_CONVERT);
7.测试结果


