实现模块:
1.定义响应数据格式
- 常量
public interface Conf {
/**
* 成功标记
*/
Integer SUCCESS = 0;
/**
* 失败标记
*/
Integer FAIL = 1;
}
- 响应体
/**
* 响应信息主体
*
* @param <T>
* @author guiyun
*/
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements 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 <T> R<T> ok() {
return restResult(null, Conf.SUCCESS, null);
}
public static <T> R<T> ok(T data) {
return restResult(data, Conf.SUCCESS, null);
}
public static <T> R<T> ok(T data, String msg) {
return restResult(data, Conf.SUCCESS, msg);
}
public static <T> R<T> failed() {
return restResult(null, Conf.FAIL, null);
}
public static <T> R<T> failed(String msg) {
return restResult(null, Conf.FAIL, msg);
}
public static <T> R<T> failed(T data) {
return restResult(data, Conf.FAIL, null);
}
public static <T> R<T> failed(T data, String msg) {
return restResult(data, Conf.FAIL, msg);
}
private static <T> R<T> restResult(T data, int code, String msg) {
R<T> 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() {
// 以后开启国际化
/*
* String message = null; if (!StringUtils.isEmpty(code)) { message =
* MessageUtils.message(code, args); } if (message == null) { message =
* defaultMessage; }
*/
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;
/**
* 全局异常拦截
*
* @author 29631
*
*/
@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;
}
/**
* 拦截Exception类的异常
*
* @param e
* @return
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public R<?> exceptionHandler(Exception e, HttpServletRequest request) {
logger.error("异常:" + e.getMessage(), e);
R<?> result = new R<>();
result.setMsg("系统异常,请稍后再试!");
return result;
}
}
原理模块等有时间再补上