Skip to content

Commit 0d3c1bb

Browse files
committed
refactor: 使用 SpEL Validator 优化部分校验场景
1 parent acfdfce commit 0d3c1bb

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
2222
import jakarta.servlet.http.HttpServletRequest;
2323
import lombok.extern.slf4j.Slf4j;
24+
import org.springframework.context.support.DefaultMessageSourceResolvable;
2425
import org.springframework.core.annotation.Order;
2526
import org.springframework.http.HttpStatus;
2627
import org.springframework.http.converter.HttpMessageNotReadableException;
@@ -32,13 +33,13 @@
3233
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
3334
import org.springframework.web.multipart.MultipartException;
3435
import org.springframework.web.servlet.NoHandlerFoundException;
36+
import top.continew.starter.core.constant.StringConstants;
3537
import top.continew.starter.core.exception.BadRequestException;
3638
import top.continew.starter.core.exception.BaseException;
3739
import top.continew.starter.core.exception.BusinessException;
38-
import top.continew.starter.core.util.ExceptionUtils;
3940
import top.continew.starter.web.model.R;
4041

41-
import java.util.Objects;
42+
import org.springframework.validation.BindException;
4243

4344
/**
4445
* 全局异常处理器
@@ -96,16 +97,19 @@ public R handleMethodArgumentTypeMismatchException(MissingServletRequestParamete
9697
}
9798

9899
/**
99-
* 方法参数无效异常
100+
* 参数校验不通过异常
100101
* <p>
101102
* {@code @NotBlank}、{@code @NotNull} 等参数验证不通过
102103
* </p>
103104
*/
104-
@ExceptionHandler(MethodArgumentNotValidException.class)
105-
public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
105+
@ExceptionHandler({BindException.class, MethodArgumentNotValidException.class})
106+
public R handleBindException(BindException e, HttpServletRequest request) {
106107
log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e);
107-
String errorMsg = ExceptionUtils.exToNull(() -> Objects.requireNonNull(e.getBindingResult().getFieldError())
108-
.getDefaultMessage());
108+
String errorMsg = e.getFieldErrors()
109+
.stream()
110+
.findFirst()
111+
.map(DefaultMessageSourceResolvable::getDefaultMessage)
112+
.orElse(StringConstants.EMPTY);
109113
return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), errorMsg);
110114
}
111115

continew-system/src/main/java/top/continew/admin/system/controller/NoticeController.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.springframework.web.bind.annotation.RestController;
2222
import top.continew.admin.common.base.controller.BaseController;
2323
import top.continew.admin.system.enums.NoticeMethodEnum;
24-
import top.continew.admin.system.enums.NoticeScopeEnum;
2524
import top.continew.admin.system.model.query.NoticeQuery;
2625
import top.continew.admin.system.model.req.NoticeReq;
2726
import top.continew.admin.system.model.resp.notice.NoticeDetailResp;
@@ -55,10 +54,6 @@ public void preHandle(CrudApi crudApi, Object[] args, Method targetMethod, Class
5554
return;
5655
}
5756
NoticeReq req = (NoticeReq)args[0];
58-
// 校验通知范围
59-
if (NoticeScopeEnum.USER.equals(req.getNoticeScope())) {
60-
ValidationUtils.throwIfEmpty(req.getNoticeUsers(), "通知用户不能为空");
61-
}
6257
// 校验通知方式
6358
List<Integer> noticeMethods = req.getNoticeMethods();
6459
if (CollUtil.isNotEmpty(noticeMethods)) {

continew-system/src/main/java/top/continew/admin/system/model/req/NoticeReq.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package top.continew.admin.system.model.req;
1818

19+
import cn.sticki.spel.validator.constrain.SpelNotEmpty;
20+
import cn.sticki.spel.validator.constrain.SpelNotNull;
21+
import cn.sticki.spel.validator.jakarta.SpelValid;
1922
import io.swagger.v3.oas.annotations.media.Schema;
23+
import jakarta.validation.constraints.Future;
2024
import jakarta.validation.constraints.NotBlank;
2125
import jakarta.validation.constraints.NotNull;
2226
import lombok.Data;
@@ -36,6 +40,7 @@
3640
* @since 2023/8/20 10:55
3741
*/
3842
@Data
43+
@SpelValid
3944
@Schema(description = "公告创建或修改请求参数")
4045
public class NoticeReq implements Serializable {
4146

@@ -76,6 +81,7 @@ public class NoticeReq implements Serializable {
7681
* 通知用户
7782
*/
7883
@Schema(description = "通知用户", example = "[1,2,3]")
84+
@SpelNotEmpty(condition = "#this.noticeScope == T(top.continew.admin.system.enums.NoticeScopeEnum).USER", message = "通知用户不能为空")
7985
private List<String> noticeUsers;
8086

8187
/**
@@ -87,13 +93,16 @@ public class NoticeReq implements Serializable {
8793
/**
8894
* 是否定时
8995
*/
90-
@Schema(description = "是否定时", example = "false")
96+
@Schema(description = "是否定时", example = "true")
97+
@NotNull(message = "是否定时不能为空")
9198
private Boolean isTiming;
9299

93100
/**
94101
* 发布时间
95102
*/
96103
@Schema(description = "发布时间", example = "2023-08-08 00:00:00", type = "string")
104+
@SpelNotNull(condition = "#this.isTiming == true", message = "定时发布时间不能为空")
105+
@Future(message = "定时发布时间不能早于当前时间")
97106
private LocalDateTime publishTime;
98107

99108
/**

continew-system/src/main/java/top/continew/admin/system/service/impl/NoticeServiceImpl.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import top.continew.admin.system.service.NoticeLogService;
3737
import top.continew.admin.system.service.NoticeService;
3838
import top.continew.starter.core.util.validation.CheckUtils;
39-
import top.continew.starter.core.util.validation.ValidationUtils;
4039
import top.continew.starter.extension.crud.model.query.PageQuery;
4140
import top.continew.starter.extension.crud.model.resp.PageResp;
4241

@@ -67,11 +66,6 @@ public PageResp<NoticeResp> page(NoticeQuery query, PageQuery pageQuery) {
6766

6867
@Override
6968
public void beforeCreate(NoticeReq req) {
70-
// 校验定时发布
71-
if (Boolean.TRUE.equals(req.getIsTiming())) {
72-
ValidationUtils.throwIf(req.getPublishTime() == null, "定时发布时间不能为空");
73-
ValidationUtils.throwIf(req.getPublishTime().isBefore(LocalDateTime.now()), "定时发布时间不能早于当前时间");
74-
}
7569
if (!NoticeStatusEnum.DRAFT.equals(req.getStatus())) {
7670
if (Boolean.TRUE.equals(req.getIsTiming())) {
7771
// 待发布
@@ -113,11 +107,6 @@ public void beforeUpdate(NoticeReq req, Long id) {
113107
req.setPublishTime(oldNotice.getPublishTime());
114108
}
115109
case DRAFT, PENDING -> {
116-
// 校验定时发布
117-
if (Boolean.TRUE.equals(req.getIsTiming())) {
118-
ValidationUtils.throwIf(req.getPublishTime() == null, "定时发布时间不能为空");
119-
ValidationUtils.throwIf(req.getPublishTime().isBefore(LocalDateTime.now()), "定时发布时间不能早于当前时间");
120-
}
121110
// 已发布
122111
if (NoticeStatusEnum.PUBLISHED.equals(req.getStatus())) {
123112
if (Boolean.TRUE.equals(req.getIsTiming())) {

0 commit comments

Comments
 (0)