这篇文章主要介绍了如何解决@NotBlank不生效的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教。
1.解决@NotBlank不生效
最近做一个新项目,写新接口时发现@NotBlank @NotEmpty不生效,@NotNull则生效,令人费解。仔细检查一下2步骤
用法:
step1、Controller层接口入参处加@Validated注解(org.springframework.validation.annotation包的)
step2、入参对象的属性上加@NotBlank 或@NotEmpty 或 @NotNull --均来自于javax.validation.constraints包下, 三者区别可百度
属性循环嵌套问题,入参对象bean中 属性也为javabean对象,也希望子对象中的属性能够被校验
用法: 在子对象属性上面加@Valid 注解即可
WxCollectUserinfo.java
package com.joolun.system.domain;
import com.joolun.common.annotation.Excel;
import com.joolun.common.core.domain.BaseEntity;
import com.joolun.system.domain.vo.UserVo;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
* 门店信息对象 wx_collect_userinfo
*
* @author qiaosl
* @date 2022-10-03
*/
public class WxCollectUserinfo extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 主键 */
@Excel(name = "主键")
private Long wcuId;
@NotNull(message = "userVo不能为空")
@Valid
private UserVo userVo;
/** 门店名称 */
@NotBlank(message = "门店名称不能为空")
@Excel(name = "门店名称")
private String storesName;
/** 用户名称 */
@NotBlank(message = "姓名不能为空")
@Excel(name = "用户名称")
private String userName;
/** 门店地址 */
@NotBlank(message = "门店地址不能为空")
@Excel(name = "门店地址")
private String storesAddress;
/** 手机号码 */
@Excel(name = "手机号码")
@NotBlank(message = "手机号码不能为空")
private String userTelnumber;
public void setWcuId(Long wcuId)
{
this.wcuId = wcuId;
}
public Long getWcuId()
{
return wcuId;
}
public void setStoresName(String storesName)
{
this.storesName = storesName;
}
public String getStoresName()
{
return storesName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getUserName()
{
return userName;
}
public void setStoresAddress(String storesAddress)
{
this.storesAddress = storesAddress;
}
public String getStoresAddress()
{
return storesAddress;
}
public void setUserTelnumber(String userTelnumber)
{
this.userTelnumber = userTelnumber;
}
public String getUserTelnumber()
{
return userTelnumber;
}
public UserVo getUserVo() {
return userVo;
}
public void setUserVo(UserVo userVo) {
this.userVo = userVo;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("wcuId", getWcuId())
.append("storesName", getStoresName())
.append("userName", getUserName())
.append("storesAddress", getStoresAddress())
.append("userTelnumber", getUserTelnumber())
.append("createBy", getCreateBy())
.append("createTime", getCreateTime())
.append("updateBy", getUpdateBy())
.append("updateTime", getUpdateTime())
.append("remark", getRemark())
.toString();
}
}
UserVo.java
package com.joolun.system.domain.vo;
import javax.validation.constraints.NotBlank;
public class UserVo {
@NotBlank(message = "title不能为空")
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}