opt:优化前后端交互后端返回不统一问题
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
package org.nl.common.base;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.unit.DataUnit;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.ResponseEntity;import org.nl.common.base.ResponseData;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 表格分页数据对象
|
||||
@@ -30,8 +33,7 @@ public class ResponseData<T> implements Serializable {
|
||||
* 列表数据
|
||||
*/
|
||||
private Object data = new ArrayList<>();
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime timestamp = LocalDateTime.now();
|
||||
private String timestamp=DateUtil.now();
|
||||
/**
|
||||
* 消息状态码
|
||||
*/
|
||||
@@ -60,9 +62,34 @@ public class ResponseData<T> implements Serializable {
|
||||
ResponseData rspData = new ResponseData<>();
|
||||
rspData.setCode(HttpStatus.OK.value());
|
||||
rspData.setMessage("操作成功");
|
||||
rspData.setData(data);
|
||||
rspData.setRespMsg("操作成功");
|
||||
rspData.setRespCode(0);
|
||||
if (data instanceof IPage){
|
||||
final IPage page = (IPage) data;
|
||||
final List records = page.getRecords();
|
||||
final long total = page.getTotal();
|
||||
rspData.setTotalElements(total);
|
||||
rspData.setData(records);
|
||||
}else {
|
||||
rspData.setData(data);
|
||||
}
|
||||
return new ResponseEntity(rspData, org.springframework.http.HttpStatus.OK);
|
||||
}
|
||||
public static ResponseEntity build(Object data,HttpStatus status) {
|
||||
ResponseData rspData = new ResponseData<>();
|
||||
rspData.setCode(HttpStatus.OK.value());
|
||||
rspData.setMessage("操作成功");
|
||||
rspData.setRespMsg("操作成功");
|
||||
rspData.setRespCode(0);
|
||||
if (data instanceof IPage){
|
||||
final IPage page = (IPage) data;
|
||||
final List records = page.getRecords();
|
||||
final long total = page.getTotal();
|
||||
rspData.setTotalElements(total);
|
||||
rspData.setData(records);
|
||||
}else {
|
||||
rspData.setData(data);
|
||||
}
|
||||
return new ResponseEntity(rspData, org.springframework.http.HttpStatus.OK);
|
||||
}
|
||||
public static ResponseEntity build(Object data,Long total) {
|
||||
@@ -85,7 +112,15 @@ public class ResponseData<T> implements Serializable {
|
||||
return new ResponseEntity(rspData, org.springframework.http.HttpStatus.OK);
|
||||
}
|
||||
|
||||
public static ResponseEntity error(Integer code,String msg) {
|
||||
public static ResponseEntity error(HttpStatus status,String msg) {
|
||||
ResponseData rspData = new ResponseData<>();
|
||||
rspData.setCode(HttpStatus.BAD_REQUEST.value());
|
||||
rspData.setMessage(msg);
|
||||
rspData.setRespMsg(msg);
|
||||
rspData.setRespCode(1);
|
||||
return new ResponseEntity(rspData, status);
|
||||
}
|
||||
public static ResponseEntity error(String msg) {
|
||||
ResponseData rspData = new ResponseData<>();
|
||||
rspData.setCode(HttpStatus.BAD_REQUEST.value());
|
||||
rspData.setMessage(msg);
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.nl.common.base.ResponseData;
|
||||
import org.nl.common.exception.BadRequestException;
|
||||
import org.nl.common.utils.ThrowableUtil;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.ResponseEntity;import org.nl.common.base.ResponseData;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@@ -39,7 +39,7 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(Throwable.class)
|
||||
public ResponseEntity handleException(Throwable e) {
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return ResponseData.error(HttpStatus.BAD_REQUEST.value(),e.getMessage());
|
||||
return ResponseData.error(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,7 +51,7 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(value = NotLoginException.class)
|
||||
public ResponseEntity notLoginException(Exception e) {
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return ResponseData.error(HttpStatus.UNAUTHORIZED.value(),"token失效");
|
||||
return ResponseData.error(HttpStatus.UNAUTHORIZED,"token失效");
|
||||
}
|
||||
/**
|
||||
* 处理自定义异常
|
||||
@@ -60,6 +60,6 @@ public class GlobalExceptionHandler {
|
||||
public ResponseEntity badRequestException(BadRequestException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return ResponseData.error(HttpStatus.BAD_REQUEST.value(),e.getMessage());
|
||||
return ResponseData.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user