zoukankan      html  css  js  c++  java
  • SpringBoot|自定义业务异常使用

    代码定义

    package com.example.demo.common;
    
    
    
    public class ServiceException extends RuntimeException {
    
        private static final long seriaVersionUID = 1L;
    
        private String message;
    
        @Override
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
    
        public ServiceException(final String message) {
            this.message = message;
        }
    
        public ServiceException(final String message, Throwable th) {
            super(message, th);
            this.message = message;
        }
    
        public static void throwEx(String message){
            throw new ServiceException(message);
        }
    
    }

    使用

        @ApiOperation("登录接口")
        @PostMapping("login")
        public String login(@RequestBody UserDto userDto){
            String result = userService.login(userDto);
            if(userDto.getName().contains("error2")){
                throw new ServiceException("error2",new NullPointerException());
            }
            if(userDto.getName().contains("error")){
                ServiceException.throwEx("用户名中含有error");
            }
            return "成功" + result;
        }

    一、postman发送error

    服务端日志

     二、postman发送error2

     服务端日志

    知道、想到、做到、得到
  • 相关阅读:
    time模块
    日期和时间
    异常和函数
    finally子句
    自定义异常
    异常中的else
    全捕捉
    排序算法---希尔排序
    排序算法---直接插入排序
    排序算法---快速排序
  • 原文地址:https://www.cnblogs.com/Durant0420/p/15073278.html
Copyright © 2011-2022 走看看