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

     服务端日志

    知道、想到、做到、得到
  • 相关阅读:
    爬虫之暴力字典生成器
    爬虫之自动生成url
    数字、大小写字母的字符编码
    对avalonjs的研究
    求墙之间有多少水洼
    2.在centos7虚拟机搭建nginx网站
    P1250 种树
    暂时用笔记
    羊村的OI题解
    P1083 借教室
  • 原文地址:https://www.cnblogs.com/Durant0420/p/15073278.html
Copyright © 2011-2022 走看看