1.定义响应数据格式
- 常量
public interface Conf {
Integer SUCCESS = 0;
Integer FAIL = 1;
}
- 响应体
@Builder @ToString @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true) public class Rimplements Serializable { private static final long serialVersionUID = 1L; @Getter @Setter // @ApiModelProperty(value = "返回标记:成功标记=0,失败标记=1") private int code; @Getter @Setter // @ApiModelProperty(value = "返回信息") private String msg; @Getter @Setter // @ApiModelProperty(value = "数据") private T data; public static R ok() { return restResult(null, Conf.SUCCESS, null); } public static R ok(T data) { return restResult(data, Conf.SUCCESS, null); } public static R ok(T data, String msg) { return restResult(data, Conf.SUCCESS, msg); } public static R failed() { return restResult(null, Conf.FAIL, null); } public static R failed(String msg) { return restResult(null, Conf.FAIL, msg); } public static R failed(T data) { return restResult(data, Conf.FAIL, null); } public static R failed(T data, String msg) { return restResult(data, Conf.FAIL, msg); } private static R restResult(T data, int code, String msg) { R apiResult = new R<>(); apiResult.setCode(code); apiResult.setData(data); apiResult.setMsg(msg); return apiResult; } }
2.自定义异常类与全局异常拦截
- 自定义异常类
public class baseException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String module;
private Integer code;
private Object[] args;
private String defaultMessage;
public baseException(String module, Integer code, Object[] args, String defaultMessage) {
this.module = module;
this.code = code;
this.args = args;
this.defaultMessage = defaultMessage;
}
public baseException(String module, Integer code, Object[] args) {
this(module, code, args, null);
}
public baseException(Integer code, String defaultMessage) {
this(null, code, null, defaultMessage);
}
public baseException(Integer code, Object[] args) {
this(null, code, args, null);
}
public baseException(String defaultMessage) {
this(null, Conf.FAIL, null, defaultMessage); // 默认给 0
}
@Override
public String getMessage() {
// 以后开启国际化
return defaultMessage;
}
public String getModule() {
return module;
}
public Integer getCode() {
return code;
}
public Object[] getArgs() {
return args;
}
public String getDefaultMessage() {
return defaultMessage;
}
}
- 全局异常拦截
package com.guiyun.exception;
@ControllerAdvice
public class DefaultExceptionHandler {
Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class);
@ExceptionHandler(baseException.class)
@ResponseBody
public R> baseException(baseException e) {
logger.error(e.getMessage());
R> result = new R<>();
result.setMsg(e.getDefaultMessage());
return result;
}
@ExceptionHandler(ParseException.class)
public R> parseException(ParseException e) {
logger.error(e.getMessage(), e);
R> result = new R<>();
result.setMsg(e.getMessage());
return result;
}
@ExceptionHandler(Exception.class)
@ResponseBody
public R> exceptionHandler(Exception e, HttpServletRequest request) {
logger.error("异常:" + e.getMessage(), e);
R> result = new R<>();
result.setMsg("系统异常,请稍后再试!");
return result;
}
}
原理模块等有时间再补上



