首页 > 其他分享 >自定义异常和统一校验参数

自定义异常和统一校验参数

时间:2023-06-19 09:02:29浏览次数:55  
标签:BusinessException String 自定义 object 校验 参数 message public

自定义异常

@Getter
public class BusinessException extends RuntimeException {
    /**
     * http状态码
     */
    private int code;

    private Object object;

    public BusinessException(String message, int code, Object object) {
        super(message);
        this.code = code;
        this.object = object;
    }

    public BusinessException(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.object = resultCode.getMessage();
    }

    public BusinessException(ResultCode resultCode, String message) {
        this.code = resultCode.getCode();
        this.object = message;
    }

    public BusinessException(String message) {
        super(message);
    }
}

一些常见需要对参数进校验,项目中自定义了一个校验工具类ParamValidate

public class ParamValidate {

    public static void isNull(Object object, String message) {
        if (object == null) {
            throw new BusinessException(ResultCode.PARAMETER_EMPTY, message);
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new BusinessException(ResultCode.PARAMETER_ERROR, message);
        }
    }

    public static void notEmpty(Collection collection, String message) {
        isNull(collection, message);
        if (collection.size() == 0) {
            throw new BusinessException(ResultCode.PARAMETER_ERROR, message);
        }
    }
}

工具使用:

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private MessageTemplateSingleton messageTemplateSingleton;

    @GetMapping("/index1")
    public CommonResult<MessageTemplateSingleton.Template> index() {
        ParamValidate.isNull(null, "参数为空");
        MessageTemplateSingleton.Template template = messageTemplateSingleton.getTemplate(1);
        return CommonResult.success(template);
    }
}

自定义异常统一处理:

@RestControllerAdvice
@Slf4j
public class ChargeStationAdvice {

    @ExceptionHandler(Exception.class)
    public CommonResult<String> doException(Exception e) {

        log.error("统一处理自定义异常机制,触发异常 msg ", e);
        String message = null;
        int errorCode = ResultCode.FAILED.getCode();
        //自定义异常
        if (e instanceof BusinessException) {
            BusinessException exception = (BusinessException) e;
            message = exception.getMessage();
            errorCode = exception.getCode();
        } else if (e instanceof HttpRequestMethodNotSupportedException) {
            message = "不支持GET/POST方法";
        } else if (e instanceof NoHandlerFoundException) {
            message = "请求接口不存在";
        }  else{
             message = "系统异常";
        }
        return CommonResult.failed(errorCode, message);
    }
}

 

标签:BusinessException,String,自定义,object,校验,参数,message,public
From: https://www.cnblogs.com/privateLogs/p/17490219.html

相关文章

  • 关于python反射机制中的参数问题处理
    关于python反射机制中的参数问题处理 python的反射机制十分的常用,主要是字符串与模块应用之间的连接方法。核心是将字符串转换成可以调用模块、模块方法的变量。主要包括了以下四个方法:hasattr(obj,name,/)Returnwhethertheobjecthasanattributewi......
  • Hexo + Butterfly 自定义页脚
    原文链接:Hexo+Butterfly自定义页脚推荐阅读基于Hexo从零开始搭建个人博客(一):环境准备基于Hexo从零开始搭建个人博客(二):项目初识基于Hexo从零开始搭建个人博客(三):主题安装基于Hexo从零开始搭建个人博客(四):基础配置基于Hexo从零开始搭建个人博客(五):详细......
  • backtrader 自定义分析器,解决多股回测难分析困难问题
    backtrader自定义分析器,解决多股回测分析困难问题解决了啥:解决回测后获取关键指标解决多股回测,获取订单分析解决多股回测买卖点可视化标识效果图通过自定义分析器KeyIndicatorAnalyzer,TradeListAnalyzer,获取回测结果数据,通过回测数据可以轻松可视化回测结果。可视化部......
  • ChatGLM项目启动选项参数
    项目启动选项usage:langchina-ChatGLM[-h][--no-remote-model][--modelMODEL][--loraLORA][--model-dirMODEL_DIR][--lora-dirLORA_DIR][--cpu][--auto-devices][--gpu-memoryGPU_MEMORY[GPU_MEMORY...]][--cpu-memoryCPU_MEMORY][--load-in-8bit][--bf16]......
  • centos添加自定义Systemd服务
    #########################https://zhuanlan.zhihu.com/p/415469149          systemctlenable**nable命令相当于在/etc/systemd/system/目录里添加了一个符号链接,指向/usr/lib/systemd/system/里面的**.service开机时,Systemd会执行/etc/systemd/system......
  • 循环码的编码、译码与循环冗余校验
    本专栏包含信息论与编码的核心知识,按知识点组织,可作为教学或学习的参考。markdown版本已归档至【Github仓库:<https://github.com/timerring/information-theory>】或者【AIShareLab】回复信息论获取。循环码的编码循环码编码用硬件实现时,可用除法电路来实现。除法电路主要是......
  • springboot中自定义注解在service方法中,aop失效
    问题描述写了个自定义注解,但是该注解只会出现在serviece层中的方法中。启动发现aop未拦截到问题原因:调用service中的xx()方法时,Spring的动态代理帮我们动态生成了一个代理的对象,暂且叫他$XxxxService。所以调用xx()方法实际上是代理对象$XxxxService调用的。但是在xx()方法内调用同......
  • RoundingMode 几个参数详解
    第一版java.math.RoundingMode几个参数详解java.math.RoundingMode里面有几个参数搞得我有点晕,现以个人理解对其一一进行总结:为了能更好理解,我们可以画一个XY轴RoundingMode.CEILING:取右边最近的整数RoundingMode.DOWN:去掉小数部分取整,也就是正数取左边,负数取右边,相当于向原点靠近......
  • chrome浏览器Bing主页自定义
    前言在使用Bing主页的时候,无法将主页的新闻彻底隐藏,也无法更换背景图片(如下图)。这就很难受了,我只想要一个只有搜索框加背景图片的一个主页。在集百家之所长后,得到一个符合个人要求的主页。主要文件manifest.json1{2"chrome_url_overrides":{3"newtab":......
  • Go 语言之自定义 zap 日志
    Go语言之自定义zap日志zap日志:https://github.com/uber-go/zap一、日志写入文件zap.NewProduction、zap.NewDevelopment是预设配置好的。zap.New可自定义配置zap.New源码这是构造Logger最灵活的方式,但也是最冗长的方式。对于典型的用例,高度固执己见的预设(NewProdu......