java 方法参数怎么优雅校验?
本文介绍了java 方法参数怎么优雅校验?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
当我们实现某个接口时,都需要对入参数进行校验。例如下面的代码
public String queryValueByKey(String parmTemplateCode, String conditionName, String conditionKey, String resultName) {
checkNotNull(parmTemplateCode, "parmTemplateCode not null");
checkNotNull(conditionName, "conditionName not null");
checkNotNull(conditionKey, "conditionKey not null");
checkNotNull(resultName, "resultName not null");
}
问题:
我觉得这样还是太繁琐以及冗余,是否可以通过自定义注解的方式校验入参,例如queryValueByKey(@ValidString String parmTemplateCode) 或者其他更简洁优雅的方式。
请教各位高手,有例子的话更好,谢谢!
解决方案
谢谢各位的回答,我觉得此篇博客的方案更符合我的预期。
这篇关于java 方法参数怎么优雅校验?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文