1.项目结构

2. exceptionCodeMsg.properties 配置文件
-1=系统异常,请稍后再试!
0=OK
200=SUCCESS
404=请求未找到,请检查请求地址是否正确
500=服务器异常
3. ExceptionCodeMsgUtil 配置文件工具类
package com.xiaoye.utils;
import java.io.IOException;
import java.util.Properties;
public class ExceptionCodeMsgUtil {
private static Properties prop = null;
static {
prop = new Properties();
try {
prop.load(ExceptionCodeMsgUtil.class.getClassLoader().getResourceAsStream("properties/exceptionCodeMsg.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key) {
return prop.getProperty(key);
}
}
4. ExceptionCodeMsg 错误码/信息实体
package com.xiaoye.common;
import com.xiaoye.utils.ExceptionCodeMsgUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
@Getter
public enum ExceptionCodeMsg {
ERROR(-1),
OK(0),
SUCCESS(200),
ERROR_404(404),
ERROR_500(500);
private Integer code;
private String msg;
private ExceptionCodeMsg(Integer code) {
this.code = code;
}
public String getMsg() {
return ExceptionCodeMsgUtil.getValue(this.code + "");
}
}
5. ResponseEntity 返回实体
package com.xiaoye.common;
import com.xiaoye.exception.IServiceException;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import java.io.Serializable;
import java.util.Date;
@Data
@AllArgsConstructor
public class ResponseEntity<T extends Serializable> implements Serializable {
private static final Logger LOGGER = LoggerFactory.getLogger(ResponseEntity.class);
private Integer code;
private String msg;
private T data;
private Boolean success;
private Long date;
private Object exception;
private ResponseEntity() {
date = new Date().getTime();
}
public static <T extends Serializable> ResponseEntity<T> IS_SUCCESS(T data) {
ResponseEntity<T> responseEntity = new ResponseEntity<>();
responseEntity.setCode(ExceptionCodeMsg.SUCCESS.getCode());
responseEntity.setMsg(ExceptionCodeMsg.SUCCESS.getMsg());
responseEntity.setSuccess(true);
responseEntity.setData(data);
return responseEntity;
}
public static <T extends Serializable> ResponseEntity<T> IS_ERROR(ExceptionCodeMsg exceptionCodeMsg, Exception exception) {
ResponseEntity<T> responseEntity = new ResponseEntity<>();
responseEntity.setCode(exceptionCodeMsg.getCode());
responseEntity.setMsg(exceptionCodeMsg.getMsg());
responseEntity.setSuccess(false);
responseEntity.setException(exception.getClass().getName());
return responseEntity;
}
public static <T extends Serializable> ResponseEntity<T> IS_ERROR(IServiceException iServiceException) {
ResponseEntity<T> responseEntity = new ResponseEntity<>();
responseEntity.setCode(iServiceException.getExceptionCodeMsg().getCode());
responseEntity.setMsg(iServiceException.getExceptionCodeMsg().getMsg());
responseEntity.setSuccess(false);
responseEntity.setException(iServiceException.getClass().getName());
return responseEntity;
}
}
6. IServiceException 自定义异常
package com.xiaoye.exception;
import com.xiaoye.common.ExceptionCodeMsg;
import lombok.Getter;
import lombok.ToString;
@Getter
@ToString
public class IServiceException extends Exception {
private ExceptionCodeMsg exceptionCodeMsg;
public IServiceException(ExceptionCodeMsg exceptionCodeMsg) {
this.exceptionCodeMsg = exceptionCodeMsg;
}
}
7. GloabExceptionHandler 全局异常拦截
package com.xiaoye.gloab;
import com.xiaoye.common.ExceptionCodeMsg;
import com.xiaoye.common.ResponseEntity;
import com.xiaoye.exception.IServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GloabExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(GloabExceptionHandler.class);
@ExceptionHandler(IServiceException.class)
public ResponseEntity handIServiceException(IServiceException exception) {
exception.printStackTrace();
return ResponseEntity.IS_ERROR(exception);
}
@ExceptionHandler(Exception.class)
public ResponseEntity handException(Exception exception) {
exception.printStackTrace();
return ResponseEntity.IS_ERROR(ExceptionCodeMsg.ERROR, exception);
}
private void printConsole(Exception exception) {
exception.printStackTrace();
}
}