全局异常拦截

import com.alibaba.fastjson.JSONObject;

public class ErrorEntity {
	
	private String code = "";
	private String message = "";
	
	public ErrorEntity(String code, String message) {
		this.code = code;
		this.message = message;
	}
	
	public ErrorEntity(ErrorCode  errorCode){
		this.code = errorCode.getCode();
	}
	
	public ErrorEntity() {
		this(ErrorCode.UNKONW_ERROR);
	}

	public ErrorEntity(ErrorCode errorCode, String message) {
		this(errorCode);
		this.message = message;
	}
	
	public ErrorEntity(String message){
		this(ErrorCode.UNKONW_ERROR, message);
	}
	
	public ErrorEntity(ErrorCode errorCode, Object object){
		this(errorCode, object.toString());
	}
	
	public String getCode() {
		return code;
	}

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

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	@Override
	public String toString() {
		JSONObject object = new JSONObject();
		object.put("code", code);
		object.put("message", message);
		
		return object.toString();
	}
	
	

}

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Slf4j
public class ExceptionHandle {


    @ExceptionHandler(value = {Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Object handle(Exception e) {
        if (e instanceof ServiceException) {
            ServiceException myException = (ServiceException)e;
            return myException.getResult();
        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String url = request.getRequestURI();
        String remoteAddr = request.getRemoteAddr();
        String method = request.getMethod();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("请求url", url);
        jsonObject.put("请求方法", method);
        log.error("系统异常 {}",e);
        return ResultUtil.result(ErrorCode.UNKONW_ERROR.getCode(), ErrorCode.UNKONW_ERROR.getReson()+":"+e.toString()+" | "+jsonObject);
    }


}

import com.alibaba.fastjson.JSONObject;

public class ResultUtil {
    private static final String STATUS = "status";
    private static final String MESSAGE = "message";
    private static final String DATA = "data";
    private static final String CODE ="code";
    private static final String TOTAL="total";

    public static String result(final Integer status,final String msg,final Object data,final Integer total) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(STATUS, status);
        jsonObject.put(MESSAGE, msg);
        jsonObject.put(DATA, data);
        jsonObject.put(TOTAL, total);
        return jsonObject.toString();
    }

    public static String result(final Integer status,final String msg,final Object data) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(STATUS, status);
        jsonObject.put(MESSAGE, msg);
        jsonObject.put(DATA, data);
        return jsonObject.toString();
    }

    public static String result(final Integer status,final String msg) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(STATUS, status);
        jsonObject.put(MESSAGE, msg);
        return jsonObject.toString();
    }

    public static String result(final String code,final String msg) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(CODE, code);
        jsonObject.put(MESSAGE, msg);
        return jsonObject.toString();
    }

}
public enum ErrorCode {

	UNKONW_ERROR("00901001", "UNKNOW ERROR"),
	;

	private final String code;
	private final String reson;

	public String getCode() {
		return code;
	}
	public String getReson() {
		return reson;
	}
	private ErrorCode(String code, String reson) {
		this.code = code;
		this.reson = reson;
	}

}
public class ServiceException extends RuntimeException{
   
	private static final long serialVersionUID = -5911395097029374900L;

	/**
	 * 返回结果
	 */
	private Object result;

	public ServiceException(Object result) {
		this.result = result;
	}

	public Object getResult() {
		return result;
	}

}```

throw new ServiceException(ResultUtil.result(ErrorCode.UNKNOW ERROR.getCode(), “未知错误。”));


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值