diff --git a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java index 6e824c9..3915a0a 100644 --- a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java +++ b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java @@ -1,5 +1,6 @@ package com.ds.miniapps.biz; +import com.ds.commons.web.config.exection.WebGlobalExceptionHandler; import com.ds.commons.web.config.swagger.SwaggerConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -10,7 +11,7 @@ import org.springframework.context.annotation.Import; * @since 2023/8/27 */ @SpringBootApplication -@Import(SwaggerConfig.class) +@Import({SwaggerConfig.class, WebGlobalExceptionHandler.class}) public class MiniAppsApplication { public static void main(String[] args) { diff --git a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java index 64ca084..5c3c65e 100644 --- a/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java +++ b/ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java @@ -1,5 +1,6 @@ package com.ds.miniapps.biz.api.dummyinfo; +import com.ds.commons.basemodel.response.R; import com.ds.miniapps.biz.service.dummyinfo.DummyInfoService; import com.ds.miniapps.model.entity.dummyinfo.DummyInfo; import io.swagger.v3.oas.annotations.Operation; @@ -28,9 +29,9 @@ public class DummyInfoApi { private DummyInfoService dummyInfoService; @GetMapping("/get_list") @Operation(summary = "获取指定数量虚拟人信息") - public List getDummyInfoList( + public R> getDummyInfoList( @RequestParam("num") @Min(message = "数量不能小于1",value = 1) Integer num ){ - return dummyInfoService.randCreateDummyInfo(num); + return R.success(dummyInfoService.randCreateDummyInfo(num)); } } diff --git a/ds-commons/ds-basemodel/pom.xml b/ds-commons/ds-basemodel/pom.xml index 3c115bf..8c89f2d 100644 --- a/ds-commons/ds-basemodel/pom.xml +++ b/ds-commons/ds-basemodel/pom.xml @@ -23,6 +23,10 @@ easyexcel 3.3.2 + + org.projectlombok + lombok + \ No newline at end of file diff --git a/ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/exception/DsException.java b/ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/exception/DsException.java new file mode 100644 index 0000000..f73a3e8 --- /dev/null +++ b/ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/exception/DsException.java @@ -0,0 +1,43 @@ +package com.ds.commons.basemodel.exception; + +import com.ds.commons.basemodel.response.R; +import lombok.*; + +/** + * @author ds + * @since 2023/8/30 + */ +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +@Data +@AllArgsConstructor +public class DsException extends RuntimeException{ + + //错误码 + private Integer code; + + //错误信息 + private String msg; + + + + /** + * 使用R.MSG枚举定义的错误消息 + * @param msg 错误消息 + */ + public DsException(R.MSG msg) { + super(msg.getMsg()); + this.code=msg.getCode(); + this.msg=msg.getMsg(); + } + + public DsException append(String msg){ + this.msg+=msg; + return this; + } + + public DsException before(String msg){ + this.msg=msg+this.msg; + return this; + } +} diff --git a/ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/PageResult.java b/ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/PageResult.java new file mode 100644 index 0000000..f1d0fb2 --- /dev/null +++ b/ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/PageResult.java @@ -0,0 +1,38 @@ +package com.ds.commons.basemodel.response; + +import lombok.Getter; + +import java.io.Serial; +import java.io.Serializable; +import java.util.List; + +@Getter +public class PageResult implements Serializable { + + @Serial + private static final long serialVersionUID = 1402394526521183854L; + + private final List content; + private long page; + private long size; + private final long total; + private long totalPage; + + public PageResult(List content, + long page, + long size, + long total, + long totalPage){ + this.content=content; + this.page=page; + this.size=size; + this.totalPage=totalPage; + this.total=total; + } + + public PageResult(List content, + long total){ + this.content=content; + this.total=total; + } +} \ No newline at end of file diff --git a/ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/R.java b/ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/R.java new file mode 100644 index 0000000..b10c260 --- /dev/null +++ b/ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/R.java @@ -0,0 +1,166 @@ +package com.ds.commons.basemodel.response; + +import com.ds.commons.basemodel.exception.DsException; +import lombok.Data; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import java.io.Serializable; +import java.util.List; + +/** + * Response Tool + * @author ds + */ +@ToString +@NoArgsConstructor +@Data +public class R implements Serializable { + + @Getter + public enum MSG { + + // 成功 + SUCCESS(1, "SUCCESS"), + + // 失败 + FAILURE(0, "FAILURE"), + /* 参数错误 1000-1999 */ + PARAM_VALIDATION_FAILURE(1000,"参数无效"), + NOT_EXISTS_PARAM(1001,"参数不存在"), + /* 用户错误 2000-2999 */ + /* 接口异常 3000-3999 */ + SQL_EXCEPTION(3000,"SQL异常") + ; + + + final int code; + final String msg; + + MSG(int code, String msg) { + this.code = code; + this.msg = msg; + } + } + + private int code; + private String msg; + + private T data; + + public R setMsg(String msg) { + this.msg = msg; + return this; + } + + public R(MSG msg) { + this.code = msg.code; + this.msg = msg.msg; + } + + /** + * 通过预设错误码和自定义错误信息创建 + * + * @param code 错误码 + * @param msg 错误信息 + */ + private R(int code, String msg) { + this.code = code; + this.msg = msg; + } + + public R(T obj) { + this.code = MSG.SUCCESS.code; + this.msg = MSG.SUCCESS.msg; + this.data = obj; + } + + public R(MSG msg, T obj) { + this.code = msg.code; + this.msg = msg.msg; + this.data = obj; + } + + public R(int code, String msg, T obj) { + this.code = code; + this.msg = msg; + this.data = obj; + } + + public static R success() { + return new R<>(MSG.SUCCESS); + } + + public static R success(T obj) { + return new R<>(MSG.SUCCESS, obj); + } + + public static R success(int code, String msg) { + return new R<>(code, msg); + } + + public static R success(T obj,String msg) { + return new R<>(MSG.SUCCESS.code,msg, obj); + } + public static R failure(T obj,String msg) { + return new R<>(MSG.FAILURE.code,msg, obj); + } + + public static R failure() { + return new R<>(MSG.FAILURE); + } + + + public static R failure(int code, String msg) { + return new R<>(code, msg); + } + + + public static R failure(MSG e) { + return new R<>(e.getCode(), e.getMsg()); + } + + + public static R failure(DsException e) { + return new R<>(e.getCode(), e.getMsg()); + } + + + public static R failure(T obj) { + return new R<>(MSG.FAILURE, obj); + } + + /** + * 分页信息 + * + * @param content 内容列表 + * @param page 当前页数 + * @param size 每页数据量 + * @param total 数据总量 + * @param totalPage 总页数 + * @return res + */ + public static R> pageList(List content, long page, long size, long total, long totalPage) { + + + PageResult pr = new PageResult<>(content, page, total, size, totalPage); + + return new R<>(MSG.SUCCESS, pr); + } + + /** + * 分页信息 + * + * @param content 内容列表 + * @param total 数据总量 + * @return res + */ + public static R> simplePageList(List content, long total) { + + PageResult pr = new PageResult<>(content, total); + + return new R<>(MSG.SUCCESS, pr); + } + +} \ No newline at end of file diff --git a/ds-commons/ds-config/ds-web-config/pom.xml b/ds-commons/ds-config/ds-web-config/pom.xml index 48f094c..3b1ab56 100644 --- a/ds-commons/ds-config/ds-web-config/pom.xml +++ b/ds-commons/ds-config/ds-web-config/pom.xml @@ -54,5 +54,16 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config + + com.ds + ds-basemodel + 0.0.1-SNAPSHOT + compile + + + org.springframework + spring-tx + 5.3.27 + \ No newline at end of file diff --git a/ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/exection/WebGlobalExceptionHandler.java b/ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/exection/WebGlobalExceptionHandler.java new file mode 100644 index 0000000..4de6304 --- /dev/null +++ b/ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/exection/WebGlobalExceptionHandler.java @@ -0,0 +1,134 @@ +package com.ds.commons.web.config.exection; + +import com.ds.commons.basemodel.exception.DsException; +import com.ds.commons.basemodel.response.R; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.validation.ConstraintViolationException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.dao.DataAccessException; +import org.springframework.http.HttpStatus; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; + +/** + * @author dss + * @since 2022/3/2 + */ +@Slf4j +@RestControllerAdvice +public class WebGlobalExceptionHandler { + + + /** + * 请求方法错误异常处理 + * @param e 异常 + * @param request 请求体 + * @return 响应体 + */ + @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public R handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { + log.error(request.getMethod()+"-"+request.getRequestURI() + ":不支持当前请求方法",e); + return R.failure(HttpStatus.METHOD_NOT_ALLOWED.value(),"不支持当前请求方法"); + } + + /** + * SQL异常处理 + * @param e 异常 + * @param request 请求体 + * @return 响应体 + */ + @ExceptionHandler(DataAccessException.class) + public R dataAccessException(DataAccessException e,HttpServletRequest request){ + log.error(request.getMethod()+"-"+request.getRequestURI()+":sql异常",e); + SQLException sqlException=(SQLException) e.getCause(); + return R.failure(sqlException.getErrorCode(),"SQL异常"); + } + + + /** + * 找不到请求参数 + */ + @ExceptionHandler(MissingServletRequestParameterException.class) + public R handleMissingServletRequestParameterException(MissingServletRequestParameterException e,HttpServletRequest request){ + log.error(request.getMethod()+"-"+request.getRequestURI()+":"+e.getParameterName()+"参数未找到",e); + return R.failure(R.MSG.NOT_EXISTS_PARAM).setMsg(e.getParameterName()+"参数未找到"); + } + + /** + * POST方法的参数效验异常处理器 + * + * @param e 参数验证异常 + * @return ResponseInfo + */ + @ExceptionHandler(MethodArgumentNotValidException.class) + public R parameterExceptionHandler(MethodArgumentNotValidException e,HttpServletRequest request) { + log.error(request.getMethod()+"-"+request.getRequestURI()+":参数验证失败",e); + // 获取异常信息 + BindingResult exceptions = e.getBindingResult(); + // 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息 + if (exceptions.hasErrors()) { + List errors = exceptions.getAllErrors(); + if (!errors.isEmpty()) { + // 这里列出了全部错误参数,按正常逻辑,只需要第一条错误即可 + FieldError fieldError = (FieldError) errors.get(0); + return R.failure(R.MSG.PARAM_VALIDATION_FAILURE).setMsg(fieldError.getDefaultMessage()); + } + } + return R.failure(R.MSG.PARAM_VALIDATION_FAILURE); + } + + + /** + * 项目运行异常处理 + * @param e 异常 + * @param request 请求体 + * @return 响应体 + */ + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(Exception.class) + public R handleException(Exception e, HttpServletRequest request) { + log.error(request.getMethod()+"-"+request.getRequestURI() + ":服务运行异常",e); + return R.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(),"服务运行异常"); + } + + /** + * 参数验证未通过异常处理 + * @param e 异常 + * @param request 请求体 + * @return 响应体 + */ + @ExceptionHandler(ConstraintViolationException.class) + public R parameterExceptionHandler(ConstraintViolationException e,HttpServletRequest request) { + log.error(request.getMethod()+"-"+request.getRequestURI()+":参数无效",e); + List list= Arrays.asList(e.getMessage().split(",").clone()); + if (!list.isEmpty()){ + return R.failure(R.MSG.PARAM_VALIDATION_FAILURE).setMsg(list.get(0).substring(list.get(0).indexOf(":")+2)); + } + return R.failure(R.MSG.PARAM_VALIDATION_FAILURE); + } + + /** + * 自定义异常处理 + * @param e 异常 + * @param request 请求体对象 + * @return 响应体 + */ + @ResponseStatus(HttpStatus.OK) + @ExceptionHandler({DsException.class}) + public R handleException(DsException e, HttpServletRequest request) { + log.error(request.getMethod()+"-"+request.getRequestURI() + ":自定义内部异常",e); + return R.failure(e); + } +} \ No newline at end of file