mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
feat(extension/crud): CrudService 增加 Valid 校验注解,Controller 层使用 Valid 替换部分 Validated 注解
在 Service 层实现类添加 Validated 注解即可开启 Service 层校验
This commit is contained in:
@@ -22,6 +22,7 @@ import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -65,7 +66,7 @@ public abstract class AbstractCrudController<S extends CrudService<L, D, Q, C>,
|
||||
@Operation(summary = "分页查询列表", description = "分页查询列表")
|
||||
@ResponseBody
|
||||
@GetMapping
|
||||
public BasePageResp<L> page(@Validated Q query, @Validated PageQuery pageQuery) {
|
||||
public BasePageResp<L> page(@Valid Q query, @Valid PageQuery pageQuery) {
|
||||
return baseService.page(query, pageQuery);
|
||||
}
|
||||
|
||||
@@ -80,7 +81,7 @@ public abstract class AbstractCrudController<S extends CrudService<L, D, Q, C>,
|
||||
@Operation(summary = "查询列表", description = "查询列表")
|
||||
@ResponseBody
|
||||
@GetMapping("/list")
|
||||
public List<L> list(@Validated Q query, @Validated SortQuery sortQuery) {
|
||||
public List<L> list(@Valid Q query, @Valid SortQuery sortQuery) {
|
||||
return baseService.list(query, sortQuery);
|
||||
}
|
||||
|
||||
@@ -95,7 +96,7 @@ public abstract class AbstractCrudController<S extends CrudService<L, D, Q, C>,
|
||||
@Operation(summary = "查询树列表", description = "查询树列表")
|
||||
@ResponseBody
|
||||
@GetMapping("/tree")
|
||||
public List<Tree<Long>> tree(@Validated Q query, @Validated SortQuery sortQuery) {
|
||||
public List<Tree<Long>> tree(@Valid Q query, @Valid SortQuery sortQuery) {
|
||||
return baseService.tree(query, sortQuery, false);
|
||||
}
|
||||
|
||||
@@ -124,7 +125,8 @@ public abstract class AbstractCrudController<S extends CrudService<L, D, Q, C>,
|
||||
@Operation(summary = "创建数据", description = "创建数据")
|
||||
@ResponseBody
|
||||
@PostMapping
|
||||
public IdResp<Long> create(@Validated(CrudValidationGroup.Create.class) @RequestBody C req) {
|
||||
@Validated(CrudValidationGroup.Create.class)
|
||||
public IdResp<Long> create(@RequestBody @Valid C req) {
|
||||
return new IdResp<>(baseService.create(req));
|
||||
}
|
||||
|
||||
@@ -139,7 +141,8 @@ public abstract class AbstractCrudController<S extends CrudService<L, D, Q, C>,
|
||||
@Parameter(name = "id", description = "ID", example = "1", in = ParameterIn.PATH)
|
||||
@ResponseBody
|
||||
@PutMapping("/{id}")
|
||||
public void update(@Validated(CrudValidationGroup.Update.class) @RequestBody C req, @PathVariable("id") Long id) {
|
||||
@Validated(CrudValidationGroup.Update.class)
|
||||
public void update(@RequestBody @Valid C req, @PathVariable("id") Long id) {
|
||||
baseService.update(req, id);
|
||||
}
|
||||
|
||||
@@ -166,7 +169,7 @@ public abstract class AbstractCrudController<S extends CrudService<L, D, Q, C>,
|
||||
@Operation(summary = "批量删除数据", description = "批量删除数据")
|
||||
@ResponseBody
|
||||
@DeleteMapping
|
||||
public void batchDelete(@Validated @RequestBody IdsReq req) {
|
||||
public void batchDelete(@RequestBody @Valid IdsReq req) {
|
||||
baseService.delete(req.getIds());
|
||||
}
|
||||
|
||||
@@ -181,7 +184,7 @@ public abstract class AbstractCrudController<S extends CrudService<L, D, Q, C>,
|
||||
@ExcludeFromGracefulResponse
|
||||
@Operation(summary = "导出数据", description = "导出数据")
|
||||
@GetMapping("/export")
|
||||
public void export(@Validated Q query, @Validated SortQuery sortQuery, HttpServletResponse response) {
|
||||
public void export(@Valid Q query, @Valid SortQuery sortQuery, HttpServletResponse response) {
|
||||
baseService.export(query, sortQuery, response);
|
||||
}
|
||||
}
|
||||
|
@@ -18,10 +18,14 @@ package top.continew.starter.extension.crud.service;
|
||||
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.extension.crud.model.query.SortQuery;
|
||||
import top.continew.starter.extension.crud.model.resp.BasePageResp;
|
||||
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
|
||||
import top.continew.starter.extension.crud.validation.CrudValidationGroup;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -44,7 +48,7 @@ public interface CrudService<L, D, Q, C> {
|
||||
* @param pageQuery 分页查询条件
|
||||
* @return 分页列表信息
|
||||
*/
|
||||
BasePageResp<L> page(Q query, PageQuery pageQuery);
|
||||
BasePageResp<L> page(@Valid Q query, @Valid PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
@@ -53,7 +57,7 @@ public interface CrudService<L, D, Q, C> {
|
||||
* @param sortQuery 排序查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
List<L> list(Q query, SortQuery sortQuery);
|
||||
List<L> list(@Valid Q query, @Valid SortQuery sortQuery);
|
||||
|
||||
/**
|
||||
* 查询树列表
|
||||
@@ -67,7 +71,7 @@ public interface CrudService<L, D, Q, C> {
|
||||
* @param isSimple 是否为简单树结构(不包含基本树结构之外的扩展字段,简单树(下拉列表)使用全局配置结构,复杂树(表格)使用 @DictField 局部配置)
|
||||
* @return 树列表信息
|
||||
*/
|
||||
List<Tree<Long>> tree(Q query, SortQuery sortQuery, boolean isSimple);
|
||||
List<Tree<Long>> tree(@Valid Q query, @Valid SortQuery sortQuery, boolean isSimple);
|
||||
|
||||
/**
|
||||
* 查询详情
|
||||
@@ -85,7 +89,7 @@ public interface CrudService<L, D, Q, C> {
|
||||
* @return 字典列表信息
|
||||
* @since 2.1.0
|
||||
*/
|
||||
List<LabelValueResp> listDict(Q query, SortQuery sortQuery);
|
||||
List<LabelValueResp> listDict(@Valid Q query, @Valid SortQuery sortQuery);
|
||||
|
||||
/**
|
||||
* 创建
|
||||
@@ -93,7 +97,8 @@ public interface CrudService<L, D, Q, C> {
|
||||
* @param req 创建请求参数
|
||||
* @return 自增 ID
|
||||
*/
|
||||
Long create(C req);
|
||||
@Validated(CrudValidationGroup.Create.class)
|
||||
Long create(@Valid C req);
|
||||
|
||||
/**
|
||||
* 修改
|
||||
@@ -101,14 +106,15 @@ public interface CrudService<L, D, Q, C> {
|
||||
* @param req 修改请求参数
|
||||
* @param id ID
|
||||
*/
|
||||
void update(C req, Long id);
|
||||
@Validated(CrudValidationGroup.Update.class)
|
||||
void update(@Valid C req, Long id);
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param ids ID 列表
|
||||
*/
|
||||
void delete(List<Long> ids);
|
||||
void delete(@NotEmpty(message = "ID 不能为空") List<Long> ids);
|
||||
|
||||
/**
|
||||
* 导出
|
||||
@@ -117,5 +123,5 @@ public interface CrudService<L, D, Q, C> {
|
||||
* @param sortQuery 排序查询条件
|
||||
* @param response 响应对象
|
||||
*/
|
||||
void export(Q query, SortQuery sortQuery, HttpServletResponse response);
|
||||
void export(@Valid Q query, @Valid SortQuery sortQuery, HttpServletResponse response);
|
||||
}
|
||||
|
Reference in New Issue
Block a user