Java API 统一返回封装
一、返回封装类 R
package com.longqin.nagas.common.core.util;
import com.longqin.nagas.common.core.constant.CommonConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* api返回封装类
*
* @Author lb
* @Date 2022/1/5 19:31
* @Version 1.0
*/
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Data
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
private Integer code; //返回码
private String msg; //返回消息
private T data; //返回数据
public static <T> R<T> ok(){return restResult(CommonConstants.SUCCESS,null,null);}
public static <T> R<T> ok(T data){return restResult(CommonConstants.SUCCESS,null,data);}
public static <T> R<T> ok(T data,String msg){return restResult(CommonConstants.SUCCESS,msg,data);}
public static <T> R<T> failed() {
return restResult(CommonConstants.FAIL, null,null);
}
public static <T> R<T> failed(String msg) { return restResult(CommonConstants.FAIL, msg,null); }
public static <T> R<T> failed(T data) {
return restResult(CommonConstants.FAIL,null,data);
}
public static <T> R<T> failed(T data, String msg) {
return restResult(CommonConstants.FAIL, msg, data);
}
private static <T> R<T> restResult(Integer code, String msg,T data) {
R<T> apiResult = new R<>();
apiResult.setCode(code);
apiResult.setMsg(msg);
apiResult.setData(data);
return apiResult;
}
}
二、常量类
package com.longqin.nagas.common.core.constant;
/**
* @Author lb
* @Date 2022/1/6 11:04
* @Version 1.0
*/
public interface CommonConstants {
/**
* 删除
*/
String STATUS_DEL = "1";
/**
* 正常
*/
String STATUS_NORMAL = "0";
/**
* 锁定
*/
String STATUS_LOCK = "9";
/**
* 菜单树根节点
*/
Integer MENU_TREE_ROOT_ID = -1;
/**
* 菜单
*/
String MENU = "0";
/**
* 编码
*/
String UTF8 = "UTF-8";
/**
* JSON 资源
*/
String CONTENT_TYPE = "application/json; charset=utf-8";
/**
* 前端工程名
*/
String FRONT_END_PROJECT = "nagas-ui";
/**
* 后端工程名
*/
String BACK_END_PROJECT = "nagas";
/**
* 成功标记
*/
Integer SUCCESS = 0;
/**
* 失败标记
*/
Integer FAIL = 1;
/**
* 验证码前缀
*/
String DEFAULT_CODE_KEY = "DEFAULT_CODE_KEY_";
/**
* 当前页
*/
String CURRENT = "current";
/**
* size
*/
String SIZE = "size";
}
三、自定义异常
package com.longqin.nagas.common.core.exception;
/**
* @Author lb
* @Date 2022/1/5 19:24
* @Version 1.0
*/
public class BusinessException extends RuntimeException{
private static final long serialVersionUID = 1L;
public BusinessException(String message) {
super(message);
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
}
public BusinessException(Throwable cause) {
super(cause);
}
protected BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
四、全局异常处理
异常处理中使用了Spring Validation验证框架,需要先在pom.xml文件中引入相关jar包依赖,如下:
<!--Spring Validation验证框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
全局异常处理类如下:
package com.longqin.nagas.common.core.exception.handler;
import com.longqin.nagas.common.core.exception.BusinessException;
import com.longqin.nagas.common.core.util.R;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
/**
* @Author lb
* @Date 2022/1/21 16:18
* @Version 1.0
*/
@RestControllerAdvice
public class GlobalExceptionControllerAdvice {
/**
* 业务异常
*
* @param businessException
* @return
*/
@ExceptionHandler(value = BusinessException.class)
public Object globalExceptionHandler(BusinessException businessException) {
return R.failed(businessException.getMessage());
}
/**
* Spring Validation验证框架后 参数不符合要求的异常处理
* @param ex {@link MethodArgumentNotValidException}
* @return
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Object MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex){
StringBuffer sb = new StringBuffer("请求参数错误:");
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
for (int i = 0; i < fieldErrors.size(); i++) {
sb.append(i + 1).append(".").append(fieldErrors.get(i).getDefaultMessage()).append(";");
}
return R.failed(null,sb.toString());
}
}