spring boot插入新数据带唯一键错误提示+统一处理返回值格式

1、新建返回类

package com.example.mytest.utils.aboutResult;

import lombok.Data;

@Data
public class ResultMap<T> {
    //错误码
    private Integer statusCode;

    //错误信息,一般为前端提示信息
    private String message;

    //返回值,一般为成功后返回的数据
    private T data;

    //错误详情,一般为失败后的详细原因,如空指针之类的
//    private String resultDetail;

    public ResultMap() {

    }

    public ResultMap(Integer statusCode, String message) {
        this.statusCode = statusCode;
        this.message = message;
    }

    public ResultMap(Integer statusCode, String message, T data) {
        this.statusCode = statusCode;
        this.message = message;
        this.data = data;
    }

    /**
     * 配合静态对象直接设置 data 参数
     */
    public ResultMap setNewData(T data) {
        ResultMap resultMap = new ResultMap();
        resultMap.setStatusCode(this.statusCode);
        resultMap.setMessage(this.message);
        resultMap.setData(data);
        return resultMap;
    }

    /**
     * 配合静态对象直接设置 errorMsg 参数
     */
    public ResultMap setNewErrorMsg(String message) {
        ResultMap resultMap = new ResultMap();
        resultMap.setStatusCode(this.statusCode);
        resultMap.setMessage(message);
        resultMap.setData(this.data);
        return resultMap;
    }

    public ResultMap setNewResult(Integer statusCode, String message, T data) {
        ResultMap resultMap = new ResultMap();
        resultMap.setStatusCode(statusCode);
        resultMap.setMessage(message);
        resultMap.setData(data);
        return resultMap;
    }

    public static final ResultMap SUCCESS = new ResultMap(1002, "ok");

    public static final ResultMap UNIQUE_PRIMARY_KEY = new ResultMap(1003, "唯一键异常");

    public static final ResultMap BUSINESS_ERROR=new ResultMap(1004, "");

    public static final ResultMap OTHER_SYSTEM_ERROR = new ResultMap(1005, "调用系统异常");

}

2、建立处理类

package com.example.mytest.utils.aboutResult;

import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class ResponseWebExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public ResultMap exceptionHandler(Exception e){
        log.error("",e);
        if (e instanceof DuplicateKeyException){
            return ResultMap.UNIQUE_PRIMARY_KEY.setNewErrorMsg("索引重复");
        }else if(e instanceof BusinessException){
            return ResultMap.BUSINESS_ERROR.setNewErrorMsg(e.getMessage());
        }else if(e instanceof MethodArgumentNotValidException){
            //参数验证捕捉
            BindingResult bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            StringBuilder errorMessage = new StringBuilder();
            for (FieldError fieldError : fieldErrors) {
                errorMessage.append(fieldError.getDefaultMessage()).append("; ");
            }
            return ResultMap.OTHER_SYSTEM_ERROR.setNewErrorMsg(errorMessage.toString());
        } else {
            return ResultMap.OTHER_SYSTEM_ERROR.setNewErrorMsg(e.toString());
        }
    }
}

3、自定义异常

public class BusinessException extends RuntimeException {
	public BusinessException(String msg){
		super(msg);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值