【开发经验】javax.validation包下的校验

63 阅读1分钟

1.java判断同时为空和不为空

@AssertTrue(message = "支付渠道编码[payChannelCode]与授权码[authCode]不能同时存在或同时为空")
@JSONField(serialize = false)
public Boolean getDepositParamIsValid() {
    return !StrUtil.isAllNotBlank(this.payChannelCode, this.authCode) || !StrUtil.isAllBlank(this.payChannelCode, this.authCode);
}

优雅写法:

if ((from == null) != (password == null)) 
{ ... }

可读性写法:

if ((from == null) && (password != null)) 
{ throw new IllegalArgumentException("If from is null, password must be null"); } 

if ((from != null) && (password == null)) 
{ throw new IllegalArgumentException("If from is not null, password must not be null"); }

参考文章:cloud.tencent.com/developer/a…