Do valid annotation verification in the springboot project, but what should I do if the object is a DTO class that inherits pojo (bean)?

  1. Add the validation annotation directly to the field of the DTO class instead of adding it to the parent class. Modify the field declaration of the DTO class, and add validation annotations to the field. For example
@Data
@TableName("t_exam_oral")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "ExamOral对象", description = "口腔检测结果表")
public class ExamOral extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 学生id
     */
    @ApiModelProperty(value = "学生id")
    private Long personId;
    /**
     * 设备id
     */
	//注解校验设备id不为空


    @ApiModelProperty(value = "设备id")
    private Long deviceId;
    /**
     * 学生姓名
     */
    @ApiModelProperty(value = "学生姓名")
    private String personName;
    /**
     * 检测批次
     */
    @ApiModelProperty(value = "检测批次")
    private Long batchId;
    /**
     * 学校id
     */
    @ApiModelProperty(value = "学校id")
    private Long schoolId;
    /**
     * 年级
     */
    @ApiModelProperty(value = "年级")
    private Integer grade;
    /**
     * 班级
     */
    @ApiModelProperty(value = "班级")
    private Integer clazz;
    /**
     * 检查人员
     */
    @ApiModelProperty(value = "检查人员")
    private Long checkerId;
    /**
     * 检查人员名称
     */
    @ApiModelProperty(value = "检查人员名称")
    private String checkerName;
    /**
     * 检测序号
     */
    @ApiModelProperty(value = "检测序号")
    private String checkNo;
    /**
     * 检测时间
     */
    @ApiModelProperty(value = "检测时间")
    private LocalDateTime checkTime;
    /**
     * 原始图片链接1;牙齿正面+白光 01
     */
    @ApiModelProperty(value = "原始图片链接1;牙齿正面+白光 01")
    private String originalImage01;
    /**
     * 原始图片链接2;牙齿正面+紫光 02
     */
    @ApiModelProperty(value = "原始图片链接2;牙齿正面+紫光 02")
    private String originalImage02;
    /**
     * 原始图片链接3;牙齿上+白光 03
     */
    @ApiModelProperty(value = "原始图片链接3;牙齿上+白光 03")
    private String originalImage03;
    /**
     * 原始图片链接4;牙齿下+白光 04
     */
    @ApiModelProperty(value = "原始图片链接4;牙齿下+白光 04")
    private String originalImage04;
    /**
     * 原始图片链接5;牙齿上+紫光 05
     */
    @ApiModelProperty(value = "原始图片链接5;牙齿上+紫光 05")
    private String originalImage05;
    /**
     * 原始图片链接6;牙齿下+紫光 06
     */
    @ApiModelProperty(value = "原始图片链接6;牙齿下+紫光 06")
    private String originalImage06;
    /**
     * 处理后的图片链接1;算法处理后牙齿正面+白光 01
     */
    @ApiModelProperty(value = "处理后的图片链接1;算法处理后牙齿正面+白光 01")
    private String processedImage01;
    /**
     * 处理后的图片链接2;算法处理后牙齿正面+紫光 02
     */
    @ApiModelProperty(value = "处理后的图片链接2;算法处理后牙齿正面+紫光 02")
    private String processedImage02;
    /**
     * 处理后的图片链接3;算法处理后牙齿上+白光 03
     */
    @ApiModelProperty(value = "处理后的图片链接3;算法处理后牙齿上+白光 03")
    private String processedImage03;
    /**
     * 处理后的图片链接4;算法处理后牙齿下+白光 04
     */
    @ApiModelProperty(value = "处理后的图片链接4;算法处理后牙齿下+白光 04")
    private String processedImage04;
    /**
     * 处理后的图片链接5;算法处理后牙齿上+紫光 05
     */
    @ApiModelProperty(value = "处理后的图片链接5;算法处理后牙齿上+紫光 05")
    private String processedImage05;
    /**
     * 处理后的图片链接6;算法处理后牙齿下+紫光 06
     */
    @ApiModelProperty(value = "处理后的图片链接6;算法处理后牙齿下+紫光 06")
    private String processedImage06;
    /**
     * 牙齿类型
     */
    @ApiModelProperty(value = "牙齿类型")
    private String toothType;
    /**
     * 治疗建议
     */
    @ApiModelProperty(value = "治疗建议")
    private String treatmentSuggestion;
    /**
     * 备注
     */
    @ApiModelProperty(value = "备注")
    private String remarks;


}

 

The DTO object is cast into a POJO object, and the fields of the POJO object correspond to the fields of the DTO object, and the verification on the DTO object has passed, then after the cast, the field information should be correctly assigned to the POJO object .

When performing mandatory conversion, it will match according to the field name and type, and assign the field value in the DTO object to the corresponding POJO object field. As long as the field names and types match, field information should not be lost.

However, please note the following:

  1. There are risks in mandatory type conversion. If the field structures between DTO objects and POJO objects are not exactly the same, or the field types are incompatible, the mandatory conversion will fail and a ClassCastException will be thrown.

  2. If additional fields are added to the DTO object, these additional fields will be ignored during the cast and will not be assigned to the POJO object.

So, if you ensure that the fields of the DTO object exactly match the field names and types of the POJO object, and you need to store the DTO data in the database, then casting may be a convenient way. But be sure to double-check the consistency of the fields before doing the conversion to avoid type mismatch issues.

Guess you like

Origin blog.csdn.net/weixin_42759398/article/details/131516336