如何使用具有不同验证注释的相同表单DTO?如何避免双重码? [英] How to use the same form DTO, with different validation annotations? How to avoid double code?

查看:76
本文介绍了如何使用具有不同验证注释的相同表单DTO?如何避免双重码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有双重代码的情况下,编写DTO并遵循不同的经过验证的注释的最佳实践是什么?
下面附上一个简单的示例,我想避免:

Which is the best practice to write DTO and follow different validated annotations, without double my code? Below attached a simple example, that I want to avoid:

public class AddressForm1 {

    @NotEmpty
    private String address;

    @NotNull
    @Max(23)
    @Min(30)
    private BigDecimal lng;

    // getters & setters
}

和;

public class AddressForm2 {

    // removed annotation, empty value permitted
    private String address;

    @NotNull
    @Max(43)
    @Min(50)
    private BigDecimal lng;

    //getters & setters
}


推荐答案

您可以使用验证组,并分组您的约束。然后使用 @ Validated 批注,并指定了适当的组

You can use validation group, and group your constraints. Than decide which set of constraints you'll apply using the @Validated annotation, with the appropriate group specified

http://www.javacodegeeks.com/2014/08/validation-groups-in -spring-mvc.html

这篇关于如何使用具有不同验证注释的相同表单DTO?如何避免双重码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆