Valid 校验嵌套对象失效
例子
package com.hhjt.firefight.params;
import com.hhjt.firefight.entity.FireFightPlanResourceEntity;
import com.hhjt.firefight.entity.PlanTestResourceEntity;
import com.hhjt.firefight.entity.PlanTestTeamEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;
/**
* @author pengyifeng
* @version 2.0
* @ClassName TestPlanParam.java
* @Description 扑火预案
* @createTime 2021年08月24日 16:01:00
*/
@Data
public class TestPlanParam implements Serializable {
/**
* id
*/
@ApiModelProperty(value = "id为空新增,否则修改")
private Integer id;
/**
* 方案名称
*/
@ApiModelProperty(value = "方案名称")
@NotBlank(message = "方案名称不能为空")
@Size(max = 50, message = "方案名称超长")
private String planName;
/**
* 火灾地点
*/
@ApiModelProperty(value = "火灾地点")
@NotBlank(message = "火灾地点不能为空")
@Size(max = 100, message = "火灾地点")
private String fireAddr;
/**
* 林区面积
*/
@ApiModelProperty(value = "林区面积")
@NotBlank(message = "林区面积不能为空")
@Size(max = 100, message = "林区面积超过上限")
private String forestArea;
/**
* 指挥长
*/
@ApiModelProperty(value = "指挥长")
@NotBlank(message = "指挥长不能为空")
@Size(max = 100, message = "指挥长超长")
private String commander;
/**
* 副指挥长
*/
@ApiModelProperty(value = "副指挥长")
@Size(max = 100, message = "副指挥长超长")
private String deputyCommander;
/**
* 指挥部成员
*/
@ApiModelProperty(value = "指挥部成员")
@Size(max = 255, message = "指挥部成员超长")
private String commandMember;
/**
* 指挥部位置
*/
@ApiModelProperty(value = "指挥部位置")
@Size(max = 100, message = "指挥部位置超长")
private String commandLocation;
/**
* 方案详情
*/
@ApiModelProperty(value = "方案详情")
private String planDetail;
/**
* 扑火队伍 该对象内的属性无法校验
*/
@ApiModelProperty(value = "扑火队伍")
@NotNull(message = "扑火队伍不能为空")
private List<PlanTestTeamEntity> teams;
/**
* 扑火物资 该对象内的属性无法校验
*/
@ApiModelProperty(value = "扑火队伍")
@NotNull(message = "扑火物资不能为空")
private List<PlanTestResourceEntity> resources;
}
嵌套对象需新增@Valid注释
package com.hhjt.firefight.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
/**
* 扑火预案-队伍配置
*
* @author pengyifeng
* @email pengyifeng@aliyun.com
* @date 2021-08-24 14:44:43
*/
@Data
@TableName("fire_fight_plan_test_team")
public class PlanTestTeamEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
*id
*/
@ApiModelProperty(value = "id为空新增,否则修改")
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 预案id
*/
@ApiModelProperty(value = "预案id")
private Integer fireFightTestPlanId;
/**
* 队伍id 新增@Valid注释
*/
@Valid
@NotNull(message = "teamId不能为空")
@ApiModelProperty(value = "队伍id")
private Integer teamId;
/**
* 队伍人数 新增@Valid注释
*/
@Valid
@NotNull(message = "队伍人数不能为空")
@ApiModelProperty(value = "队伍人数")
private Integer teamMemNum;
}
接口 参数新增@Valid
/**
* 保存、更新
*/
@ApiOperation(value = "保存、更新")
@PostMapping("/saveOrUpdate")
public BaseResp save(@RequestBody @Valid TestPlanParam param){
BaseResp resp=testPlanService.saveOrUpdate(param);
return resp;
}
@Valid 校验嵌套对象