Java Exception统一处理

1.自定义Exception

package com.justin.exception;

/**
 * @ClassName JustinException
 * @Desc 自定义系统异常
 * @Author justin.Sun
 * @Date 2019/5/5 9:12
 **/
public class JustinException extends RuntimeException {
    public JustinException() {
    }

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

    public JustinException(String message, Throwable cause) {
        super(message, cause);
    }

    public JustinException(Throwable cause) {
        super(cause);
    }

    public JustinException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

2.定义系统请求状态返回码定义类

package com.justin.exception;

/**
 * @ClassName SystemResponseStatus
 * @Desc 系统Http请求状态返回码定义类
 * @Author justin.Sun
 * @Date 2019/5/5 9:32
 **/
public class SystemResponseStatus {

    public final static String SUCCESS="000";
    public final static String ERROR="999";
}

3.定义Response

package com.justin.exception;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * @ClassName BaseResponse
 * @Desc 系统Http返回定义类
 * @Author justin.Sun
 * @Date 2019/5/5 9:23
 **/
public class BaseResponse<T> implements Serializable {

    private String code;
    private String msg;
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss",timezone = "GMT+8")
    private Date respDateTime;
    private T Data;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public java.util.Date getRespDateTime() {
        return respDateTime;
    }

    public void setRespDateTime(java.util.Date respDateTime) {
        this.respDateTime = respDateTime;
    }

    public T getData() {
        return Data;
    }

    public void setData(T data) {
        Data = data;
    }

    public static <T> BaseResponse<T> create(String code, String msg, T data){
        BaseResponse<T> response=new BaseResponse<T>();
        response.setCode(code);
        response.setMsg(msg);
        response.setRespDateTime(new Date());
        response.setData(data);
        return response;
    }

    public static <T> BaseResponse<T> success(String msg,T data) {
        return BaseResponse.create(SystemResponseStatus.SUCCESS, msg, data);
    }

    public static <T> BaseResponse<T> success(T data){
        return BaseResponse.success("success",data);
    }

    public static BaseResponse success(String msg){
        return BaseResponse.success(msg,null);
    }

    public static BaseResponse success(){
        return BaseResponse.success("success",null);
    }

    public static <T> BaseResponse<T> error(String msg,T data) {
        return BaseResponse.create(SystemResponseStatus.ERROR, msg, data);
    }

    public static <T> BaseResponse<T> error(T data){
        return BaseResponse.success("error",data);
    }

    public static BaseResponse error(String msg){
        return BaseResponse.success(msg,null);
    }

    public static BaseResponse error(){
        return BaseResponse.success("error",null);
    }
}

4.定义Exception配置读取类并添加配置

package com.justin.exception;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @ClassName ExceptionProperties
 * @Desc 系统异常配置读取类
 * @Author justin.Sun
 * @Date 2019/5/5 10:06
 **/
@Component
@ConfigurationProperties("app.exception")
public class ExceptionProperties {

    //是否开启日志处理
    private boolean autolog=false;

    public boolean isAutolog() {
        return autolog;
    }

    public void setAutolog(boolean autolog) {
        this.autolog = autolog;
    }
}

//添加配置
app:
  exception:
    autolog: true

5.定义Exception捕获

package com.justin.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName DefalutExceptionHandler
 * @Desc 系统RestController异常捕获处理类
 * @Author justin.Sun
 * @Date 2019/5/5 9:15
 **/
@RestControllerAdvice//注意此处为RestController,如Controller采用@Controller请换用@ControllerAdvice并配置扫描区域
public class DefalutExceptionHandler {
    private final Logger logger= LoggerFactory.getLogger(DefalutExceptionHandler.class);

    @Autowired
    private ExceptionProperties properties;

    @ExceptionHandler
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse handler(Exception e){
        if (!properties.isAutolog()){
            return BaseResponse.error();
        }
        if (e instanceof JustinException){
            return BaseResponse.error(e.getMessage());
        }else{
            logger.error("系统异常",e);
            return BaseResponse.error("网络繁忙");
        }
    }
}

发表回复

您的电子邮箱地址不会被公开。

3 × 4 =