Springboot通用异常处理

一.编写异常枚举类里面解写自己的异常

package com.yukkso.exception;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@Getter
@NoArgsConstructor
public enum ExceptionEnum {
    PRICE_CANNOT_NULL(400,"价格不能为空"),
    COMMODITY_CANNOT_FIND(404,"商品找不到"),
    ID_CANNOT_FIND(404,"商品找不到"),
    ;

    private Integer code;
    private String msg;


}

二.自己写一个通用的异常类,记住一定要继承一个类

package com.yukkso.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class LtypException extends RuntimeException {
    private ExceptionEnum exceptionEnum;

}

三.编写异常结果处理

package com.yukkso.exception;

public class ExceptionResult {
    private Integer status;
    private String message;

    public ExceptionResult(ExceptionEnum e){
        this.status=e.getCode();
        this.message=e.getMsg();
    }
}

四.编写异常通知处理

package com.yukkso.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(LtypException.class)
    public ResponseEntity<ExceptionResult> responseEntity(LtypException e){
        return ResponseEntity.status(e.getExceptionEnum().getCode())
                .body(new ExceptionResult(e.getExceptionEnum()));
    }
}

五.在自己代码中使用

 @GetMapping("/get")
    public String selectAllUser(HttpServletRequest request) {
        try {
            System.out.println("URI-----"+request.getRequestURI());
            System.out.println("URL-------"+request.getRequestURL().toString());
            System.out.println("协议-------"+request.getScheme());
            System.out.println("域名-------"+request.getServerName());
            System.out.println("项目名-------"+request.getContextPath());
            System.out.println("请求路径-------"+request.getServletPath());
            System.out.println("端口-------"+request.getServerPort());
            System.out.println(System.getProperty("user.dir")+File.separator+request.getContextPath());
            ApplicationHome applicationHome=new ApplicationHome(this.getClass());
            System.out.println();

            System.out.println(System.getProperty("user.dir"));
            if (!StringUtils.isEmpty(System.getProperty("user.dir"))){
                //这里为我自己编写的异常
                throw new LtypException(ExceptionEnum.COMMODITY_CANNOT_FIND);
            }
            System.out.println( this.getClass().getResource("/").getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值