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(), “未知错误。”));