首页 > 编程语言 > 详细

SpringBoot|自定义业务异常使用

时间:2021-07-29 10:41:16      阅读:22      评论:0      收藏:0      [点我收藏+]

代码定义

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

技术分享图片

 

 服务端日志

技术分享图片

 

SpringBoot|自定义业务异常使用

原文:https://www.cnblogs.com/Durant0420/p/15073278.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!