/**
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
-
自定义异常
-
@author niuniu
-
@date 2019-04-30
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class CommonException extends RuntimeException{private static final long serialVersionUID = -2470461654663264392L;
private Integer errorCode;
private String message;public CommonException() {
super();
}public CommonException(String message) {
super(message);
this.message = message;
}public CommonException(Integer errorCode, String message) {
super(message);
this.errorCode = errorCode;
this.message = message;
}public CommonException(ApiCode apiCode) {
super(apiCode.getMessage());
this.errorCode = apiCode.getCode();
this.message = apiCode.getMessage();
}public CommonException(String message, Throwable cause) {
super(message, cause);
}public CommonException(Throwable cause) {
super(cause);
}
}
返回数据通用模板:
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
-
API 返回结果
-
@author niuniu
-
@date 2020-04-30
*/
@Data
@Accessors(chain = true)
@Builder
@AllArgsConstructor
public class ApiResult implements Serializable {
private static final long serialVersionUID = 8004487252556526569L;/**
- 响应码
*/
@ApiModelProperty(value = “响应码”)
private int status;
/**
- 是否成功
*/
@ApiModelProperty(value = “是否成功:成功true,失败false”)
private boolean success;
/**
- 响应消息
*/
@ApiModelProperty(value = “响应消息”)
private String msg;
/**
- 总条数
*/
@ApiModelProperty(value = “总条数”)
private Integer total;
/**
- 总页数
*/
@ApiModelProperty(value = “总页数”)
private Integer totalPage;
/**
- 响应数据
*/
@ApiModelProperty(value = “响应数据”)
private T data;
/**
- 响应时间
*/
@ApiModelProperty(value = “响应时间”)
@JSONField(format = “yyyy-MM-dd HH:mm:ss”)
@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”,timezone=“GMT+8”)
private Date time;
public ApiResult() {
time = new Date();
}public static ApiResult result(boolean flag){
if (flag){
return ok();
}
return fail();
}public static ApiResult result(ApiCode apiCode){
return result(apiCode,null);
}public static ApiResult result(ApiCode apiCode, T data){
return result(apiCode,null,data);
}public static ApiResult resultPage(Integer total, Integer totalPage, T data){
return (ApiResult) ApiResult.builder()
.total(total)
.totalPage(totalPage)
.status(200)
.msg(null)
.data(data)
.success(true)
.time(new Date())
.build();
}public static ApiResult result(ApiCode apiCode, String message, T data){
boolean success = false;
if (apiCode.getCode() == ApiCode.SUCCESS.getCode()){
success = true;
}
if (StringUtils.isBlank(message)){
message = apiCode.getMessage();
}
return (ApiResult) ApiResult.builder()
.status(apiCode.getCode())
.msg(message)
.data(data)
.success(success)
.time(new Date())
.build();
}public static ApiResult ok(){
return ok(null);
}public static ApiResult ok(T data){
return result(ApiCode.SUCCESS,data);
}public static ApiResult ok(T data, String message){
return result(ApiCode.SUCCESS,message,data);
}public static ApiResult<Map<String,Object>> okMap(String key, Object value){
Map<String,Object> map = new HashMap<>(1);
map.put(key,value);
return ok(map);
}public static ApiResult fail(ApiCode apiCode){
return result(apiCode,null);
}public static ApiResult fail(String message){
return result(ApiCode.FAIL,message,null);}
public static ApiResult fail(ApiCode apiCode, T data){
if (ApiCode.SUCCESS == apiCode){
throw new RuntimeException(“失败结果状态码不能为” + ApiCode.SUCCESS.getCode());
}
return result(apiCode,data);}
public static ApiResult fail(Integer errorCode, String message){
return new ApiResult()
.setSuccess(false)
.setStatus(errorCode)
.setMsg(message);
}public static ApiResult<Map<String,Object>> fail(String key, Object value){
Map<String,Object> map = new HashMap<>(1);
map.put(key,value);
return result(ApiCode.FAIL,map);
}public static ApiResult resultPage(T t, int limit){
List list = (List) t;
int count = list.size() / limit;
if (list.size() == 0) {
return (ApiResult) ApiResult.builder()
.total(0)
.totalPage(0)
.status(200)
.msg(null)
.data(list)
.success(true)
.time(new Date())
.build();
}
if (list.size() <= limit) {
return (ApiResult) ApiResult.builder()
.total(list.size())
.totalPage(1)
.status(200)
.msg(null)
.data(list)
.success(true)
.time(new Date())
.build();
} else if (count % limit == 0) {
return (ApiResult) ApiResult.builder()
.total(list.size())
.totalPage(count)
.status(200)
.msg(null)
.data(list)
.success(true)
.time(new Date())
.build();
} else {
return (ApiResult) ApiResult.builder()
.total(list.size())
.totalPage(count+1)
.status(200)
.msg(null)
.data(list)
.success(true)
.time(new Date())
.build();
}
}public static ApiResult fail() {
return fail(ApiCode.FAIL);
}
} - 响应码