1. 在support模块引入几个工具类
父pom
support pom
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2. 封装数据
2.1 封装响应码
package com.ylp.common.constants;
/**
* 保存响应状态码
*/
public interface HttpStatus {
/**
* 操作成功
*/
public static final int SUCCESS = 200;
/**
* 对象创建成功
*/
public static final int CREATED = 201;
/**
* 请求已经被接受
*/
public static final int ACCEPTED = 202;
/**
* 操作已经执行成功,但是没有返回数据
*/
public static final int NO_CONTENT = 204;
/**
* 资源已被移除
*/
public static final int MOVED_PERM = 301;
/**
* 重定向
*/
public static final int SEE_OTHER = 303;
/**
* 资源没有被修改
*/
public static final int NOT_MODIFIED = 304;
/**
* 参数列表错误(缺少,格式不匹配)
*/
public static final int BAD_REQUEST = 400;
/**
* 未授权
*/
public static final int UNAUTHORIZED = 401;
/**
* 访问受限,授权过期
*/
public static final int FORBIDDEN = 403;
/**
* 资源,服务未找到
*/
public static final int NOT_FOUND = 404;
/**
* 不允许的http方法
*/
public static final int BAD_METHOD = 405;
/**
* 资源冲突,或者资源被锁
*/
public static final int CONFLICT = 409;
/**
* 不支持的数据,媒体类型
*/
public static final int UNSUPPORTED_TYPE = 415;
/**
* 系统内部错误
*/
public static final int ERROR = 500;
/**
* 接口未实现
*/
public static final int NOT_IMPLEMENTED = 501;
/**
* 系统警告消息
*/
public static final int WARN = 601;
}
2.2 封装响应数据类
注: 要使用OpenApi3注解的话在common模块也需要引入knife4j依赖
package com.ylp.common.response;
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "响应数据")
public class Result<T> {
/**
* 状态码 0:成功 1: 失败
*/
@Schema(description = "状态码")
private int code;
/**
* 返回内容
*/
@Schema(description = "返回内容")
private String message;
/**
* 数据对象
*/
@Schema(description = "返回数据")
private T data;
public Result(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
public static <E> Result<E> success() {
return new Result<>(0, "操作成功", null);
}
public static <E> Result<E> success(E data) {
return new Result<>(0, "操作成功", data);
}
public static <E> Result<E> success(String message, E data) {
return new Result<>(0, message, data);
}
public static <E> Result<E> error(String message) {
return new Result<>(1, message, null);
}
public static <E> Result<E> error() {
return new Result<>(1, "操作失败", null);
}
}
2.3 分页查询数据封装
package com.ylp.common.response;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
@Schema(description = "分页查询数据")
public class PageResult<T> {
@Schema(description = "分页数据")
private List<T> list;
@Schema(description = "数据总条数")
private long total;
public PageResult(List<T> data, long total) {
this.list = data;
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
}
2.4 分页参数封装
可继承此类,接受前端分页条件查询参数
package com.ylp.common.request;
import java.io.Serializable;
public class PageParm implements Serializable {
// 当前页
private Long currentPage = 1L;
// 每页的数据量
private Long pageSize = 10L;
public Long getCurrentPage() {
return currentPage;
}
public PageParm() {
}
public PageParm(Long currentPage, Long pageSize) {
this.currentPage = currentPage;
this.pageSize = pageSize;
}
public void setCurrentPage(Long currentPage) {
this.currentPage = currentPage;
}
public Long getPageSize() {
return pageSize;
}
public void setPageSize(Long pageSize) {
this.pageSize = pageSize;
}
}
2.5 自定义业务异常
用于业务异常抛出
package com.ylp.common.exception;
/**
* 自定义的业务异常。当我们的系统出现异常时,返回该异常给前端
*/
public class ServiceException extends RuntimeException{
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
private Integer code;
/**
* 错误提示
*/
private String message;
/**
* 错误明细,内部调试错误
*/
private String detailMessage;
/**
* 空构造方法,避免反序列化问题
*/
public ServiceException() {
}
public ServiceException(String message) {
this.message = message;
}
public ServiceException(Integer code,String message) {
this.message = message;
this.code = code;
}
public String getDetailMessage() {
return detailMessage;
}
public String getMessage() {
return message;
}
public Integer getCode() {
return code;
}
public ServiceException setMessage(String message) {
this.message = message;
return this;
}
}
3. 全局异常处理
在support 配置全局异常处理,用于捕获系统异常,防止返回前端报错栈信息及登录拦截信息处理
package com.ylp.support.config.execption;
import com.ylp.common.exception.ServiceException;
import com.ylp.common.response.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 全局异常处理
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 业务异常
*/
@ExceptionHandler(ServiceException.class)
public Result handleServiceException(ServiceException e)
{
log.error(e.getMessage(), e);
return Result.error(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : "操作失败");
}
/**
* 服务异常
*/
@ExceptionHandler(Exception.class)
public Result handlerException(Exception e) {
log.warn(e.getMessage());
return Result.error(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : "服务异常");
}
}