@size(max = value)和@min(value)@max(value)之间的差异

时间:2022-09-27 08:51:20

I want ot do some domain validation

我想要做一些域验证

in my object i am having one integer ,

在我的对象中,我有一个整数,

now my question is if i write

现在我的问题是如果我写

@Min(SEQ_MIN_VALUE)
@Max(SEQ_MAX_VALUE)
private Integer sequence;

and

 @Size(min = 1, max = NAME_MAX_LENGTH)
 private Integer sequence;

If it's integer which one is proper for domain validation.

如果是整数哪一个适合域验证。

can anybody explain me what is the difference between them ?

任何人都可以解释一下他们之间的区别是什么?

Thanks.

谢谢。

2 个解决方案

#1


73  

@Min and @Max are used for validating numeric fields which could be String(representing number), int, short, byte etc and their respective primitive wrappers.

@Min和@Max用于验证数字字段,这些字段可以是String(表示数字),int,short,byte等以及它们各自的原始包装器。

@Size is used to check the length constraints on the fields.

@Size用于检查字段的长度限制。

As per documentation @Size supports String, Collection, Map and arrays while @Min and @Max supports primitives and their wrappers. See the documentation.

根据文档,@ Size支持String,Collection,Map和数组,而@Min和@Max支持原语及其包装器。请参阅文档。

#2


12  

package com.mycompany;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Car {

    @NotNull
    private String manufacturer;

    @NotNull
    @Size(min = 2, max = 14)
    private String licensePlate;

    @Min(2)
    private int seatCount;

    public Car(String manufacturer, String licencePlate, int seatCount) {
        this.manufacturer = manufacturer;
        this.licensePlate = licencePlate;
        this.seatCount = seatCount;
    }

    //getters and setters ...
}

@NotNull, @Size and @Min are so-called constraint annotations, that we use to declare constraints, which shall be applied to the fields of a Car instance:

@NotNull,@ Size和@Min是所谓的约束注释,我们用它来声明约束,这些约束应该应用于Car实例的字段:

manufacturer shall never be null

制造商永远不会为空

licensePlate shall never be null and must be between 2 and 14 characters long

licensePlate永远不应为null,且长度必须介于2到14个字符之间

seatCount shall be at least 2.

seatCount应至少为2。

#1


73  

@Min and @Max are used for validating numeric fields which could be String(representing number), int, short, byte etc and their respective primitive wrappers.

@Min和@Max用于验证数字字段,这些字段可以是String(表示数字),int,short,byte等以及它们各自的原始包装器。

@Size is used to check the length constraints on the fields.

@Size用于检查字段的长度限制。

As per documentation @Size supports String, Collection, Map and arrays while @Min and @Max supports primitives and their wrappers. See the documentation.

根据文档,@ Size支持String,Collection,Map和数组,而@Min和@Max支持原语及其包装器。请参阅文档。

#2


12  

package com.mycompany;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Car {

    @NotNull
    private String manufacturer;

    @NotNull
    @Size(min = 2, max = 14)
    private String licensePlate;

    @Min(2)
    private int seatCount;

    public Car(String manufacturer, String licencePlate, int seatCount) {
        this.manufacturer = manufacturer;
        this.licensePlate = licencePlate;
        this.seatCount = seatCount;
    }

    //getters and setters ...
}

@NotNull, @Size and @Min are so-called constraint annotations, that we use to declare constraints, which shall be applied to the fields of a Car instance:

@NotNull,@ Size和@Min是所谓的约束注释,我们用它来声明约束,这些约束应该应用于Car实例的字段:

manufacturer shall never be null

制造商永远不会为空

licensePlate shall never be null and must be between 2 and 14 characters long

licensePlate永远不应为null,且长度必须介于2到14个字符之间

seatCount shall be at least 2.

seatCount应至少为2。