From e8bd583068d16ec58e586567f34e24aa2afadb00 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Sat, 16 Jan 2021 18:13:16 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90api=E3=80=91=E5=AE=8C=E5=96=84api?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9=E6=B3=A8=E8=A7=A3=E7=9A=84=E6=A0=A1=E9=AA=8C=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/pojo/resource/FieldMetadata.java | 7 ++- .../resource/api/util/ClassReflectUtil.java | 5 +- .../modular/factory/ResourceFactory.java | 60 +++++++++++++++++++ .../service/impl/SysResourceServiceImpl.java | 17 ++---- 4 files changed, 74 insertions(+), 15 deletions(-) diff --git a/kernel-d-scanner/scanner-api/src/main/java/cn/stylefeng/roses/kernel/resource/api/pojo/resource/FieldMetadata.java b/kernel-d-scanner/scanner-api/src/main/java/cn/stylefeng/roses/kernel/resource/api/pojo/resource/FieldMetadata.java index 7682f0390..e2813d247 100644 --- a/kernel-d-scanner/scanner-api/src/main/java/cn/stylefeng/roses/kernel/resource/api/pojo/resource/FieldMetadata.java +++ b/kernel-d-scanner/scanner-api/src/main/java/cn/stylefeng/roses/kernel/resource/api/pojo/resource/FieldMetadata.java @@ -45,10 +45,15 @@ public class FieldMetadata { * 按校验组分的注解集合 *

* 例如: - * key = add, value = [NotBlank,TableUniqueValue] + * key = add, value = [不能为空,最大多少位,邮箱类型] */ private Map> groupAnnotations; + /** + * 校验信息的提示信息 + */ + private String validationMessages; + /** * 泛型或object类型的字段的描述 */ diff --git a/kernel-d-scanner/scanner-api/src/main/java/cn/stylefeng/roses/kernel/resource/api/util/ClassReflectUtil.java b/kernel-d-scanner/scanner-api/src/main/java/cn/stylefeng/roses/kernel/resource/api/util/ClassReflectUtil.java index 5bc2134c1..3bab1dc0a 100644 --- a/kernel-d-scanner/scanner-api/src/main/java/cn/stylefeng/roses/kernel/resource/api/util/ClassReflectUtil.java +++ b/kernel-d-scanner/scanner-api/src/main/java/cn/stylefeng/roses/kernel/resource/api/util/ClassReflectUtil.java @@ -132,7 +132,8 @@ public class ClassReflectUtil { if (annotations == null) { annotations = new HashSet<>(); } - annotations.add(fieldAnnotation.annotationType().getSimpleName()); + String messageTip = invokeAnnotationMethodIgnoreError(fieldAnnotation, "message", String.class); + annotations.add(messageTip); groupAnnotations.put(validateGroupsClass.getSimpleName(), annotations); } @@ -159,4 +160,4 @@ public class ClassReflectUtil { return strings; } -} \ No newline at end of file +} diff --git a/kernel-s-system/system-business-resource/src/main/java/cn/stylefeng/roses/kernel/resource/modular/factory/ResourceFactory.java b/kernel-s-system/system-business-resource/src/main/java/cn/stylefeng/roses/kernel/resource/modular/factory/ResourceFactory.java index 98fa63643..939c83721 100644 --- a/kernel-s-system/system-business-resource/src/main/java/cn/stylefeng/roses/kernel/resource/modular/factory/ResourceFactory.java +++ b/kernel-s-system/system-business-resource/src/main/java/cn/stylefeng/roses/kernel/resource/modular/factory/ResourceFactory.java @@ -3,6 +3,8 @@ package cn.stylefeng.roses.kernel.resource.modular.factory; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.copier.CopyOptions; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import cn.stylefeng.roses.kernel.resource.api.pojo.resource.FieldMetadata; import cn.stylefeng.roses.kernel.resource.api.pojo.resource.ResourceDefinition; import cn.stylefeng.roses.kernel.resource.modular.entity.SysResource; import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum; @@ -10,6 +12,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.Feature; import com.alibaba.fastjson.serializer.SerializerFeature; +import java.util.Map; import java.util.Set; /** @@ -107,4 +110,61 @@ public class ResourceFactory { return resourceDefinition; } + /** + * ResourceDefinition转化为api界面的详情信息 + * + * @author fengshuonan + * @date 2021/1/16 16:09 + */ + public static ResourceDefinition fillResourceDetail(ResourceDefinition resourceDefinition) { + + // 这个接口的校验组信息 + Set validateGroups = resourceDefinition.getValidateGroups(); + + // 接口的请求参数信息 + Set paramFieldDescriptions = resourceDefinition.getParamFieldDescriptions(); + fillDetailMessage(validateGroups, paramFieldDescriptions); + + // 接口的响应参数信息 + Set responseFieldDescriptions = resourceDefinition.getResponseFieldDescriptions(); + fillDetailMessage(validateGroups, responseFieldDescriptions); + + return resourceDefinition; + } + + /** + * 填充字段里详细的提示信息 + * + * @author fengshuonan + * @date 2021/1/16 18:00 + */ + public static Set fillDetailMessage(Set validateGroups, Set fieldMetadataSet) { + if (validateGroups == null || validateGroups.isEmpty()) { + return fieldMetadataSet; + } + + if (fieldMetadataSet == null || fieldMetadataSet.isEmpty()) { + return fieldMetadataSet; + } + for (FieldMetadata fieldMetadata : fieldMetadataSet) { + StringBuilder finalValidateMessages = new StringBuilder(); + Map> groupAnnotations = fieldMetadata.getGroupAnnotations(); + if (groupAnnotations != null) { + for (String validateGroup : validateGroups) { + Set validateMessage = groupAnnotations.get(validateGroup); + if (validateMessage != null && !validateMessage.isEmpty()) { + finalValidateMessages.append(StrUtil.join(",", validateMessage)); + } + } + } + fieldMetadata.setValidationMessages(finalValidateMessages.toString()); + + // 递归填充子类型的详细提示信息 + if (fieldMetadata.getGenericFieldMetadata() != null && !fieldMetadata.getGenericFieldMetadata().isEmpty()) { + fillDetailMessage(validateGroups, fieldMetadata.getGenericFieldMetadata()); + } + } + return fieldMetadataSet; + } + } diff --git a/kernel-s-system/system-business-resource/src/main/java/cn/stylefeng/roses/kernel/resource/modular/service/impl/SysResourceServiceImpl.java b/kernel-s-system/system-business-resource/src/main/java/cn/stylefeng/roses/kernel/resource/modular/service/impl/SysResourceServiceImpl.java index ac6e5ddfa..f7a5694a3 100644 --- a/kernel-s-system/system-business-resource/src/main/java/cn/stylefeng/roses/kernel/resource/modular/service/impl/SysResourceServiceImpl.java +++ b/kernel-s-system/system-business-resource/src/main/java/cn/stylefeng/roses/kernel/resource/modular/service/impl/SysResourceServiceImpl.java @@ -27,7 +27,6 @@ import cn.stylefeng.roses.kernel.system.UserServiceApi; import cn.stylefeng.roses.kernel.system.pojo.resource.LayuiApiResourceTreeNode; import cn.stylefeng.roses.kernel.system.pojo.resource.request.ResourceRequest; import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResourceResponse; -import cn.stylefeng.roses.kernel.system.pojo.user.SysUserResponse; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; @@ -121,18 +120,12 @@ public class SysResourceServiceImpl extends ServiceImpl