opt:优化前后端交互后端返回不统一问题
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package org.nl.common.base;
|
||||
|
||||
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 java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* 表格分页数据对象
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class ResponseData<T> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 总记录数
|
||||
*/
|
||||
private long totalElements;
|
||||
/**
|
||||
* 列表数据
|
||||
*/
|
||||
private Object data = new ArrayList<>();
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime timestamp = LocalDateTime.now();
|
||||
/**
|
||||
* 消息状态码
|
||||
*/
|
||||
private Integer code;
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private String message;
|
||||
//五期特有
|
||||
private Integer respCode;
|
||||
private String respMsg;
|
||||
|
||||
public static ResponseEntity build(IPage page) {
|
||||
ResponseData rspData = new ResponseData<>();
|
||||
rspData.setCode(HttpStatus.OK.value());
|
||||
rspData.setMessage("操作成功");
|
||||
rspData.setRespMsg("操作成功");
|
||||
rspData.setRespCode(0);
|
||||
rspData.setData(page.getRecords());
|
||||
rspData.setTotalElements(page.getTotal());
|
||||
return new ResponseEntity(rspData, org.springframework.http.HttpStatus.OK);
|
||||
|
||||
}
|
||||
|
||||
public static ResponseEntity build(Object data) {
|
||||
ResponseData rspData = new ResponseData<>();
|
||||
rspData.setCode(HttpStatus.OK.value());
|
||||
rspData.setMessage("操作成功");
|
||||
rspData.setData(data);
|
||||
rspData.setRespMsg("操作成功");
|
||||
rspData.setRespCode(0);
|
||||
return new ResponseEntity(rspData, org.springframework.http.HttpStatus.OK);
|
||||
}
|
||||
public static ResponseEntity build(Object data,Long total) {
|
||||
ResponseData rspData = new ResponseData<>();
|
||||
rspData.setCode(HttpStatus.OK.value());
|
||||
rspData.setMessage("操作成功");
|
||||
rspData.setData(data);
|
||||
rspData.setRespMsg("操作成功");
|
||||
rspData.setRespCode(0);
|
||||
rspData.setTotalElements(total);
|
||||
return new ResponseEntity(rspData, org.springframework.http.HttpStatus.OK);
|
||||
}
|
||||
|
||||
public static ResponseEntity build() {
|
||||
ResponseData rspData = new ResponseData<>();
|
||||
rspData.setCode(HttpStatus.OK.value());
|
||||
rspData.setMessage("操作成功");
|
||||
rspData.setRespMsg("操作成功");
|
||||
rspData.setRespCode(0);
|
||||
return new ResponseEntity(rspData, org.springframework.http.HttpStatus.OK);
|
||||
}
|
||||
|
||||
public static ResponseEntity error(Integer code,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, org.springframework.http.HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package org.nl.common.base;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 表格分页数据对象
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class TableDataInfo<T> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 总记录数
|
||||
*/
|
||||
private long totalElements;
|
||||
|
||||
/**
|
||||
* 列表数据
|
||||
*/
|
||||
private Object content = new ArrayList<>();
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime timestamp = LocalDateTime.now();
|
||||
/**
|
||||
* 消息状态码
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private String message;
|
||||
private Integer respCode;
|
||||
private String respMsg;
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
* @param list 列表数据
|
||||
* @param total 总记录数
|
||||
*/
|
||||
public TableDataInfo(List<T> list, long total) {
|
||||
this.content = list;
|
||||
this.totalElements = total;
|
||||
}
|
||||
|
||||
public static <T> TableDataInfo<T> build(IPage<T> page) {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setMessage("操作成功");
|
||||
rspData.setRespMsg("操作成功");
|
||||
rspData.setRespCode(0);
|
||||
rspData.setContent(page.getRecords());
|
||||
rspData.setTotalElements(page.getTotal());
|
||||
return rspData;
|
||||
}
|
||||
|
||||
public static <T> TableDataInfo<T> build(Object list) {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setMessage("操作成功");
|
||||
rspData.setContent(list);
|
||||
rspData.setRespMsg("操作成功");
|
||||
rspData.setRespCode(0);
|
||||
return rspData;
|
||||
}
|
||||
|
||||
public static <T> TableDataInfo<T> build() {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setMessage("操作成功");
|
||||
rspData.setRespMsg("操作成功");
|
||||
rspData.setRespCode(0);
|
||||
return rspData;
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.nl.common.exception.handler;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-23
|
||||
*/
|
||||
@Data
|
||||
class ApiError {
|
||||
|
||||
/**
|
||||
* 总记录数
|
||||
*/
|
||||
private long totalElements = 0;
|
||||
/**
|
||||
* 列表数据
|
||||
*/
|
||||
private Object content = new ArrayList<>();
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime timestamp;
|
||||
/**
|
||||
* 消息状态码
|
||||
*/
|
||||
private String code;
|
||||
private Integer respCode;
|
||||
private String respMsg;
|
||||
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private String message;
|
||||
|
||||
private ApiError() {
|
||||
timestamp = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public static ApiError error(String message) {
|
||||
ApiError apiError = new ApiError();
|
||||
apiError.setCode("400");
|
||||
apiError.setRespCode(1);
|
||||
apiError.setMessage(message);
|
||||
apiError.setRespMsg(message);
|
||||
return apiError;
|
||||
}
|
||||
|
||||
public static ApiError error(String status, String message) {
|
||||
ApiError apiError = new ApiError();
|
||||
apiError.setCode(status);
|
||||
apiError.setMessage(message);
|
||||
apiError.setMessage(message);
|
||||
apiError.setRespMsg(message);
|
||||
return apiError;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,20 +17,14 @@ package org.nl.common.exception.handler;
|
||||
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.common.base.ResponseData;
|
||||
import org.nl.common.exception.BadRequestException;
|
||||
import org.nl.common.exception.EntityExistException;
|
||||
import org.nl.common.exception.EntityNotFoundException;
|
||||
import org.nl.common.utils.ThrowableUtil;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-23
|
||||
@@ -43,10 +37,9 @@ public class GlobalExceptionHandler {
|
||||
* 处理所有不可知的异常
|
||||
*/
|
||||
@ExceptionHandler(Throwable.class)
|
||||
public ResponseEntity<ApiError> handleException(Throwable e) {
|
||||
// 打印堆栈信息
|
||||
public ResponseEntity handleException(Throwable e) {
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return buildResponseEntity(ApiError.error(e.getMessage()));
|
||||
return ResponseData.error(HttpStatus.BAD_REQUEST.value(),e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,62 +49,17 @@ public class GlobalExceptionHandler {
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = NotLoginException.class)
|
||||
public ResponseEntity<ApiError> notLoginException(Exception e) {
|
||||
public ResponseEntity notLoginException(Exception e) {
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return buildResponseEntity(ApiError.error("401", "token 失效"));
|
||||
return ResponseData.error(HttpStatus.UNAUTHORIZED.value(),"token失效");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理自定义异常
|
||||
*/
|
||||
@ExceptionHandler(value = BadRequestException.class)
|
||||
public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
|
||||
public ResponseEntity badRequestException(BadRequestException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return buildResponseEntity(ApiError.error(e.getCode(), e.getMessage()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 EntityExist
|
||||
*/
|
||||
@ExceptionHandler(value = EntityExistException.class)
|
||||
public ResponseEntity<ApiError> entityExistException(EntityExistException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return buildResponseEntity(ApiError.error(e.getMessage()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 EntityNotFound
|
||||
*/
|
||||
@ExceptionHandler(value = EntityNotFoundException.class)
|
||||
public ResponseEntity<ApiError> entityNotFoundException(EntityNotFoundException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return buildResponseEntity(ApiError.error("404", e.getMessage()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理所有接口数据验证异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
String[] str = Objects.requireNonNull(e.getBindingResult().getAllErrors().get(0).getCodes())[1].split("\\.");
|
||||
String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
|
||||
String msg = "不能为空";
|
||||
if (msg.equals(message)) {
|
||||
message = str[1] + ":" + message;
|
||||
}
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一返回
|
||||
*/
|
||||
private ResponseEntity<ApiError> buildResponseEntity(ApiError apiError) {
|
||||
return new ResponseEntity<>(apiError, HttpStatus.OK);
|
||||
return ResponseData.error(HttpStatus.BAD_REQUEST.value(),e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user