diff --git a/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java b/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java index b95022a7..2990b5eb 100644 --- a/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java +++ b/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java @@ -21,6 +21,7 @@ import cn.hutool.core.text.CharSequenceUtil; import com.fasterxml.jackson.databind.exc.InvalidFormatException; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; @@ -32,13 +33,13 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import org.springframework.web.multipart.MultipartException; import org.springframework.web.servlet.NoHandlerFoundException; +import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.exception.BadRequestException; import top.continew.starter.core.exception.BaseException; import top.continew.starter.core.exception.BusinessException; -import top.continew.starter.core.util.ExceptionUtils; import top.continew.starter.web.model.R; -import java.util.Objects; +import org.springframework.validation.BindException; /** * 全局异常处理器 @@ -96,16 +97,19 @@ public class GlobalExceptionHandler { } /** - * 方法参数无效异常 + * 参数校验不通过异常 *

* {@code @NotBlank}、{@code @NotNull} 等参数验证不通过 *

*/ - @ExceptionHandler(MethodArgumentNotValidException.class) - public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) { + @ExceptionHandler({BindException.class, MethodArgumentNotValidException.class}) + public R handleBindException(BindException e, HttpServletRequest request) { log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e); - String errorMsg = ExceptionUtils.exToNull(() -> Objects.requireNonNull(e.getBindingResult().getFieldError()) - .getDefaultMessage()); + String errorMsg = e.getFieldErrors() + .stream() + .findFirst() + .map(DefaultMessageSourceResolvable::getDefaultMessage) + .orElse(StringConstants.EMPTY); return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), errorMsg); } diff --git a/continew-system/src/main/java/top/continew/admin/system/controller/NoticeController.java b/continew-system/src/main/java/top/continew/admin/system/controller/NoticeController.java index e528d57e..11ad4fb9 100644 --- a/continew-system/src/main/java/top/continew/admin/system/controller/NoticeController.java +++ b/continew-system/src/main/java/top/continew/admin/system/controller/NoticeController.java @@ -21,7 +21,6 @@ import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.bind.annotation.RestController; import top.continew.admin.common.base.controller.BaseController; import top.continew.admin.system.enums.NoticeMethodEnum; -import top.continew.admin.system.enums.NoticeScopeEnum; import top.continew.admin.system.model.query.NoticeQuery; import top.continew.admin.system.model.req.NoticeReq; import top.continew.admin.system.model.resp.notice.NoticeDetailResp; @@ -55,10 +54,6 @@ public class NoticeController extends BaseController noticeMethods = req.getNoticeMethods(); if (CollUtil.isNotEmpty(noticeMethods)) { diff --git a/continew-system/src/main/java/top/continew/admin/system/model/req/NoticeReq.java b/continew-system/src/main/java/top/continew/admin/system/model/req/NoticeReq.java index 57aca118..d5131395 100644 --- a/continew-system/src/main/java/top/continew/admin/system/model/req/NoticeReq.java +++ b/continew-system/src/main/java/top/continew/admin/system/model/req/NoticeReq.java @@ -16,7 +16,11 @@ package top.continew.admin.system.model.req; +import cn.sticki.spel.validator.constrain.SpelNotEmpty; +import cn.sticki.spel.validator.constrain.SpelNotNull; +import cn.sticki.spel.validator.jakarta.SpelValid; import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.Future; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Data; @@ -36,6 +40,7 @@ import java.util.List; * @since 2023/8/20 10:55 */ @Data +@SpelValid @Schema(description = "公告创建或修改请求参数") public class NoticeReq implements Serializable { @@ -76,6 +81,7 @@ public class NoticeReq implements Serializable { * 通知用户 */ @Schema(description = "通知用户", example = "[1,2,3]") + @SpelNotEmpty(condition = "#this.noticeScope == T(top.continew.admin.system.enums.NoticeScopeEnum).USER", message = "通知用户不能为空") private List noticeUsers; /** @@ -87,13 +93,16 @@ public class NoticeReq implements Serializable { /** * 是否定时 */ - @Schema(description = "是否定时", example = "false") + @Schema(description = "是否定时", example = "true") + @NotNull(message = "是否定时不能为空") private Boolean isTiming; /** * 发布时间 */ @Schema(description = "发布时间", example = "2023-08-08 00:00:00", type = "string") + @SpelNotNull(condition = "#this.isTiming == true", message = "定时发布时间不能为空") + @Future(message = "定时发布时间不能早于当前时间") private LocalDateTime publishTime; /** diff --git a/continew-system/src/main/java/top/continew/admin/system/service/impl/NoticeServiceImpl.java b/continew-system/src/main/java/top/continew/admin/system/service/impl/NoticeServiceImpl.java index 307904c9..6f146659 100644 --- a/continew-system/src/main/java/top/continew/admin/system/service/impl/NoticeServiceImpl.java +++ b/continew-system/src/main/java/top/continew/admin/system/service/impl/NoticeServiceImpl.java @@ -36,7 +36,6 @@ import top.continew.admin.system.service.MessageService; import top.continew.admin.system.service.NoticeLogService; import top.continew.admin.system.service.NoticeService; import top.continew.starter.core.util.validation.CheckUtils; -import top.continew.starter.core.util.validation.ValidationUtils; import top.continew.starter.extension.crud.model.query.PageQuery; import top.continew.starter.extension.crud.model.resp.PageResp; @@ -67,11 +66,6 @@ public class NoticeServiceImpl extends BaseServiceImpl { - // 校验定时发布 - if (Boolean.TRUE.equals(req.getIsTiming())) { - ValidationUtils.throwIf(req.getPublishTime() == null, "定时发布时间不能为空"); - ValidationUtils.throwIf(req.getPublishTime().isBefore(LocalDateTime.now()), "定时发布时间不能早于当前时间"); - } // 已发布 if (NoticeStatusEnum.PUBLISHED.equals(req.getStatus())) { if (Boolean.TRUE.equals(req.getIsTiming())) {