全局异常处理

GlobalExceptionHandler.java

package com.haizhi.baixin.lgscreen.controller;

import com.haizhi.baixin.lgscreen.common.exception.BusiException;
import com.haizhi.baixin.lgscreen.common.model.Response;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

/**
 * @date 2020/6/28 21:49
 * @description
 **/
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusiException.class)
    public Response exception(BusiException ex){
        return Response.fail(ex.getBusiEnum().getCode(),ex.getMessage());
    }

    @ExceptionHandler(Throwable.class)
    public Response exception(Throwable ex){
        return Response.error(ex.getMessage());
    }
}

Response.java

package com.haizhi.baixin.lgscreen.common.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @date 2020/6/28 21:33
 * @description 返回结果
 **/
@Data
@ApiModel("返回结果")
public class Response<T> {

    @ApiModelProperty(value = "返回状态",example = "100:成功,-100:错误")
    private Integer code;

    @ApiModelProperty("错误信息")
    private String message;

    @ApiModelProperty("返回数据")
    private T data;

    private Response(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Response success(){
        return success(null);
    }

    public static <T> Response success(T data){
        return new Response(Constant.SUCCESS,"success",data);
    }

    public static Response fail(Integer code, String message){
        return new Response(code,message,null);
    }

    public static Response error(String message){
        return new Response(Constant.ERROR,message,null);
    }

}

BusiException.java

package com.haizhi.baixin.lgscreen.common.exception;

/**
 * @date 2020/6/28 21:51
 * @description
 **/
public class BusiException extends RuntimeException{

    private BusiEnum busiEnum;

    public BusiException(BusiEnum busiEnum) {
        super(busiEnum.getMessage());
        this.busiEnum = busiEnum;
    }

    public BusiEnum getBusiEnum() {
        return busiEnum;
    }

    public void setBusiEnum(BusiEnum busiEnum) {
        this.busiEnum = busiEnum;
    }
}

BusiEnum.java

package com.haizhi.baixin.lgscreen.common.exception;

/**
 * @date 2020/6/28 21:52
 * @description
 **/
public enum BusiEnum {

    SUCCESS(100,"成功"),
    ERROR(-100,"系统错误");

    private Integer code;
    private String message;

    BusiEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

 Constant.java

package com.haizhi.baixin.lgscreen.common.model;

/**
 * @date 2020/6/28 21:36
 * @description
 **/
public class Constant {
    public static final Integer SUCCESS = 100;

    public static final Integer ERROR = -100;

    public static final String DISTRIBUTION_GR = "gr"; //分布情况Flag-个人

    public static final String DISTRIBUTION_XW = "xw"; //分布情况Flag-小微企业

    public static final String RISKALLTYPE_Y = "y"; //累计调用策略统计方式-当年

    public static final String RISKALLTYPE_H = "h"; //累计调用策略统计方式-当天

    public static final Integer MAP_DEFAULT_COUNT = 20; //地图默认查询多少条

    public static final Integer SMART_DEFAULT_COUNT = 200; //地图默认查询多少条

    public static final String RISK_GRAPH_TYPE_CITY = "city"; //风险画像类型-城市

    public static final String RISK_GRAPH_TYPE_DEVICE = "device"; //风险画像类型-设备

    public static final String RISK_GRAPH_TYPE_RISKTYPE = "risktype"; //风险画像类型-风险类型
}

 

posted @ 2020-07-17 16:52  习惯沉淀  阅读(508)  评论(0编辑  收藏  举报