【api】完善api详情接口,增加对注解的校验提示

pull/3/head
fengshuonan 2021-01-16 18:13:16 +08:00
parent fead9657d1
commit e8bd583068
4 changed files with 74 additions and 15 deletions

View File

@ -45,10 +45,15 @@ public class FieldMetadata {
*
* <p>
*
* key = add, value = [NotBlank,TableUniqueValue]
* key = add, value = []
*/
private Map<String, Set<String>> groupAnnotations;
/**
*
*/
private String validationMessages;
/**
* object
*/

View File

@ -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;
}
}
}

View File

@ -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;
}
/**
* ResourceDefinitionapi
*
* @author fengshuonan
* @date 2021/1/16 16:09
*/
public static ResourceDefinition fillResourceDetail(ResourceDefinition resourceDefinition) {
// 这个接口的校验组信息
Set<String> validateGroups = resourceDefinition.getValidateGroups();
// 接口的请求参数信息
Set<FieldMetadata> paramFieldDescriptions = resourceDefinition.getParamFieldDescriptions();
fillDetailMessage(validateGroups, paramFieldDescriptions);
// 接口的响应参数信息
Set<FieldMetadata> responseFieldDescriptions = resourceDefinition.getResponseFieldDescriptions();
fillDetailMessage(validateGroups, responseFieldDescriptions);
return resourceDefinition;
}
/**
*
*
* @author fengshuonan
* @date 2021/1/16 18:00
*/
public static Set<FieldMetadata> fillDetailMessage(Set<String> validateGroups, Set<FieldMetadata> fieldMetadataSet) {
if (validateGroups == null || validateGroups.isEmpty()) {
return fieldMetadataSet;
}
if (fieldMetadataSet == null || fieldMetadataSet.isEmpty()) {
return fieldMetadataSet;
}
for (FieldMetadata fieldMetadata : fieldMetadataSet) {
StringBuilder finalValidateMessages = new StringBuilder();
Map<String, Set<String>> groupAnnotations = fieldMetadata.getGroupAnnotations();
if (groupAnnotations != null) {
for (String validateGroup : validateGroups) {
Set<String> 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;
}
}

View File

@ -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<SysResourceMapper, SysRe
sysResourceLambdaQueryWrapper.eq(SysResource::getResourceCode, resourceRequest.getResourceCode());
SysResource sysResource = this.getOne(sysResourceLambdaQueryWrapper);
if (sysResource != null) {
ResourceDefinition definition = ResourceFactory.createResourceDefinition(sysResource);
// 翻译创建人
if (sysResource.getCreateUser().equals(RuleConstants.TREE_ROOT_ID)) {
definition.setCreateUser("超级管理员");
} else {
SysUserResponse userInfo = userServiceApi.getUserInfoByUserId(sysResource.getCreateUser());
if (ObjectUtil.isNotEmpty(userInfo)) {
definition.setCreateUser(userInfo.getRealName());
}
}
return definition;
// 实体转化为ResourceDefinition
ResourceDefinition resourceDefinition = ResourceFactory.createResourceDefinition(sysResource);
// 填充具体的提示信息
return ResourceFactory.fillResourceDetail(resourceDefinition);
} else {
return null;
}