javax.validation:用于验证字符串长度的约束(以字节为单位)

时间:2021-06-17 18:35:00

I'm using javax.validation to validate some bean fields' values.

我正在使用javax.validation来验证一些bean字段的值。

This is what I use normally:

这是我通常使用的:

public class Market {

    @NotNull
    @Size(max=4)
    private String marketCode;

    @Digits(integer=4, fraction=0)
    private Integer stalls;

    // getters/setters
}

This will make sure that every Market instance has a market code with a maximum length of 4 characters and a number of stall with a maximum of 4 integer digits and 0 decimal digits.

这将确保每个Market实例都有一个市场代码,其最大长度为4个字符,并且具有最多4个整数位和0个十进制数的停顿数。

Now, I use this bean to load/store data from/to DB.

现在,我使用这个bean从/向DB加载/存储数据。

In the DB I have table Markets defined like this:

在数据库我有表格市场定义如下:

CREATE TABLE MARKETS (
    MARKET_CODE VARCHAR2(4 BYTE) NOT NULL,
    STALLS NUMBER(4,0)
)

As you can see, I have MARKET_CODE which can be at most 4 bytes long. The @Size annotation will check if the string is at most 4 characters long, which is wrong.

如您所见,我有MARKET_CODE,最多可以有4个字节。 @Size注释将检查字符串是否最多4个字符,这是错误的。

So, the question is: is there an annotation like @Size that will check for the string bytes instead of the characters?

所以,问题是:是否有像@Size这样的注释会检查字符串字节而不是字符?

1 个解决方案

#1


6  

Check the Hibernate Validator documentation on Creating custom constraints.

查看有关创建自定义约束的Hibernate Validator文档。

Your validator will need to encode the String into a byte[], using some default or specified Charset. I imagine you might well use UTF-8.

您的验证器需要使用一些默认或指定的Charset将String编码为byte []。我想你可能会使用UTF-8。

Maybe something like this, which uses a hard coded UTF-8 encoding and assumes a suitable annotation, as outlined in the Hibernate documentation linked.

也许这样的东西,它使用硬编码的UTF-8编码并假设一个合适的注释,如Hibernate文档中所述。

public class MaxByteLengthValidator implements ConstraintValidator<MaxByteLength, String> {
    private int max;
    public void initialize(MaxByteLength constraintAnnotation) {
        this.max = constraintAnnotation.value();
    }
    public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
        return object == null || object.getBytes(Charsets.UTF_8).length <= this.max;
    }
}

#1


6  

Check the Hibernate Validator documentation on Creating custom constraints.

查看有关创建自定义约束的Hibernate Validator文档。

Your validator will need to encode the String into a byte[], using some default or specified Charset. I imagine you might well use UTF-8.

您的验证器需要使用一些默认或指定的Charset将String编码为byte []。我想你可能会使用UTF-8。

Maybe something like this, which uses a hard coded UTF-8 encoding and assumes a suitable annotation, as outlined in the Hibernate documentation linked.

也许这样的东西,它使用硬编码的UTF-8编码并假设一个合适的注释,如Hibernate文档中所述。

public class MaxByteLengthValidator implements ConstraintValidator<MaxByteLength, String> {
    private int max;
    public void initialize(MaxByteLength constraintAnnotation) {
        this.max = constraintAnnotation.value();
    }
    public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
        return object == null || object.getBytes(Charsets.UTF_8).length <= this.max;
    }
}