javax.validation.constraints.Pattern验证类型类java.lang.String []

时间:2018-06-13 15:52:32

标签: java bean-validation

我尝试为String[]创建验证,如下所示,但我得到了例外。

  

11:28:02,832 WARN [http-nio-8080-exec-1] internal.ValidationExceptionMapper(ValidationExceptionMapper.java:119) - 意外的Bean验证问题。   javax.validation.UnexpectedTypeException:HV000030:找不到约束的验证器' javax.validation.constraints.Pattern'验证类型'类java.lang.String []'。检查配置

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

/**
 * Created by vagrant on 3/16/18.
 */
public class ShareClassSearchRequest extends BaseSearchRequest
{
    private static final long serialVersionUID = 1L;

    @Valid
    private @Pattern(regexp = InputValidationPattern.REGULAR_TEXT)String[] legalNames;

    @Valid
    private @Pattern(regexp = InputValidationPattern.INTEGER)String[] investmentProductIds;

    public String[] getLegalNames()
    {
        return legalNames;
    }

    public void setLegalNames(String[] legalNames)
    {
        this.legalNames = legalNames;
    }

    public String[] getInvestmentProductIds()
    {
        return investmentProductIds;
    }

    public void setInvestmentProductIds(String[] investmentProductIds)
    {
        this.investmentProductIds = investmentProductIds;
    }
}

1 个答案:

答案 0 :(得分:0)

@Pattern无法应用于String[]

相反,您可以使用List替换数组:

@Valid
private List<@Pattern(regexp = InputValidationPattern.REGULAR_TEXT) String> legalNames;

在java 7及更早版本中,您必须为String[]

创建自定义约束