首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring服务方法和复杂的验证逻辑/规则

Spring服务方法和复杂的验证逻辑/规则
EN

Stack Overflow用户
提问于 2017-02-02 15:11:25
回答 1查看 2.7K关注 0票数 1

在我的Spring/Boot Java项目中,我有一组服务方法,例如如下所示:

代码语言:javascript
复制
@Override
public Decision create(String name, String description, String url, String imageUrl, Decision parentDecision, Tenant tenant, User user) {

    name = StringUtils.trimMultipleSpaces(name);
    if (org.apache.commons.lang3.StringUtils.isEmpty(name)) {
        throw new IllegalArgumentException("Decision name can't be blank");
    }
    if (!org.apache.commons.lang3.StringUtils.isEmpty(url) && !urlValidator.isValid(url)) {
        throw new IllegalArgumentException("Decision url is not valid");
    }
    if (!org.apache.commons.lang3.StringUtils.isEmpty(imageUrl) && !urlValidator.isValid(imageUrl)) {
        throw new IllegalArgumentException("Decision imageUrl is not valid");
    }

    if (user == null) {
        throw new IllegalArgumentException("User can't be empty");
    }

    if (tenant != null) {
        List<Tenant> userTenants = tenantDao.findTenantsForUser(user.getId());
        if (!userTenants.contains(tenant)) {
            throw new IllegalArgumentException("User doesn't belong to this tenant");
        }
    }

    if (parentDecision != null) {
        if (tenant == null) {
            if (findFreeChildDecisionByName(parentDecision.getId(), name) != null) {
                throw new EntityAlreadyExistsException("Parent decision already contains a child decision with a given name");
            }
        } else {
            if (findTenantedChildDecisionByName(parentDecision.getId(), name, tenant.getId()) != null) {
                throw new EntityAlreadyExistsException("Parent decision already contains a child decision with a given name");
            }
        }

        Tenant parentDecisionTenant = tenantDao.findTenantForDecision(parentDecision.getId());
        if (parentDecisionTenant != null) {
            if (tenant == null) {
                throw new IllegalArgumentException("Public decision cannot be added as a child to tenanted parent decision");
            }
            if (!parentDecisionTenant.equals(tenant)) {
                throw new IllegalArgumentException("Decision cannot belong to tenant other than parent decision tenant");
            }
        } else {
            if (tenant != null) {
                throw new IllegalArgumentException("Tenanted decision cannot be added as a child to public parent decision");
            }
        }

    } else {
        if (tenant == null) {
            if (findFreeRootDecisionByName(name) != null) {
                throw new EntityAlreadyExistsException("Root decision with a given name already exists");
            }
        } else {
            if (findTenantedRootDecisionByName(name, tenant.getId()) != null) {
                throw new EntityAlreadyExistsException("Root decision with a given name for this tenant already exists");
            }
        }
    }

    Decision decision = createOrUpdate(new Decision(name, description, url, imageUrl, parentDecision, user, tenant));

    if (parentDecision != null) {
        parentDecision.addChildDecision(decision);
    }

    criterionGroupDao.create(CriterionGroupDaoImpl.DEFAULT_CRITERION_GROUP_NAME, null, decision, user);
    characteristicGroupDao.create(CharacteristicGroupDaoImpl.DEFAULT_CHARACTERISTIC_GROUP_NAME, null, decision, user);

    return decision;
}

正如您所看到的,此方法的大部分代码行都被验证逻辑占用,我继续在其中添加新的验证用例。

我想重构这个方法,并将验证逻辑移到这个方法之外的更合适的地方。请建议如何使用Spring框架来实现。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-02 15:57:19

正如评论中提到的,您可以通过使用JSR-303bean验证来实现此目标。第一步是创建一个包含输入参数的类:

代码语言:javascript
复制
public class DecisionInput {
    private String name;
    private String description;
    private String url;
    private String imageUrl;
    private Decision parentDecision;
    private Tenant tenant;
    private User user;

    // Constructors, getters, setters, ...
}

在此之后,您可以开始添加验证注释,例如:

代码语言:javascript
复制
public class DecisionInput {
    @NotEmpty
    private String name;
    @NotEmpty
    private String description;
    @NotEmpty
    private String url;
    @NotEmpty
    private String imageUrl;
    private Decision parentDecision;
    private Tenant tenant;
    @NotNull
    private User user;

    // Constructors, getters, setters, ...
}

注意,@NotEmpty批注不是标准的JSR-303批注,而是Hibernate批注。如果您更喜欢使用标准JSR-303,您可以随时创建自己的自定义验证器。对于您的租户和您的决策,您肯定需要一个自定义验证器。首先创建一个注解(例如@ValidTenant)。在注释类上,确保添加@Constraint注释,例如:

代码语言:javascript
复制
@Constraint(validatedBy = TenantValidator.class) // Your validator class
@Target({ TYPE, ANNOTATION_TYPE }) // Static import from ElementType, change this to METHOD/FIELD if you want to create a validator for a single field (rather than a cross-field validation)
@Retention(RUNTIME) // Static import from RetentionPolicy
@Documented
public @interface ValidTenant {
    String message() default "{ValidTenant.message}";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default { };
}

现在,您必须创建TenantValidator类并使其实现ConstraintValidator<ValidTenant, DecisionInput>,例如:

代码语言:javascript
复制
@Component
public class TenantValidator implements ConstraintValidator<ValidTenant, DecisionInput> {
    @Autowired
    private TenantDAO tenantDao;

    @Override
    public void initialize(ValidTenant annotation) {
    }

    @Override
    public boolean isValid(DecisionInput input, ConstraintValidatorContext context) {
        List<Tenant> userTenants = tenantDao.findTenantsForUser(input.getUser().getId());
       return userTenants.contains(input.getTenant());
    }
}

对于父决策的验证也可以执行相同的操作。现在,您只需将您的服务方法重构为:

代码语言:javascript
复制
public Decision create(@Valid DecisionInput input) {
    // No more validation logic necessary
}

如果你想使用你自己的错误信息,我建议你阅读this answer。基本上,您创建一个ValidationMessages.properties文件并将您的消息放在那里。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41996143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档