通用模板

This commit is contained in:
huangge1199 2025-07-12 09:35:21 +08:00
parent 888ce18e02
commit 9e79678bfb
8 changed files with 409 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package com.huangge1199.ai.common;
import lombok.Data;
import java.io.Serializable;
/**
* 通用的删除请求类
*
* @author huangge1199
* @since 2025/6/30 16:21:47
*/
@Data
public class DeleteRequest implements Serializable {
/**
* id
*/
private Long id;
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,94 @@
package com.huangge1199.ai.common;
/**
* 返回状态码
*
* @author ruoyi
*/
public class 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;
}

View File

@ -0,0 +1,33 @@
package com.huangge1199.ai.common;
import lombok.Data;
/**
* 通用的分页请求类
*
* @author huangge1199
* @since 2025/6/30 16:07:29
*/
@Data
public class PageRequest {
/**
* 当前页号
*/
private int current = 1;
/**
* 页面大小
*/
private int pageSize = 10;
/**
* 排序字段
*/
private String sortField;
/**
* 排序顺序默认升序
*/
private String sortOrder = "descend";
}

View File

@ -0,0 +1,113 @@
package com.huangge1199.ai.common;
import java.io.Serializable;
/**
* 响应信息主体
*
* @author ruoyi
*/
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 成功
*/
public static final int SUCCESS = HttpStatus.SUCCESS;
/**
* 失败
*/
public static final int FAIL = HttpStatus.ERROR;
private int code;
private String msg;
private T data;
public static <T> R<T> ok() {
return restResult(null, SUCCESS, "操作成功");
}
public static <T> R<T> ok(T data) {
return restResult(data, SUCCESS, "操作成功");
}
public static <T> R<T> ok(T data, String msg) {
return restResult(data, SUCCESS, msg);
}
public static <T> R<T> fail() {
return restResult(null, FAIL, "操作失败");
}
public static <T> R<T> fail(String msg) {
return restResult(null, FAIL, msg);
}
public static <T> R<T> fail(Exception e) {
String msg = e.getMessage();
if (e.getCause() != null) {
msg = e.getCause().getMessage();
} else {
return restResult(null, FAIL, msg);
}
if (e.getCause().getCause() != null) {
msg = e.getCause().getCause().getMessage();
}
return restResult(null, FAIL, msg);
}
public static <T> R<T> fail(T data) {
return restResult(data, FAIL, "操作失败");
}
public static <T> R<T> fail(T data, String msg) {
return restResult(data, FAIL, msg);
}
public static <T> R<T> fail(int code, String msg) {
return restResult(null, code, msg);
}
private static <T> R<T> restResult(T data, int code, String msg) {
R<T> apiResult = new R<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static <T> Boolean isError(R<T> ret) {
return !isSuccess(ret);
}
public static <T> Boolean isSuccess(R<T> ret) {
return R.SUCCESS == ret.getCode();
}
}

View File

@ -0,0 +1,38 @@
package com.huangge1199.ai.exception;
import lombok.Getter;
/**
* 异常信息
*
* @author huangge1199
* @since 2025/6/27 16:26:54
*/
@Getter
public enum ErrorCode {
SUCCESS(200, "ok"),
PARAMS_ERROR(40000, "请求参数错误"),
NOT_LOGIN_ERROR(40100, "未登录"),
NO_AUTH_ERROR(40101, "无权限"),
NOT_FOUND_ERROR(40400, "请求数据不存在"),
FORBIDDEN_ERROR(40300, "禁止访问"),
SYSTEM_ERROR(50000, "系统内部异常"),
OPERATION_ERROR(50001, "操作失败");
/**
* 状态码
*/
private final int code;
/**
* 信息
*/
private final String message;
ErrorCode(int code, String message) {
this.code = code;
this.message = message;
}
}

View File

@ -0,0 +1,29 @@
package com.huangge1199.ai.exception;
import com.huangge1199.picture.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 全局异常处理器
*
* @author huangge1199
* @since 2025/6/30 10:56:33
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(MyException.class)
public R<?> businessExceptionHandler(MyException e) {
log.error("BusinessException", e);
return R.fail(e.getCode(), e.getMessage());
}
@ExceptionHandler(RuntimeException.class)
public R<?> businessExceptionHandler(RuntimeException e) {
log.error("RuntimeException", e);
return R.fail(ErrorCode.SYSTEM_ERROR, "系统错误");
}
}

View File

@ -0,0 +1,36 @@
package com.huangge1199.ai.exception;
import lombok.Getter;
import lombok.Setter;
/**
* 自定义业务异常
*
* @author huangge1199
* @since 2025/6/30 10:42:10
*/
@Setter
@Getter
public class MyException extends RuntimeException {
/**
* 错误码
*/
private final int code;
public MyException(int code, String message) {
super(message);
this.code = code;
}
public MyException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.code = errorCode.getCode();
}
public MyException(ErrorCode errorCode, String message) {
super(message);
this.code = errorCode.getCode();
}
}

View File

@ -0,0 +1,44 @@
package com.huangge1199.ai.exception;
/**
* 异常处理工具类
*
* @author huangge1199
* @since 2025/6/30 11:00:12
*/
public class ThrowUtils {
/**
* 条件成立则抛异常
*
* @param condition 条件
* @param runtimeException 异常
*/
public static void throwIf(boolean condition, RuntimeException runtimeException) {
if (condition) {
throw runtimeException;
}
}
/**
* 条件成立则抛异常
*
* @param condition 条件
* @param errorCode 错误码
*/
public static void throwIf(boolean condition, ErrorCode errorCode) {
throwIf(condition, new MyException(errorCode));
}
/**
* 条件成立则抛异常
*
* @param condition 条件
* @param errorCode 错误码
* @param message 错误信息
*/
public static void throwIf(boolean condition, ErrorCode errorCode, String message) {
throwIf(condition, new MyException(errorCode, message));
}
}